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 dragonfly freebsd linux netbsd openbsd 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 12package time 13 14import ( 15 "errors" 16 "runtime" 17 "syscall" 18) 19 20func 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. 27var 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 34var origZoneDirs = zoneDirs 35 36func 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 46func 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 72func 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 77 } 78 } 79 return nil, errors.New("unknown time zone " + name) 80} 81