PR ada/66526
[official-gcc.git] / libgo / go / time / zoneinfo_unix.go
blob5fc669e478d07c637010c05492b936b53d90cc86
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 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 var firstErr error
74 for _, zoneDir := range zoneDirs {
75 if z, err := loadZoneFile(zoneDir, name); err == nil {
76 z.name = name
77 return z, nil
78 } else if firstErr == nil && !isNotExist(err) {
79 firstErr = err
82 if firstErr != nil {
83 return nil, firstErr
85 return nil, errors.New("unknown time zone " + name)