OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [time/] [zoneinfo_unix.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
// +build darwin freebsd linux netbsd openbsd
6
 
7
// Parse "zoneinfo" time zone file.
8
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
9
// See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
10
// and ftp://munnari.oz.au/pub/oldtz/
11
 
12
package time
13
 
14
import (
15
        "errors"
16
        "syscall"
17
)
18
 
19
const (
20
        headerSize = 4 + 16 + 4*7
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
func (d *data) read(n int) []byte {
30
        if len(d.p) < n {
31
                d.p = nil
32
                d.error = true
33
                return nil
34
        }
35
        p := d.p[0:n]
36
        d.p = d.p[n:]
37
        return p
38
}
39
 
40
func (d *data) big4() (n uint32, ok bool) {
41
        p := d.read(4)
42
        if len(p) < 4 {
43
                d.error = true
44
                return 0, false
45
        }
46
        return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
47
}
48
 
49
func (d *data) byte() (n byte, ok bool) {
50
        p := d.read(1)
51
        if len(p) < 1 {
52
                d.error = true
53
                return 0, false
54
        }
55
        return p[0], true
56
}
57
 
58
// Make a string by stopping at the first NUL
59
func byteString(p []byte) string {
60
        for i := 0; i < len(p); i++ {
61
                if p[i] == 0 {
62
                        return string(p[0:i])
63
                }
64
        }
65
        return string(p)
66
}
67
 
68
var badData = errors.New("malformed time zone information")
69
 
70
func loadZoneData(bytes []byte) (l *Location, err error) {
71
        d := data{bytes, false}
72
 
73
        // 4-byte magic "TZif"
74
        if magic := d.read(4); string(magic) != "TZif" {
75
                return nil, badData
76
        }
77
 
78
        // 1-byte version, then 15 bytes of padding
79
        var p []byte
80
        if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
81
                return nil, badData
82
        }
83
 
84
        // six big-endian 32-bit integers:
85
        //      number of UTC/local indicators
86
        //      number of standard/wall indicators
87
        //      number of leap seconds
88
        //      number of transition times
89
        //      number of local time zones
90
        //      number of characters of time zone abbrev strings
91
        const (
92
                NUTCLocal = iota
93
                NStdWall
94
                NLeap
95
                NTime
96
                NZone
97
                NChar
98
        )
99
        var n [6]int
100
        for i := 0; i < 6; i++ {
101
                nn, ok := d.big4()
102
                if !ok {
103
                        return nil, badData
104
                }
105
                n[i] = int(nn)
106
        }
107
 
108
        // Transition times.
109
        txtimes := data{d.read(n[NTime] * 4), false}
110
 
111
        // Time zone indices for transition times.
112
        txzones := d.read(n[NTime])
113
 
114
        // Zone info structures
115
        zonedata := data{d.read(n[NZone] * 6), false}
116
 
117
        // Time zone abbreviations.
118
        abbrev := d.read(n[NChar])
119
 
120
        // Leap-second time pairs
121
        d.read(n[NLeap] * 8)
122
 
123
        // Whether tx times associated with local time types
124
        // are specified as standard time or wall time.
125
        isstd := d.read(n[NStdWall])
126
 
127
        // Whether tx times associated with local time types
128
        // are specified as UTC or local time.
129
        isutc := d.read(n[NUTCLocal])
130
 
131
        if d.error { // ran out of data
132
                return nil, badData
133
        }
134
 
135
        // If version == 2, the entire file repeats, this time using
136
        // 8-byte ints for txtimes and leap seconds.
137
        // We won't need those until 2106.
138
 
139
        // Now we can build up a useful data structure.
140
        // First the zone information.
141
        //      utcoff[4] isdst[1] nameindex[1]
142
        zone := make([]zone, n[NZone])
143
        for i := range zone {
144
                var ok bool
145
                var n uint32
146
                if n, ok = zonedata.big4(); !ok {
147
                        return nil, badData
148
                }
149
                zone[i].offset = int(n)
150
                var b byte
151
                if b, ok = zonedata.byte(); !ok {
152
                        return nil, badData
153
                }
154
                zone[i].isDST = b != 0
155
                if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
156
                        return nil, badData
157
                }
158
                zone[i].name = byteString(abbrev[b:])
159
        }
160
 
161
        // Now the transition time info.
162
        tx := make([]zoneTrans, n[NTime])
163
        for i := range tx {
164
                var ok bool
165
                var n uint32
166
                if n, ok = txtimes.big4(); !ok {
167
                        return nil, badData
168
                }
169
                tx[i].when = int64(int32(n))
170
                if int(txzones[i]) >= len(zone) {
171
                        return nil, badData
172
                }
173
                tx[i].index = txzones[i]
174
                if i < len(isstd) {
175
                        tx[i].isstd = isstd[i] != 0
176
                }
177
                if i < len(isutc) {
178
                        tx[i].isutc = isutc[i] != 0
179
                }
180
        }
181
 
182
        // Commited to succeed.
183
        l = &Location{zone: zone, tx: tx}
184
 
185
        // Fill in the cache with information about right now,
186
        // since that will be the most common lookup.
187
        sec, _ := now()
188
        for i := range tx {
189
                if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
190
                        l.cacheStart = tx[i].when
191
                        l.cacheEnd = 1<<63 - 1
192
                        if i+1 < len(tx) {
193
                                l.cacheEnd = tx[i+1].when
194
                        }
195
                        l.cacheZone = &l.zone[tx[i].index]
196
                }
197
        }
198
 
199
        return l, nil
200
}
201
 
202
func loadZoneFile(name string) (l *Location, err error) {
203
        buf, err := readFile(name)
204
        if err != nil {
205
                return
206
        }
207
        return loadZoneData(buf)
208
}
209
 
210
func initTestingZone() {
211
        syscall.Setenv("TZ", "America/Los_Angeles")
212
        initLocal()
213
}
214
 
215
// Many systems use /usr/share/zoneinfo, Solaris 2 has
216
// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
217
var zoneDirs = []string{
218
        "/usr/share/zoneinfo/",
219
        "/usr/share/lib/zoneinfo/",
220
        "/usr/lib/locale/TZ/",
221
}
222
 
223
func initLocal() {
224
        // consult $TZ to find the time zone to use.
225
        // no $TZ means use the system default /etc/localtime.
226
        // $TZ="" means use UTC.
227
        // $TZ="foo" means use /usr/share/zoneinfo/foo.
228
 
229
        tz, ok := syscall.Getenv("TZ")
230
        switch {
231
        case !ok:
232
                z, err := loadZoneFile("/etc/localtime")
233
                if err == nil {
234
                        localLoc = *z
235
                        localLoc.name = "Local"
236
                        return
237
                }
238
        case tz != "" && tz != "UTC":
239
                if z, err := loadLocation(tz); err == nil {
240
                        localLoc = *z
241
                        return
242
                }
243
        }
244
 
245
        // Fall back to UTC.
246
        localLoc.name = "UTC"
247
}
248
 
249
func loadLocation(name string) (*Location, error) {
250
        for _, zoneDir := range zoneDirs {
251
                if z, err := loadZoneFile(zoneDir + name); err == nil {
252
                        z.name = name
253
                        return z, nil
254
                }
255
        }
256
        return nil, errors.New("unknown time zone " + name)
257
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.