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), https://en.wikipedia.org/wiki/Zoneinfo,
8 // and ftp://munnari.oz.au/pub/oldtz/
18 // maxFileSize is the max permitted size of files read by readFile.
19 // As reference, the zoneinfo.zip distributed by Go is ~350 KB,
20 // so 10MB is overkill.
21 const maxFileSize = 10 << 20
23 type fileSizeError string
25 func (f fileSizeError) Error() string {
26 return "time: file " + string(f) + " is too large"
29 // Copies of io.Seek* constants to avoid importing "io":
36 // Simple I/O interface to binary blob of data.
42 func (d *dataIO) read(n int) []byte {
53 func (d *dataIO) big4() (n uint32, ok bool) {
59 return uint32(p[3]) | uint32(p[2])<<8 | uint32(p[1])<<16 | uint32(p[0])<<24, true
62 func (d *dataIO) byte() (n byte, ok bool) {
71 // Make a string by stopping at the first NUL
72 func byteString(p []byte) string {
73 for i := 0; i < len(p); i++ {
81 var badData = errors.New("malformed time zone information")
83 // LoadLocationFromTZData returns a Location with the given name
84 // initialized from the IANA Time Zone database-formatted data.
85 // The data should be in the format of a standard IANA time zone file
86 // (for example, the content of /etc/localtime on Unix systems).
87 func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
88 d := dataIO{data, false}
90 // 4-byte magic "TZif"
91 if magic := d.read(4); string(magic) != "TZif" {
95 // 1-byte version, then 15 bytes of padding
97 if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' && p[0] != '3' {
101 // six big-endian 32-bit integers:
102 // number of UTC/local indicators
103 // number of standard/wall indicators
104 // number of leap seconds
105 // number of transition times
106 // number of local time zones
107 // number of characters of time zone abbrev strings
117 for i := 0; i < 6; i++ {
126 txtimes := dataIO{d.read(n[NTime] * 4), false}
128 // Time zone indices for transition times.
129 txzones := d.read(n[NTime])
131 // Zone info structures
132 zonedata := dataIO{d.read(n[NZone] * 6), false}
134 // Time zone abbreviations.
135 abbrev := d.read(n[NChar])
137 // Leap-second time pairs
140 // Whether tx times associated with local time types
141 // are specified as standard time or wall time.
142 isstd := d.read(n[NStdWall])
144 // Whether tx times associated with local time types
145 // are specified as UTC or local time.
146 isutc := d.read(n[NUTCLocal])
148 if d.error { // ran out of data
152 // If version == 2 or 3, the entire file repeats, this time using
153 // 8-byte ints for txtimes and leap seconds.
154 // We won't need those until 2106.
156 // Now we can build up a useful data structure.
157 // First the zone information.
158 // utcoff[4] isdst[1] nameindex[1]
159 zone := make([]zone, n[NZone])
160 for i := range zone {
163 if n, ok = zonedata.big4(); !ok {
166 zone[i].offset = int(int32(n))
168 if b, ok = zonedata.byte(); !ok {
171 zone[i].isDST = b != 0
172 if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
175 zone[i].name = byteString(abbrev[b:])
176 if runtime.GOOS == "aix" && len(name) > 8 && (name[:8] == "Etc/GMT+" || name[:8] == "Etc/GMT-") {
177 // There is a bug with AIX 7.2 TL 0 with files in Etc,
178 // GMT+1 will return GMT-1 instead of GMT+1 or -01.
179 if name != "Etc/GMT+0" {
181 zone[i].name = name[4:]
186 // Now the transition time info.
187 tx := make([]zoneTrans, n[NTime])
191 if n, ok = txtimes.big4(); !ok {
194 tx[i].when = int64(int32(n))
195 if int(txzones[i]) >= len(zone) {
198 tx[i].index = txzones[i]
200 tx[i].isstd = isstd[i] != 0
203 tx[i].isutc = isutc[i] != 0
208 // Build fake transition to cover all time.
209 // This happens in fixed locations like "Etc/GMT0".
210 tx = append(tx, zoneTrans{when: alpha, index: 0})
213 // Committed to succeed.
214 l := &Location{zone: zone, tx: tx, name: name}
216 // Fill in the cache with information about right now,
217 // since that will be the most common lookup.
220 if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
221 l.cacheStart = tx[i].when
224 l.cacheEnd = tx[i+1].when
226 l.cacheZone = &l.zone[tx[i].index]
233 // loadTzinfoFromDirOrZip returns the contents of the file with the given name
234 // in dir. dir can either be an uncompressed zip file, or a directory.
235 func loadTzinfoFromDirOrZip(dir, name string) ([]byte, error) {
236 if len(dir) > 4 && dir[len(dir)-4:] == ".zip" {
237 return loadTzinfoFromZip(dir, name)
240 name = dir + "/" + name
242 return readFile(name)
245 // There are 500+ zoneinfo files. Rather than distribute them all
246 // individually, we ship them in an uncompressed zip file.
247 // Used this way, the zip file format serves as a commonly readable
248 // container for the individual small files. We choose zip over tar
249 // because zip files have a contiguous table of contents, making
250 // individual file lookups faster, and because the per-file overhead
251 // in a zip file is considerably less than tar's 512 bytes.
253 // get4 returns the little-endian 32-bit value in b.
254 func get4(b []byte) int {
258 return int(b[0]) | int(b[1])<<8 | int(b[2])<<16 | int(b[3])<<24
261 // get2 returns the little-endian 16-bit value in b.
262 func get2(b []byte) int {
266 return int(b[0]) | int(b[1])<<8
269 // loadTzinfoFromZip returns the contents of the file with the given name
270 // in the given uncompressed zip file.
271 func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
272 fd, err := open(zipfile)
279 zecheader = 0x06054b50
280 zcheader = 0x02014b50
287 buf := make([]byte, ztailsize)
288 if err := preadn(fd, buf, -ztailsize); err != nil || get4(buf) != zecheader {
289 return nil, errors.New("corrupt zip file " + zipfile)
292 size := get4(buf[12:])
293 off := get4(buf[16:])
295 buf = make([]byte, size)
296 if err := preadn(fd, buf, off); err != nil {
297 return nil, errors.New("corrupt zip file " + zipfile)
300 for i := 0; i < n; i++ {
322 // 46+namelen+xlen+fclen - next header
324 if get4(buf) != zcheader {
327 meth := get2(buf[10:])
328 size := get4(buf[24:])
329 namelen := get2(buf[28:])
330 xlen := get2(buf[30:])
331 fclen := get2(buf[32:])
332 off := get4(buf[42:])
333 zname := buf[46 : 46+namelen]
334 buf = buf[46+namelen+xlen+fclen:]
335 if string(zname) != name {
339 return nil, errors.New("unsupported compression for " + name + " in " + zipfile)
342 // zip per-file header layout:
356 // 30+namelen+xlen - file data
358 buf = make([]byte, zheadersize+namelen)
359 if err := preadn(fd, buf, off); err != nil ||
360 get4(buf) != zheader ||
361 get2(buf[8:]) != meth ||
362 get2(buf[26:]) != namelen ||
363 string(buf[30:30+namelen]) != name {
364 return nil, errors.New("corrupt zip file " + zipfile)
366 xlen = get2(buf[28:])
368 buf = make([]byte, size)
369 if err := preadn(fd, buf, off+30+namelen+xlen); err != nil {
370 return nil, errors.New("corrupt zip file " + zipfile)
376 return nil, syscall.ENOENT
379 // loadTzinfoFromTzdata returns the time zone information of the time zone
380 // with the given name, from a tzdata database file as they are typically
382 var loadTzinfoFromTzdata func(file, name string) ([]byte, error)
384 // loadTzinfo returns the time zone information of the time zone
385 // with the given name, from a given source. A source may be a
386 // timezone database directory, tzdata database file or an uncompressed
387 // zip file, containing the contents of such a directory.
388 func loadTzinfo(name string, source string) ([]byte, error) {
389 if len(source) >= 6 && source[len(source)-6:] == "tzdata" {
390 return loadTzinfoFromTzdata(source, name)
392 return loadTzinfoFromDirOrZip(source, name)
395 // loadLocation returns the Location with the given name from one of
396 // the specified sources. See loadTzinfo for a list of supported sources.
397 // The first timezone data matching the given name that is successfully loaded
398 // and parsed is returned as a Location.
399 func loadLocation(name string, sources []string) (z *Location, firstErr error) {
400 for _, source := range sources {
401 var zoneData, err = loadTzinfo(name, source)
403 if z, err = LoadLocationFromTZData(name, zoneData); err == nil {
407 if firstErr == nil && err != syscall.ENOENT {
414 return nil, errors.New("unknown time zone " + name)
417 // readFile reads and returns the content of the named file.
418 // It is a trivial implementation of ioutil.ReadFile, reimplemented
419 // here to avoid depending on io/ioutil or os.
420 // It returns an error if name exceeds maxFileSize bytes.
421 func readFile(name string) ([]byte, error) {
433 n, err = read(f, buf[:])
435 ret = append(ret, buf[:n]...)
437 if n == 0 || err != nil {
440 if len(ret) > maxFileSize {
441 return nil, fileSizeError(name)