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.
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
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/",
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime.GOROOT() + "/lib/time/zoneinfo/",
|
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
|
|
|
}
|