2010-12-03 04:34:57 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-07-30 14:28:58 -07:00
|
|
|
//go:build aix || (darwin && !ios) || dragonfly || freebsd || hurd || (linux && !android) || netbsd || openbsd || solaris
|
2011-10-26 23:57:58 +00:00
|
|
|
|
2010-12-03 04:34:57 +00:00
|
|
|
// Parse "zoneinfo" time zone file.
|
|
|
|
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
|
2018-09-24 21:46:21 +00:00
|
|
|
// See tzfile(5), https://en.wikipedia.org/wiki/Zoneinfo,
|
2010-12-03 04:34:57 +00:00
|
|
|
// and ftp://munnari.oz.au/pub/oldtz/
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2012-03-02 20:01:37 +00:00
|
|
|
"runtime"
|
2011-12-13 19:16:27 +00:00
|
|
|
"syscall"
|
2010-12-03 04:34:57 +00:00
|
|
|
)
|
|
|
|
|
2011-12-13 19:16:27 +00:00
|
|
|
// Many systems use /usr/share/zoneinfo, Solaris 2 has
|
|
|
|
// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
|
2018-01-09 01:23:08 +00:00
|
|
|
var zoneSources = []string{
|
2011-12-13 19:16:27 +00:00
|
|
|
"/usr/share/zoneinfo/",
|
|
|
|
"/usr/share/lib/zoneinfo/",
|
|
|
|
"/usr/lib/locale/TZ/",
|
2013-11-27 01:05:38 +00:00
|
|
|
runtime.GOROOT() + "/lib/time/zoneinfo.zip",
|
|
|
|
}
|
|
|
|
|
2011-12-13 19:16:27 +00:00
|
|
|
func initLocal() {
|
2010-12-03 04:34:57 +00:00
|
|
|
// consult $TZ to find the time zone to use.
|
|
|
|
// no $TZ means use the system default /etc/localtime.
|
|
|
|
// $TZ="" means use UTC.
|
2020-12-23 09:57:37 -08:00
|
|
|
// $TZ="foo" or $TZ=":foo" if foo is an absolute path, then the file pointed
|
|
|
|
// by foo will be used to initialize timezone; otherwise, file
|
|
|
|
// /usr/share/zoneinfo/foo will be used.
|
2010-12-03 04:34:57 +00:00
|
|
|
|
2011-12-13 19:16:27 +00:00
|
|
|
tz, ok := syscall.Getenv("TZ")
|
2010-12-03 04:34:57 +00:00
|
|
|
switch {
|
2011-12-13 19:16:27 +00:00
|
|
|
case !ok:
|
2020-07-27 22:27:54 -07:00
|
|
|
z, err := loadLocation("localtime", []string{"/etc"})
|
2011-12-13 19:16:27 +00:00
|
|
|
if err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
localLoc.name = "Local"
|
|
|
|
return
|
|
|
|
}
|
2020-12-23 09:57:37 -08:00
|
|
|
case tz != "":
|
|
|
|
if tz[0] == ':' {
|
|
|
|
tz = tz[1:]
|
|
|
|
}
|
|
|
|
if tz != "" && tz[0] == '/' {
|
|
|
|
if z, err := loadLocation(tz, []string{""}); err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
if tz == "/etc/localtime" {
|
|
|
|
localLoc.name = "Local"
|
|
|
|
} else {
|
|
|
|
localLoc.name = tz
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if tz != "" && tz != "UTC" {
|
|
|
|
if z, err := loadLocation(tz, zoneSources); err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
return
|
|
|
|
}
|
2011-12-13 19:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to UTC.
|
|
|
|
localLoc.name = "UTC"
|
|
|
|
}
|