2008-09-17 17:20:00 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-09-22 17:41:14 -06:00
|
|
|
// Parse "zoneinfo" time zone file.
|
|
|
|
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
|
|
|
|
// See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
|
|
|
|
// and ftp://munnari.oz.au/pub/oldtz/
|
|
|
|
|
2008-09-17 17:20:00 -06:00
|
|
|
package time
|
|
|
|
|
2008-09-22 17:41:14 -06:00
|
|
|
import (
|
2009-01-16 11:14:12 -07:00
|
|
|
"io";
|
2008-09-22 17:41:14 -06:00
|
|
|
"once";
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2009-02-15 23:12:35 -07:00
|
|
|
headerSize = 4+16+4*7;
|
|
|
|
|
|
|
|
zoneDir = "/usr/share/zoneinfo/";
|
2008-09-22 17:41:14 -06:00
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:05 -07:00
|
|
|
// Errors that can be generated recovering time zone information.
|
2009-04-18 17:44:13 -06:00
|
|
|
type TimeZoneError struct {
|
|
|
|
os.ErrorString
|
|
|
|
}
|
|
|
|
|
2009-04-30 14:40:55 -06:00
|
|
|
var errShort = TimeZoneError{ "time: short zone file" }
|
|
|
|
var errInvalid = TimeZoneError{ "time: invalid zone file" }
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Simple I/O interface to binary blob of data.
|
2009-02-15 23:12:35 -07:00
|
|
|
type data struct {
|
2008-12-18 23:37:22 -07:00
|
|
|
p []byte;
|
|
|
|
error bool;
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
func (d *data) read(n int) []byte {
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(d.p) < n {
|
2008-12-19 04:05:37 -07:00
|
|
|
d.p = nil;
|
2008-12-18 23:37:22 -07:00
|
|
|
d.error = true;
|
2008-12-19 04:05:37 -07:00
|
|
|
return nil;
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
p := d.p[0:n];
|
2008-10-07 13:31:31 -06:00
|
|
|
d.p = d.p[n:len(d.p)];
|
2008-09-22 17:41:14 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
func (d *data) big4() (n uint32, ok bool) {
|
|
|
|
p := d.read(4);
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(p) < 4 {
|
|
|
|
d.error = true;
|
2008-09-22 17:41:14 -06:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return uint32(p[0]) << 24 | uint32(p[1]) << 16 | uint32(p[2]) << 8 | uint32(p[3]), true
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
func (d *data) byte() (n byte, ok bool) {
|
|
|
|
p := d.read(1);
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(p) < 1 {
|
|
|
|
d.error = true;
|
2008-09-22 17:41:14 -06:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return p[0], true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Make a string by stopping at the first NUL
|
2009-02-15 23:12:35 -07:00
|
|
|
func byteString(p []byte) string {
|
2008-09-22 17:41:14 -06:00
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
if p[i] == 0 {
|
|
|
|
return string(p[0:i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsed representation
|
2009-02-15 23:12:35 -07:00
|
|
|
type zone struct {
|
2008-09-22 17:41:14 -06:00
|
|
|
utcoff int;
|
|
|
|
isdst bool;
|
2008-12-18 23:37:22 -07:00
|
|
|
name string;
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
type zonetime struct {
|
2008-09-22 17:41:14 -06:00
|
|
|
time int32; // transition time, in seconds since 1970 GMT
|
2009-02-15 23:12:35 -07:00
|
|
|
zone *zone; // the zone that goes into effect at that time
|
2008-09-22 17:41:14 -06:00
|
|
|
isstd, isutc bool; // ignored - no idea what these mean
|
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func parseinfo(bytes []byte) (zt []zonetime, err os.Error) {
|
2009-03-03 09:39:12 -07:00
|
|
|
d := data{bytes, false};
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// 4-byte magic "TZif"
|
2009-02-15 23:12:35 -07:00
|
|
|
if magic := d.read(4); string(magic) != "TZif" {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, TimeZoneError{ "time: bad zone magic" }
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1-byte version, then 15 bytes of padding
|
2008-12-18 23:37:22 -07:00
|
|
|
var p []byte;
|
2009-02-15 23:12:35 -07:00
|
|
|
if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, TimeZoneError { "time: bad zone file version" }
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
vers := p[0];
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// six big-endian 32-bit integers:
|
|
|
|
// number of UTC/local indicators
|
|
|
|
// number of standard/wall indicators
|
|
|
|
// number of leap seconds
|
|
|
|
// number of transition times
|
|
|
|
// number of local time zones
|
|
|
|
// number of characters of time zone abbrev strings
|
|
|
|
const (
|
|
|
|
NUTCLocal = iota;
|
|
|
|
NStdWall;
|
|
|
|
NLeap;
|
|
|
|
NTime;
|
|
|
|
NZone;
|
|
|
|
NChar
|
|
|
|
)
|
2008-10-07 13:31:31 -06:00
|
|
|
var n [6]int;
|
2008-09-22 17:41:14 -06:00
|
|
|
for i := 0; i < 6; i++ {
|
2009-02-15 23:12:35 -07:00
|
|
|
nn, ok := d.big4();
|
2008-09-22 17:41:14 -06:00
|
|
|
if !ok {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errShort
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
n[i] = int(nn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transition times.
|
2009-03-03 09:39:12 -07:00
|
|
|
txtimes := data{d.read(n[NTime]*4), false};
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Time zone indices for transition times.
|
2009-02-15 23:12:35 -07:00
|
|
|
txzones := d.read(n[NTime]);
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Zone info structures
|
2009-03-03 09:39:12 -07:00
|
|
|
zonedata := data{d.read(n[NZone]*6), false};
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Time zone abbreviations.
|
2009-02-15 23:12:35 -07:00
|
|
|
abbrev := d.read(n[NChar]);
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Leap-second time pairs
|
2009-03-03 09:39:12 -07:00
|
|
|
leapdata := data{d.read(n[NLeap]*8), false};
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Whether tx times associated with local time types
|
|
|
|
// are specified as standard time or wall time.
|
2009-02-15 23:12:35 -07:00
|
|
|
isstd := d.read(n[NStdWall]);
|
2008-09-22 17:41:14 -06:00
|
|
|
|
|
|
|
// Whether tx times associated with local time types
|
|
|
|
// are specified as UTC or local time.
|
2009-02-15 23:12:35 -07:00
|
|
|
isutc := d.read(n[NUTCLocal]);
|
2008-09-22 17:41:14 -06:00
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
if d.error { // ran out of data
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errShort
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If version == 2, the entire file repeats, this time using
|
|
|
|
// 8-byte ints for txtimes and leap seconds.
|
|
|
|
// We won't need those until 2106.
|
|
|
|
|
|
|
|
// Now we can build up a useful data structure.
|
|
|
|
// First the zone information.
|
|
|
|
// utcoff[4] isdst[1] nameindex[1]
|
2009-02-15 23:12:35 -07:00
|
|
|
z := make([]zone, n[NZone]);
|
|
|
|
for i := 0; i < len(z); i++ {
|
2008-09-22 17:41:14 -06:00
|
|
|
var ok bool;
|
2008-10-07 13:31:31 -06:00
|
|
|
var n uint32;
|
2009-02-15 23:12:35 -07:00
|
|
|
if n, ok = zonedata.big4(); !ok {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errShort
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2009-02-15 23:12:35 -07:00
|
|
|
z[i].utcoff = int(n);
|
2008-10-07 13:31:31 -06:00
|
|
|
var b byte;
|
2009-02-15 23:12:35 -07:00
|
|
|
if b, ok = zonedata.byte(); !ok {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errShort
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2009-02-15 23:12:35 -07:00
|
|
|
z[i].isdst = b != 0;
|
|
|
|
if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errInvalid
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2009-02-15 23:12:35 -07:00
|
|
|
z[i].name = byteString(abbrev[b:len(abbrev)])
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now the transition time info.
|
2009-02-15 23:12:35 -07:00
|
|
|
zt = make([]zonetime, n[NTime]);
|
2008-09-22 17:41:14 -06:00
|
|
|
for i := 0; i < len(zt); i++ {
|
|
|
|
var ok bool;
|
2008-10-07 13:31:31 -06:00
|
|
|
var n uint32;
|
2009-02-15 23:12:35 -07:00
|
|
|
if n, ok = txtimes.big4(); !ok {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errShort
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
zt[i].time = int32(n);
|
2009-02-15 23:12:35 -07:00
|
|
|
if int(txzones[i]) >= len(z) {
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, errInvalid
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2009-02-15 23:12:35 -07:00
|
|
|
zt[i].zone = &z[txzones[i]];
|
2008-09-22 17:41:14 -06:00
|
|
|
if i < len(isstd) {
|
|
|
|
zt[i].isstd = isstd[i] != 0
|
|
|
|
}
|
|
|
|
if i < len(isutc) {
|
|
|
|
zt[i].isutc = isutc[i] != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zt, nil
|
|
|
|
}
|
|
|
|
|
2009-04-30 14:40:55 -06:00
|
|
|
func readinfofile(name string) ([]zonetime, os.Error) {
|
2009-06-22 14:26:13 -06:00
|
|
|
buf, err := io.ReadFile(name);
|
2009-04-30 14:40:55 -06:00
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
tx, err := parseinfo(buf);
|
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
return tx, nil;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
if tzerr, ok := err.(TimeZoneError); ok {
|
2009-05-08 16:40:14 -06:00
|
|
|
tzerr.ErrorString = os.ErrorString(tzerr.String() + ": " + name)
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2009-04-30 14:40:55 -06:00
|
|
|
return nil, err
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
var zones []zonetime
|
2009-04-17 01:08:24 -06:00
|
|
|
var zoneerr os.Error
|
2008-09-22 17:41:14 -06:00
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
func setupZone() {
|
|
|
|
// consult $TZ to find the time zone to use.
|
|
|
|
// no $TZ means use the system default /etc/localtime.
|
|
|
|
// $TZ="" means use UTC.
|
|
|
|
// $TZ="foo" means use /usr/share/zoneinfo/foo.
|
|
|
|
|
|
|
|
tz, err := os.Getenv("TZ");
|
|
|
|
var file string;
|
|
|
|
switch {
|
|
|
|
case err == os.ENOENV:
|
|
|
|
zones, zoneerr = readinfofile("/etc/localtime");
|
|
|
|
case err != nil:
|
|
|
|
zoneerr = err;
|
|
|
|
case len(tz) > 0:
|
|
|
|
zones, zoneerr = readinfofile(zoneDir + tz);
|
|
|
|
case len(tz) == 0:
|
|
|
|
// do nothing: use UTC
|
|
|
|
}
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func lookupTimezone(sec int64) (zone string, offset int, err os.Error) {
|
2009-02-15 23:12:35 -07:00
|
|
|
once.Do(setupZone);
|
2008-12-18 23:37:22 -07:00
|
|
|
if zoneerr != nil || len(zones) == 0 {
|
2009-02-15 23:12:35 -07:00
|
|
|
return "UTC", 0, zoneerr
|
2008-09-22 17:41:14 -06:00
|
|
|
}
|
2008-09-17 17:20:00 -06:00
|
|
|
|
2008-09-22 17:41:14 -06:00
|
|
|
// Binary search for entry with largest time <= sec
|
|
|
|
tz := zones;
|
|
|
|
for len(tz) > 1 {
|
|
|
|
m := len(tz)/2;
|
|
|
|
if sec < int64(tz[m].time) {
|
|
|
|
tz = tz[0:m]
|
|
|
|
} else {
|
|
|
|
tz = tz[m:len(tz)]
|
|
|
|
}
|
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
z := tz[0].zone;
|
2008-09-22 17:41:14 -06:00
|
|
|
return z.name, z.utcoff, nil
|
2008-09-17 17:20:00 -06:00
|
|
|
}
|