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

Subversion Repositories openrisc

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

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
package time
6
 
7
import "sync"
8
 
9
// A Location maps time instants to the zone in use at that time.
10
// Typically, the Location represents the collection of time offsets
11
// in use in a geographical area, such as CEST and CET for central Europe.
12
type Location struct {
13
        name string
14
        zone []zone
15
        tx   []zoneTrans
16
 
17
        // Most lookups will be for the current time.
18
        // To avoid the binary search through tx, keep a
19
        // static one-element cache that gives the correct
20
        // zone for the time when the Location was created.
21
        // if cacheStart <= t <= cacheEnd,
22
        // lookup can return cacheZone.
23
        // The units for cacheStart and cacheEnd are seconds
24
        // since January 1, 1970 UTC, to match the argument
25
        // to lookup.
26
        cacheStart int64
27
        cacheEnd   int64
28
        cacheZone  *zone
29
}
30
 
31
// A zone represents a single time zone such as CEST or CET.
32
type zone struct {
33
        name   string // abbreviated name, "CET"
34
        offset int    // seconds east of UTC
35
        isDST  bool   // is this zone Daylight Savings Time?
36
}
37
 
38
// A zoneTrans represents a single time zone transition.
39
type zoneTrans struct {
40
        when         int64 // transition time, in seconds since 1970 GMT
41
        index        uint8 // the index of the zone that goes into effect at that time
42
        isstd, isutc bool  // ignored - no idea what these mean
43
}
44
 
45
// UTC represents Universal Coordinated Time (UTC).
46
var UTC *Location = &utcLoc
47
 
48
// utcLoc is separate so that get can refer to &utcLoc
49
// and ensure that it never returns a nil *Location,
50
// even if a badly behaved client has changed UTC.
51
var utcLoc = Location{name: "UTC"}
52
 
53
// Local represents the system's local time zone.
54
var Local *Location = &localLoc
55
 
56
// localLoc is separate so that initLocal can initialize
57
// it even if a client has changed Local.
58
var localLoc Location
59
var localOnce sync.Once
60
 
61
func (l *Location) get() *Location {
62
        if l == nil {
63
                return &utcLoc
64
        }
65
        if l == &localLoc {
66
                localOnce.Do(initLocal)
67
        }
68
        return l
69
}
70
 
71
// String returns a descriptive name for the time zone information,
72
// corresponding to the argument to LoadLocation.
73
func (l *Location) String() string {
74
        return l.get().name
75
}
76
 
77
// FixedZone returns a Location that always uses
78
// the given zone name and offset (seconds east of UTC).
79
func FixedZone(name string, offset int) *Location {
80
        l := &Location{
81
                name:       name,
82
                zone:       []zone{{name, offset, false}},
83
                tx:         []zoneTrans{{-1 << 63, 0, false, false}},
84
                cacheStart: -1 << 63,
85
                cacheEnd:   1<<63 - 1,
86
        }
87
        l.cacheZone = &l.zone[0]
88
        return l
89
}
90
 
91
// lookup returns information about the time zone in use at an
92
// instant in time expressed as seconds since January 1, 1970 00:00:00 UTC.
93
//
94
// The returned information gives the name of the zone (such as "CET"),
95
// the start and end times bracketing sec when that zone is in effect,
96
// the offset in seconds east of UTC (such as -5*60*60), and whether
97
// the daylight savings is being observed at that time.
98
func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start, end int64) {
99
        l = l.get()
100
 
101
        if len(l.tx) == 0 {
102
                name = "UTC"
103
                offset = 0
104
                isDST = false
105
                start = -1 << 63
106
                end = 1<<63 - 1
107
                return
108
        }
109
 
110
        if zone := l.cacheZone; zone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
111
                name = zone.name
112
                offset = zone.offset
113
                isDST = zone.isDST
114
                start = l.cacheStart
115
                end = l.cacheEnd
116
                return
117
        }
118
 
119
        // Binary search for entry with largest time <= sec.
120
        // Not using sort.Search to avoid dependencies.
121
        tx := l.tx
122
        end = 1<<63 - 1
123
        for len(tx) > 1 {
124
                m := len(tx) / 2
125
                lim := tx[m].when
126
                if sec < lim {
127
                        end = lim
128
                        tx = tx[0:m]
129
                } else {
130
                        tx = tx[m:]
131
                }
132
        }
133
        zone := &l.zone[tx[0].index]
134
        name = zone.name
135
        offset = zone.offset
136
        isDST = zone.isDST
137
        start = tx[0].when
138
        // end = maintained during the search
139
        return
140
}
141
 
142
// lookupName returns information about the time zone with
143
// the given name (such as "EST").
144
func (l *Location) lookupName(name string) (offset int, isDST bool, ok bool) {
145
        l = l.get()
146
        for i := range l.zone {
147
                zone := &l.zone[i]
148
                if zone.name == name {
149
                        return zone.offset, zone.isDST, true
150
                }
151
        }
152
        return
153
}
154
 
155
// lookupOffset returns information about the time zone with
156
// the given offset (such as -5*60*60).
157
func (l *Location) lookupOffset(offset int) (name string, isDST bool, ok bool) {
158
        l = l.get()
159
        for i := range l.zone {
160
                zone := &l.zone[i]
161
                if zone.offset == offset {
162
                        return zone.name, zone.isDST, true
163
                }
164
        }
165
        return
166
}
167
 
168
// NOTE(rsc): Eventually we will need to accept the POSIX TZ environment
169
// syntax too, but I don't feel like implementing it today.
170
 
171
// NOTE(rsc): Using the IANA names below means ensuring we have access
172
// to the database.  Probably we will ship the files in $GOROOT/lib/zoneinfo/
173
// and only look there if there are no system files available (such as on Windows).
174
// The files total 200 kB.
175
 
176
// LoadLocation returns the Location with the given name.
177
//
178
// If the name is "" or "UTC", LoadLocation returns UTC.
179
// If the name is "Local", LoadLocation returns Local.
180
//
181
// Otherwise, the name is taken to be a location name corresponding to a file
182
// in the IANA Time Zone database, such as "America/New_York".
183
func LoadLocation(name string) (*Location, error) {
184
        if name == "" || name == "UTC" {
185
                return UTC, nil
186
        }
187
        if name == "Local" {
188
                return Local, nil
189
        }
190
        return loadLocation(name)
191
}

powered by: WebSVN 2.1.0

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