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 freebsd linux openbsd
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 headerSize = 4 + 16 + 4*7
23 // Simple I/O interface to binary blob of data.
29 func (d *data) read(n int) []byte {
40 func (d *data) big4() (n uint32, ok bool) {
46 return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
49 func (d *data) byte() (n byte, ok bool) {
58 // Make a string by stopping at the first NUL
59 func byteString(p []byte) string {
60 for i := 0; i < len(p); i++ {
68 func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
69 d := data{bytes, false}
71 // 4-byte magic "TZif"
72 if magic := d.read(4); string(magic) != "TZif" {
76 // 1-byte version, then 15 bytes of padding
78 if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
82 // six big-endian 32-bit integers:
83 // number of UTC/local indicators
84 // number of standard/wall indicators
85 // number of leap seconds
86 // number of transition times
87 // number of local time zones
88 // number of characters of time zone abbrev strings
98 for i := 0; i < 6; i++ {
107 txtimes := data{d.read(n[NTime] * 4), false}
109 // Time zone indices for transition times.
110 txzones := d.read(n[NTime])
112 // Zone info structures
113 zonedata := data{d.read(n[NZone] * 6), false}
115 // Time zone abbreviations.
116 abbrev := d.read(n[NChar])
118 // Leap-second time pairs
121 // Whether tx times associated with local time types
122 // are specified as standard time or wall time.
123 isstd := d.read(n[NStdWall])
125 // Whether tx times associated with local time types
126 // are specified as UTC or local time.
127 isutc := d.read(n[NUTCLocal])
129 if d.error { // ran out of data
133 // If version == 2, the entire file repeats, this time using
134 // 8-byte ints for txtimes and leap seconds.
135 // We won't need those until 2106.
137 // Now we can build up a useful data structure.
138 // First the zone information.
139 // utcoff[4] isdst[1] nameindex[1]
140 z := make([]zone, n[NZone])
141 for i := 0; i < len(z); i++ {
144 if n, ok = zonedata.big4(); !ok {
149 if b, ok = zonedata.byte(); !ok {
153 if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
156 z[i].name = byteString(abbrev[b:])
159 // Now the transition time info.
160 zt = make([]zonetime, n[NTime])
161 for i := 0; i < len(zt); i++ {
164 if n, ok = txtimes.big4(); !ok {
167 zt[i].time = int32(n)
168 if int(txzones[i]) >= len(z) {
171 zt[i].zone = &z[txzones[i]]
173 zt[i].isstd = isstd[i] != 0
176 zt[i].isutc = isutc[i] != 0
182 func readinfofile(name string) ([]zonetime, bool) {
183 buf, err := ioutil.ReadFile(name)
187 return parseinfo(buf)
190 func setupTestingZone() {
191 os.Setenv("TZ", "America/Los_Angeles")
196 // consult $TZ to find the time zone to use.
197 // no $TZ means use the system default /etc/localtime.
198 // $TZ="" means use UTC.
199 // $TZ="foo" means use /usr/share/zoneinfo/foo.
200 // Many systems use /usr/share/zoneinfo, Solaris 2 has
201 // /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
202 zoneDirs := []string{"/usr/share/zoneinfo/",
203 "/usr/share/lib/zoneinfo/",
204 "/usr/lib/locale/TZ/"}
206 tz, err := os.Getenverror("TZ")
208 case err == os.ENOENV:
209 zones, _ = readinfofile("/etc/localtime")
211 for _, zoneDir := range zoneDirs {
213 if zones, ok = readinfofile(zoneDir + tz); ok {
218 // do nothing: use UTC