PR c/64417
[official-gcc.git] / libgo / go / time / zoneinfo_unix.go
blob3fe8e55795c0f724ec9c72543836a38290cc1658
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 dragonfly freebsd linux 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/
12 package time
14 import (
15 "errors"
16 "runtime"
17 "syscall"
20 func initTestingZone() {
21 syscall.Setenv("TZ", "America/Los_Angeles")
22 initLocal()
25 // Many systems use /usr/share/zoneinfo, Solaris 2 has
26 // /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
27 var zoneDirs = []string{
28 "/usr/share/zoneinfo/",
29 "/usr/share/lib/zoneinfo/",
30 "/usr/lib/locale/TZ/",
31 runtime.GOROOT() + "/lib/time/zoneinfo.zip",
34 var origZoneDirs = zoneDirs
36 func forceZipFileForTesting(zipOnly bool) {
37 zoneDirs = make([]string, len(origZoneDirs))
38 copy(zoneDirs, origZoneDirs)
39 if zipOnly {
40 for i := 0; i < len(zoneDirs)-1; i++ {
41 zoneDirs[i] = "/XXXNOEXIST"
46 func initLocal() {
47 // consult $TZ to find the time zone to use.
48 // no $TZ means use the system default /etc/localtime.
49 // $TZ="" means use UTC.
50 // $TZ="foo" means use /usr/share/zoneinfo/foo.
52 tz, ok := syscall.Getenv("TZ")
53 switch {
54 case !ok:
55 z, err := loadZoneFile("", "/etc/localtime")
56 if err == nil {
57 localLoc = *z
58 localLoc.name = "Local"
59 return
61 case tz != "" && tz != "UTC":
62 if z, err := loadLocation(tz); err == nil {
63 localLoc = *z
64 return
68 // Fall back to UTC.
69 localLoc.name = "UTC"
72 func loadLocation(name string) (*Location, error) {
73 for _, zoneDir := range zoneDirs {
74 if z, err := loadZoneFile(zoneDir, name); err == nil {
75 z.name = name
76 return z, nil
79 return nil, errors.New("unknown time zone " + name)