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/"
23 // Simple I/O interface to binary blob of data.
30 func (d *data) read(n int) []byte {
41 func (d *data) big4() (n uint32, ok bool) {
47 return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
50 func (d *data) byte() (n byte, ok bool) {
60 // Make a string by stopping at the first NUL
61 func byteString(p []byte) string {
62 for i := 0; i < len(p); i++ {
70 // Parsed representation
77 type zonetime struct {
78 time int32 // transition time, in seconds since 1970 GMT
79 zone *zone // the zone that goes into effect at that time
80 isstd, isutc bool // ignored - no idea what these mean
83 func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
84 d := data{bytes, false}
86 // 4-byte magic "TZif"
87 if magic := d.read(4); string(magic) != "TZif" {
91 // 1-byte version, then 15 bytes of padding
93 if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
97 // six big-endian 32-bit integers:
98 // number of UTC/local indicators
99 // number of standard/wall indicators
100 // number of leap seconds
101 // number of transition times
102 // number of local time zones
103 // number of characters of time zone abbrev strings
113 for i := 0; i < 6; i++ {
122 txtimes := data{d.read(n[NTime] * 4), false}
124 // Time zone indices for transition times.
125 txzones := d.read(n[NTime])
127 // Zone info structures
128 zonedata := data{d.read(n[NZone] * 6), false}
130 // Time zone abbreviations.
131 abbrev := d.read(n[NChar])
133 // Leap-second time pairs
136 // Whether tx times associated with local time types
137 // are specified as standard time or wall time.
138 isstd := d.read(n[NStdWall])
140 // Whether tx times associated with local time types
141 // are specified as UTC or local time.
142 isutc := d.read(n[NUTCLocal])
144 if d.error { // ran out of data
148 // If version == 2, the entire file repeats, this time using
149 // 8-byte ints for txtimes and leap seconds.
150 // We won't need those until 2106.
152 // Now we can build up a useful data structure.
153 // First the zone information.
154 // utcoff[4] isdst[1] nameindex[1]
155 z := make([]zone, n[NZone])
156 for i := 0; i < len(z); i++ {
159 if n, ok = zonedata.big4(); !ok {
164 if b, ok = zonedata.byte(); !ok {
168 if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
171 z[i].name = byteString(abbrev[b:])
174 // Now the transition time info.
175 zt = make([]zonetime, n[NTime])
176 for i := 0; i < len(zt); i++ {
179 if n, ok = txtimes.big4(); !ok {
182 zt[i].time = int32(n)
183 if int(txzones[i]) >= len(z) {
186 zt[i].zone = &z[txzones[i]]
188 zt[i].isstd = isstd[i] != 0
191 zt[i].isutc = isutc[i] != 0
197 func readinfofile(name string) ([]zonetime, bool) {
198 buf, err := ioutil.ReadFile(name)
202 return parseinfo(buf)
206 var onceSetupZone sync.Once
209 // consult $TZ to find the time zone to use.
210 // no $TZ means use the system default /etc/localtime.
211 // $TZ="" means use UTC.
212 // $TZ="foo" means use /usr/share/zoneinfo/foo.
214 tz, err := os.Getenverror("TZ")
216 case err == os.ENOENV:
217 zones, _ = readinfofile("/etc/localtime")
219 zones, _ = readinfofile(zoneDir + tz)
221 // do nothing: use UTC
225 // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
226 func lookupTimezone(sec int64) (zone string, offset int) {
227 onceSetupZone.Do(setupZone)
232 // Binary search for entry with largest time <= sec
236 if sec < int64(tz[m].time) {
243 return z.name, z.utcoff
246 // lookupByName returns the time offset for the
247 // time zone with the given abbreviation. It only considers
248 // time zones that apply to the current system.
249 // For example, for a system configured as being in New York,
250 // it only recognizes "EST" and "EDT".
251 // For a system in San Francisco, "PST" and "PDT".
252 // For a system in Sydney, "EST" and "EDT", though they have
253 // different meanings than they do in New York.
254 func lookupByName(name string) (off int, found bool) {
255 onceSetupZone.Do(setupZone)
256 for _, z := range zones {
257 if name == z.zone.name {
258 return z.zone.utcoff, true