Skip to content

feat(freebsd): add support for freebsd #5102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions coderd/util/tz/tz_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build freebsd

package tz

import (
"path/filepath"
"strings"
"time"

"golang.org/x/xerrors"
)

const etcLocaltime = "/etc/localtime"
const zoneInfoPath = "/usr/share/zoneinfo"

// TimezoneIANA attempts to determine the local timezone in IANA format.
// If the TZ environment variable is set, this is used.
// Otherwise, /etc/localtime is used to determine the timezone.
// Reference: https://stackoverflow.com/a/63805394
// On Windows platforms, instead of reading /etc/localtime, powershell
// is used instead to get the current time location in IANA format.
// Reference: https://superuser.com/a/1584968
func TimezoneIANA() (*time.Location, error) {
loc, err := locationFromEnv()
if err == nil {
return loc, nil
}
if !xerrors.Is(err, errNoEnvSet) {
return nil, xerrors.Errorf("lookup timezone from env: %w", err)
}

lp, err := filepath.EvalSymlinks(etcLocaltime)
if err != nil {
return nil, xerrors.Errorf("read location of %s: %w", etcLocaltime, err)
}
stripped := strings.Replace(lp, zoneInfoPath, "", -1)
stripped = strings.TrimPrefix(stripped, string(filepath.Separator))
loc, err = time.LoadLocation(stripped)
if err != nil {
return nil, xerrors.Errorf("invalid location %q guessed from %s: %w", stripped, lp, err)
}
return loc, nil
}