-
Notifications
You must be signed in to change notification settings - Fork 611
/
tz_linux.go
45 lines (39 loc) · 1.24 KB
/
tz_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//go:build linux
package tz
import (
"path/filepath"
"strings"
"time"
"golang.org/x/xerrors"
)
const (
etcLocaltime = "/etc/localtime"
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
}