github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/third_party/gofrontend/libgo/go/time/zoneinfo_unix.go (about) 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. 4 5 // +build darwin,386 darwin,amd64 dragonfly freebsd linux nacl netbsd openbsd solaris 6 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/ 11 12 package time 13 14 import ( 15 "errors" 16 "runtime" 17 "syscall" 18 ) 19 20 func initTestingZone() { 21 syscall.Setenv("TZ", "America/Los_Angeles") 22 initLocal() 23 } 24 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", 32 } 33 34 var origZoneDirs = zoneDirs 35 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" 42 } 43 } 44 } 45 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. 51 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 60 } 61 case tz != "" && tz != "UTC": 62 if z, err := loadLocation(tz); err == nil { 63 localLoc = *z 64 return 65 } 66 } 67 68 // Fall back to UTC. 69 localLoc.name = "UTC" 70 } 71 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 80 } 81 } 82 if firstErr != nil { 83 return nil, firstErr 84 } 85 return nil, errors.New("unknown time zone " + name) 86 }