2010-12-03 05:34:57 +01: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.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Parse "zoneinfo" time zone file.
|
|
|
|
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
|
|
|
|
// See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
|
|
|
|
// and ftp://munnari.oz.au/pub/oldtz/
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2011-12-13 20:16:27 +01:00
|
|
|
"errors"
|
2012-03-02 21:01:37 +01:00
|
|
|
"runtime"
|
2011-12-13 20:16:27 +01:00
|
|
|
"syscall"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
func initTestingZone() {
|
|
|
|
syscall.Setenv("TZ", "America/Los_Angeles")
|
|
|
|
initLocal()
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Many systems use /usr/share/zoneinfo, Solaris 2 has
|
|
|
|
// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
|
|
|
|
var zoneDirs = []string{
|
|
|
|
"/usr/share/zoneinfo/",
|
|
|
|
"/usr/share/lib/zoneinfo/",
|
|
|
|
"/usr/lib/locale/TZ/",
|
2013-11-27 02:05:38 +01:00
|
|
|
runtime.GOROOT() + "/lib/time/zoneinfo.zip",
|
|
|
|
}
|
|
|
|
|
|
|
|
var origZoneDirs = zoneDirs
|
|
|
|
|
|
|
|
func forceZipFileForTesting(zipOnly bool) {
|
|
|
|
zoneDirs = make([]string, len(origZoneDirs))
|
|
|
|
copy(zoneDirs, origZoneDirs)
|
|
|
|
if zipOnly {
|
|
|
|
for i := 0; i < len(zoneDirs)-1; i++ {
|
|
|
|
zoneDirs[i] = "/XXXNOEXIST"
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func initLocal() {
|
2010-12-03 05:34:57 +01:00
|
|
|
// consult $TZ to find the time zone to use.
|
|
|
|
// no $TZ means use the system default /etc/localtime.
|
|
|
|
// $TZ="" means use UTC.
|
|
|
|
// $TZ="foo" means use /usr/share/zoneinfo/foo.
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
tz, ok := syscall.Getenv("TZ")
|
2010-12-03 05:34:57 +01:00
|
|
|
switch {
|
2011-12-13 20:16:27 +01:00
|
|
|
case !ok:
|
2012-03-02 21:01:37 +01:00
|
|
|
z, err := loadZoneFile("", "/etc/localtime")
|
2011-12-13 20:16:27 +01:00
|
|
|
if err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
localLoc.name = "Local"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case tz != "" && tz != "UTC":
|
|
|
|
if z, err := loadLocation(tz); err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to UTC.
|
|
|
|
localLoc.name = "UTC"
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLocation(name string) (*Location, error) {
|
|
|
|
for _, zoneDir := range zoneDirs {
|
2012-03-02 21:01:37 +01:00
|
|
|
if z, err := loadZoneFile(zoneDir, name); err == nil {
|
2011-12-13 20:16:27 +01:00
|
|
|
z.name = name
|
|
|
|
return z, nil
|
2011-01-29 01:07:56 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return nil, errors.New("unknown time zone " + name)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|