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 // Parse "zoneinfo" time zone file.
6 // This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
7 // See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
8 // and ftp://munnari.oz.au/pub/oldtz/
19 headerSize = 4 + 16 + 4*7
20 zoneDir = "/usr/share/zoneinfo/"
21 zoneDir2 = "/usr/share/lib/zoneinfo/"
24 // Simple I/O interface to binary blob of data.
31 func (d *data) read(n int) []byte {
42 func (d *data) big4() (n uint32, ok bool) {
48 return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
51 func (d *data) byte() (n byte, ok bool) {
61 // Make a string by stopping at the first NUL
62 func byteString(p []byte) string {
63 for i := 0; i < len(p); i++ {
71 // Parsed representation
78 type zonetime struct {
79 time int32 // transition time, in seconds since 1970 GMT
80 zone *zone // the zone that goes into effect at that time
81 isstd, isutc bool // ignored - no idea what these mean
84 func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
85 d := data{bytes, false}
87 // 4-byte magic "TZif"
88 if magic := d.read(4); string(magic) != "TZif" {
92 // 1-byte version, then 15 bytes of padding
94 if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
98 // six big-endian 32-bit integers:
99 // number of UTC/local indicators
100 // number of standard/wall indicators
101 // number of leap seconds
102 // number of transition times
103 // number of local time zones
104 // number of characters of time zone abbrev strings
114 for i := 0; i < 6; i++ {
123 txtimes := data{d.read(n[NTime] * 4), false}
125 // Time zone indices for transition times.
126 txzones := d.read(n[NTime])
128 // Zone info structures
129 zonedata := data{d.read(n[NZone] * 6), false}
131 // Time zone abbreviations.
132 abbrev := d.read(n[NChar])
134 // Leap-second time pairs
137 // Whether tx times associated with local time types
138 // are specified as standard time or wall time.
139 isstd := d.read(n[NStdWall])
141 // Whether tx times associated with local time types
142 // are specified as UTC or local time.
143 isutc := d.read(n[NUTCLocal])
145 if d.error { // ran out of data
149 // If version == 2, the entire file repeats, this time using
150 // 8-byte ints for txtimes and leap seconds.
151 // We won't need those until 2106.
153 // Now we can build up a useful data structure.
154 // First the zone information.
155 // utcoff[4] isdst[1] nameindex[1]
156 z := make([]zone, n[NZone])
157 for i := 0; i < len(z); i++ {
160 if n, ok = zonedata.big4(); !ok {
165 if b, ok = zonedata.byte(); !ok {
169 if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
172 z[i].name = byteString(abbrev[b:])
175 // Now the transition time info.
176 zt = make([]zonetime, n[NTime])
177 for i := 0; i < len(zt); i++ {
180 if n, ok = txtimes.big4(); !ok {
183 zt[i].time = int32(n)
184 if int(txzones[i]) >= len(z) {
187 zt[i].zone = &z[txzones[i]]
189 zt[i].isstd = isstd[i] != 0
192 zt[i].isutc = isutc[i] != 0
198 func readinfofile(name string) ([]zonetime, bool) {
199 buf, err := ioutil.ReadFile(name)
203 return parseinfo(buf)
207 var onceSetupZone sync.Once
210 // consult $TZ to find the time zone to use.
211 // no $TZ means use the system default /etc/localtime.
212 // $TZ="" means use UTC.
213 // $TZ="foo" means use /usr/share/zoneinfo/foo.
215 tz, err := os.Getenverror("TZ")
217 case err == os.ENOENV:
218 zones, _ = readinfofile("/etc/localtime")
221 zones, ok = readinfofile(zoneDir + tz)
223 zones, _ = readinfofile(zoneDir2 + tz)
226 // do nothing: use UTC
230 // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
231 func lookupTimezone(sec int64) (zone string, offset int) {
232 onceSetupZone.Do(setupZone)
237 // Binary search for entry with largest time <= sec
241 if sec < int64(tz[m].time) {
248 return z.name, z.utcoff
251 // lookupByName returns the time offset for the
252 // time zone with the given abbreviation. It only considers
253 // time zones that apply to the current system.
254 // For example, for a system configured as being in New York,
255 // it only recognizes "EST" and "EDT".
256 // For a system in San Francisco, "PST" and "PDT".
257 // For a system in Sydney, "EST" and "EDT", though they have
258 // different meanings than they do in New York.
259 func lookupByName(name string) (off int, found bool) {
260 onceSetupZone.Do(setupZone)
261 for _, z := range zones {
262 if name == z.zone.name {
263 return z.zone.utcoff, true