]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/time/zoneinfo_unix.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / time / zoneinfo_unix.go
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.
4
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/
9
10 package time
11
12 import (
13 "io/ioutil"
14 "os"
15 "sync"
16 )
17
18 const (
19 headerSize = 4 + 16 + 4*7
20 zoneDir = "/usr/share/zoneinfo/"
21 )
22
23 // Simple I/O interface to binary blob of data.
24 type data struct {
25 p []byte
26 error bool
27 }
28
29
30 func (d *data) read(n int) []byte {
31 if len(d.p) < n {
32 d.p = nil
33 d.error = true
34 return nil
35 }
36 p := d.p[0:n]
37 d.p = d.p[n:]
38 return p
39 }
40
41 func (d *data) big4() (n uint32, ok bool) {
42 p := d.read(4)
43 if len(p) < 4 {
44 d.error = true
45 return 0, false
46 }
47 return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
48 }
49
50 func (d *data) byte() (n byte, ok bool) {
51 p := d.read(1)
52 if len(p) < 1 {
53 d.error = true
54 return 0, false
55 }
56 return p[0], true
57 }
58
59
60 // Make a string by stopping at the first NUL
61 func byteString(p []byte) string {
62 for i := 0; i < len(p); i++ {
63 if p[i] == 0 {
64 return string(p[0:i])
65 }
66 }
67 return string(p)
68 }
69
70 // Parsed representation
71 type zone struct {
72 utcoff int
73 isdst bool
74 name string
75 }
76
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
81 }
82
83 func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
84 d := data{bytes, false}
85
86 // 4-byte magic "TZif"
87 if magic := d.read(4); string(magic) != "TZif" {
88 return nil, false
89 }
90
91 // 1-byte version, then 15 bytes of padding
92 var p []byte
93 if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
94 return nil, false
95 }
96
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
104 const (
105 NUTCLocal = iota
106 NStdWall
107 NLeap
108 NTime
109 NZone
110 NChar
111 )
112 var n [6]int
113 for i := 0; i < 6; i++ {
114 nn, ok := d.big4()
115 if !ok {
116 return nil, false
117 }
118 n[i] = int(nn)
119 }
120
121 // Transition times.
122 txtimes := data{d.read(n[NTime] * 4), false}
123
124 // Time zone indices for transition times.
125 txzones := d.read(n[NTime])
126
127 // Zone info structures
128 zonedata := data{d.read(n[NZone] * 6), false}
129
130 // Time zone abbreviations.
131 abbrev := d.read(n[NChar])
132
133 // Leap-second time pairs
134 d.read(n[NLeap] * 8)
135
136 // Whether tx times associated with local time types
137 // are specified as standard time or wall time.
138 isstd := d.read(n[NStdWall])
139
140 // Whether tx times associated with local time types
141 // are specified as UTC or local time.
142 isutc := d.read(n[NUTCLocal])
143
144 if d.error { // ran out of data
145 return nil, false
146 }
147
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.
151
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++ {
157 var ok bool
158 var n uint32
159 if n, ok = zonedata.big4(); !ok {
160 return nil, false
161 }
162 z[i].utcoff = int(n)
163 var b byte
164 if b, ok = zonedata.byte(); !ok {
165 return nil, false
166 }
167 z[i].isdst = b != 0
168 if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
169 return nil, false
170 }
171 z[i].name = byteString(abbrev[b:])
172 }
173
174 // Now the transition time info.
175 zt = make([]zonetime, n[NTime])
176 for i := 0; i < len(zt); i++ {
177 var ok bool
178 var n uint32
179 if n, ok = txtimes.big4(); !ok {
180 return nil, false
181 }
182 zt[i].time = int32(n)
183 if int(txzones[i]) >= len(z) {
184 return nil, false
185 }
186 zt[i].zone = &z[txzones[i]]
187 if i < len(isstd) {
188 zt[i].isstd = isstd[i] != 0
189 }
190 if i < len(isutc) {
191 zt[i].isutc = isutc[i] != 0
192 }
193 }
194 return zt, true
195 }
196
197 func readinfofile(name string) ([]zonetime, bool) {
198 buf, err := ioutil.ReadFile(name)
199 if err != nil {
200 return nil, false
201 }
202 return parseinfo(buf)
203 }
204
205 var zones []zonetime
206 var onceSetupZone sync.Once
207
208 func setupZone() {
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.
213
214 tz, err := os.Getenverror("TZ")
215 switch {
216 case err == os.ENOENV:
217 zones, _ = readinfofile("/etc/localtime")
218 case len(tz) > 0:
219 zones, _ = readinfofile(zoneDir + tz)
220 case len(tz) == 0:
221 // do nothing: use UTC
222 }
223 }
224
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)
228 if len(zones) == 0 {
229 return "UTC", 0
230 }
231
232 // Binary search for entry with largest time <= sec
233 tz := zones
234 for len(tz) > 1 {
235 m := len(tz) / 2
236 if sec < int64(tz[m].time) {
237 tz = tz[0:m]
238 } else {
239 tz = tz[m:]
240 }
241 }
242 z := tz[0].zone
243 return z.name, z.utcoff
244 }
245
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
259 }
260 }
261 return 0, false
262 }