1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin,386 darwin,amd64 dragonfly freebsd linux,!android nacl netbsd openbsd solaris
7 // Parse "zoneinfo" time zone file.
8 // This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
9 // See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
10 // and ftp://munnari.oz.au/pub/oldtz/
20 func initTestingZone() {
21 z
, err
:= loadLocation("America/Los_Angeles")
23 panic("cannot load America/Los_Angeles for testing: " + err
.Error())
29 // Many systems use /usr/share/zoneinfo, Solaris 2 has
30 // /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
31 var zoneDirs
= []string{
32 "/usr/share/zoneinfo/",
33 "/usr/share/lib/zoneinfo/",
34 "/usr/lib/locale/TZ/",
35 runtime
.GOROOT() + "/lib/time/zoneinfo.zip",
38 var origZoneDirs
= zoneDirs
40 func forceZipFileForTesting(zipOnly
bool) {
41 zoneDirs
= make([]string, len(origZoneDirs
))
42 copy(zoneDirs
, origZoneDirs
)
44 for i
:= 0; i
< len(zoneDirs
)-1; i
++ {
45 zoneDirs
[i
] = "/XXXNOEXIST"
51 // consult $TZ to find the time zone to use.
52 // no $TZ means use the system default /etc/localtime.
53 // $TZ="" means use UTC.
54 // $TZ="foo" means use /usr/share/zoneinfo/foo.
56 tz
, ok
:= syscall
.Getenv("TZ")
59 z
, err
:= loadZoneFile("", "/etc/localtime")
62 localLoc
.name
= "Local"
65 case tz
!= "" && tz
!= "UTC":
66 if z
, err
:= loadLocation(tz
); err
== nil {
76 func loadLocation(name
string) (*Location
, error
) {
78 for _
, zoneDir
:= range zoneDirs
{
79 if z
, err
:= loadZoneFile(zoneDir
, name
); err
== nil {
82 } else if firstErr
== nil && !isNotExist(err
) {
89 return nil, errors
.New("unknown time zone " + name
)