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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [time/] [zoneinfo_plan9.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 2011 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 Plan 9 timezone(2) files.
6
 
7
package time
8
 
9
import (
10
        "errors"
11
        "syscall"
12
)
13
 
14
var badData = errors.New("malformed time zone information")
15
 
16
func isSpace(r rune) bool {
17
        return r == ' ' || r == '\t' || r == '\n'
18
}
19
 
20
// Copied from strings to avoid a dependency.
21
func fields(s string) []string {
22
        // First count the fields.
23
        n := 0
24
        inField := false
25
        for _, rune := range s {
26
                wasInField := inField
27
                inField = !isSpace(rune)
28
                if inField && !wasInField {
29
                        n++
30
                }
31
        }
32
 
33
        // Now create them.
34
        a := make([]string, n)
35
        na := 0
36
        fieldStart := -1 // Set to -1 when looking for start of field.
37
        for i, rune := range s {
38
                if isSpace(rune) {
39
                        if fieldStart >= 0 {
40
                                a[na] = s[fieldStart:i]
41
                                na++
42
                                fieldStart = -1
43
                        }
44
                } else if fieldStart == -1 {
45
                        fieldStart = i
46
                }
47
        }
48
        if fieldStart >= 0 { // Last field might end at EOF.
49
                a[na] = s[fieldStart:]
50
        }
51
        return a
52
}
53
 
54
func loadZoneData(s string) (l *Location, err error) {
55
        f := fields(s)
56
        if len(f) < 4 {
57
                if len(f) == 2 && f[0] == "GMT" {
58
                        return UTC, nil
59
                }
60
                return nil, badData
61
        }
62
 
63
        var zones [2]zone
64
 
65
        // standard timezone offset
66
        o, err := atoi(f[1])
67
        if err != nil {
68
                return nil, badData
69
        }
70
        zones[0] = zone{name: f[0], offset: o, isDST: false}
71
 
72
        // alternate timezone offset
73
        o, err = atoi(f[3])
74
        if err != nil {
75
                return nil, badData
76
        }
77
        zones[1] = zone{name: f[2], offset: o, isDST: true}
78
 
79
        // transition time pairs
80
        var tx []zoneTrans
81
        f = f[4:]
82
        for i := 0; i < len(f); i++ {
83
                zi := 0
84
                if i%2 == 0 {
85
                        zi = 1
86
                }
87
                t, err := atoi(f[i])
88
                if err != nil {
89
                        return nil, badData
90
                }
91
                t -= zones[0].offset
92
                tx = append(tx, zoneTrans{when: int64(t), index: uint8(zi)})
93
        }
94
 
95
        // Committed to succeed.
96
        l = &Location{zone: zones[:], tx: tx}
97
 
98
        // Fill in the cache with information about right now,
99
        // since that will be the most common lookup.
100
        sec, _ := now()
101
        for i := range tx {
102
                if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
103
                        l.cacheStart = tx[i].when
104
                        l.cacheEnd = 1<<63 - 1
105
                        if i+1 < len(tx) {
106
                                l.cacheEnd = tx[i+1].when
107
                        }
108
                        l.cacheZone = &l.zone[tx[i].index]
109
                }
110
        }
111
 
112
        return l, nil
113
}
114
 
115
func loadZoneFile(name string) (*Location, error) {
116
        b, err := readFile(name)
117
        if err != nil {
118
                return nil, err
119
        }
120
        return loadZoneData(string(b))
121
}
122
 
123
func initTestingZone() {
124
        if z, err := loadZoneFile("/adm/timezone/US_Pacific"); err == nil {
125
                localLoc = *z
126
                return
127
        }
128
 
129
        // Fall back to UTC.
130
        localLoc.name = "UTC"
131
}
132
 
133
func initLocal() {
134
        t, ok := syscall.Getenv("timezone")
135
        if ok {
136
                if z, err := loadZoneData(t); err == nil {
137
                        localLoc = *z
138
                        return
139
                }
140
        } else {
141
                if z, err := loadZoneFile("/adm/timezone/local"); err == nil {
142
                        localLoc = *z
143
                        localLoc.name = "Local"
144
                        return
145
                }
146
        }
147
 
148
        // Fall back to UTC.
149
        localLoc.name = "UTC"
150
}
151
 
152
func loadLocation(name string) (*Location, error) {
153
        if z, err := loadZoneFile("/adm/timezone/" + name); err == nil {
154
                return z, nil
155
        }
156
        return nil, errors.New("unknown time zone " + name)
157
}

powered by: WebSVN 2.1.0

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