Skip to content

Commit e3d18f8

Browse files
koobsghuntley
authored andcommitted
coderd/tz: Add FreeBSD tz support
`./scripts/build_go.sh --os freebsd` fails to build with the following (unique) errors: cli/schedule.go:223:19: undefined: tz.TimezoneIANA cli/util.go:116:17: undefined: tz.TimezoneIANA coderd/util/tz currently contains per-OS implementions (one file per supported OS), but is missing a tz_freebsd.go. Follow the existing pattern by creating a tz_freebsd.go from tz_linux.go. FreeBSD supports standard IANA zoneinfo (and /etc/localtime), so no other changes are required. Fixes: #4516
1 parent 629e15a commit e3d18f8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

coderd/util/tz/tz_freebsd.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build freebsd
2+
3+
package tz
4+
5+
import (
6+
"path/filepath"
7+
"strings"
8+
"time"
9+
10+
"golang.org/x/xerrors"
11+
)
12+
13+
const etcLocaltime = "/etc/localtime"
14+
const zoneInfoPath = "/usr/share/zoneinfo"
15+
16+
// TimezoneIANA attempts to determine the local timezone in IANA format.
17+
// If the TZ environment variable is set, this is used.
18+
// Otherwise, /etc/localtime is used to determine the timezone.
19+
// Reference: https://stackoverflow.com/a/63805394
20+
// On Windows platforms, instead of reading /etc/localtime, powershell
21+
// is used instead to get the current time location in IANA format.
22+
// Reference: https://superuser.com/a/1584968
23+
func TimezoneIANA() (*time.Location, error) {
24+
loc, err := locationFromEnv()
25+
if err == nil {
26+
return loc, nil
27+
}
28+
if !xerrors.Is(err, errNoEnvSet) {
29+
return nil, xerrors.Errorf("lookup timezone from env: %w", err)
30+
}
31+
32+
lp, err := filepath.EvalSymlinks(etcLocaltime)
33+
if err != nil {
34+
return nil, xerrors.Errorf("read location of %s: %w", etcLocaltime, err)
35+
}
36+
stripped := strings.Replace(lp, zoneInfoPath, "", -1)
37+
stripped = strings.TrimPrefix(stripped, string(filepath.Separator))
38+
loc, err = time.LoadLocation(stripped)
39+
if err != nil {
40+
return nil, xerrors.Errorf("invalid location %q guessed from %s: %w", stripped, lp, err)
41+
}
42+
return loc, nil
43+
}

0 commit comments

Comments
 (0)