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 |
|
|
package time
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"errors"
|
9 |
|
|
"syscall"
|
10 |
|
|
)
|
11 |
|
|
|
12 |
|
|
// TODO(rsc): Fall back to copy of zoneinfo files.
|
13 |
|
|
|
14 |
|
|
// BUG(brainman,rsc): On Windows, the operating system does not provide complete
|
15 |
|
|
// time zone information.
|
16 |
|
|
// The implementation assumes that this year's rules for daylight savings
|
17 |
|
|
// time apply to all previous and future years as well.
|
18 |
|
|
// Also, time zone abbreviations are unavailable. The implementation constructs
|
19 |
|
|
// them using the capital letters from a longer time zone description.
|
20 |
|
|
|
21 |
|
|
// abbrev returns the abbreviation to use for the given zone name.
|
22 |
|
|
func abbrev(name []uint16) string {
|
23 |
|
|
// name is 'Pacific Standard Time' but we want 'PST'.
|
24 |
|
|
// Extract just capital letters. It's not perfect but the
|
25 |
|
|
// information we need is not available from the kernel.
|
26 |
|
|
// Because time zone abbreviations are not unique,
|
27 |
|
|
// Windows refuses to expose them.
|
28 |
|
|
//
|
29 |
|
|
// http://social.msdn.microsoft.com/Forums/eu/vclanguage/thread/a87e1d25-fb71-4fe0-ae9c-a9578c9753eb
|
30 |
|
|
// http://stackoverflow.com/questions/4195948/windows-time-zone-abbreviations-in-asp-net
|
31 |
|
|
var short []rune
|
32 |
|
|
for _, c := range name {
|
33 |
|
|
if 'A' <= c && c <= 'Z' {
|
34 |
|
|
short = append(short, rune(c))
|
35 |
|
|
}
|
36 |
|
|
}
|
37 |
|
|
return string(short)
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
// pseudoUnix returns the pseudo-Unix time (seconds since Jan 1 1970 *LOCAL TIME*)
|
41 |
|
|
// denoted by the system date+time d in the given year.
|
42 |
|
|
// It is up to the caller to convert this local time into a UTC-based time.
|
43 |
|
|
func pseudoUnix(year int, d *syscall.Systemtime) int64 {
|
44 |
|
|
// Windows specifies daylight savings information in "day in month" format:
|
45 |
|
|
// d.Month is month number (1-12)
|
46 |
|
|
// d.DayOfWeek is appropriate weekday (Sunday=0 to Saturday=6)
|
47 |
|
|
// d.Day is week within the month (1 to 5, where 5 is last week of the month)
|
48 |
|
|
// d.Hour, d.Minute and d.Second are absolute time
|
49 |
|
|
day := 1
|
50 |
|
|
t := Date(year, Month(d.Month), day, int(d.Hour), int(d.Minute), int(d.Second), 0, UTC)
|
51 |
|
|
i := int(d.DayOfWeek) - int(t.Weekday())
|
52 |
|
|
if i < 0 {
|
53 |
|
|
i += 7
|
54 |
|
|
}
|
55 |
|
|
day += i
|
56 |
|
|
if week := int(d.Day) - 1; week < 4 {
|
57 |
|
|
day += week * 7
|
58 |
|
|
} else {
|
59 |
|
|
// "Last" instance of the day.
|
60 |
|
|
day += 4 * 7
|
61 |
|
|
if day > daysIn(Month(d.Month), year) {
|
62 |
|
|
day -= 7
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
return t.sec + int64(day-1)*secondsPerDay + internalToUnix
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
func initLocalFromTZI(i *syscall.Timezoneinformation) {
|
69 |
|
|
l := &localLoc
|
70 |
|
|
|
71 |
|
|
nzone := 1
|
72 |
|
|
if i.StandardDate.Month > 0 {
|
73 |
|
|
nzone++
|
74 |
|
|
}
|
75 |
|
|
l.zone = make([]zone, nzone)
|
76 |
|
|
|
77 |
|
|
std := &l.zone[0]
|
78 |
|
|
std.name = abbrev(i.StandardName[0:])
|
79 |
|
|
if nzone == 1 {
|
80 |
|
|
// No daylight savings.
|
81 |
|
|
std.offset = -int(i.Bias) * 60
|
82 |
|
|
l.cacheStart = -1 << 63
|
83 |
|
|
l.cacheEnd = 1<<63 - 1
|
84 |
|
|
l.cacheZone = std
|
85 |
|
|
return
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
// StandardBias must be ignored if StandardDate is not set,
|
89 |
|
|
// so this computation is delayed until after the nzone==1
|
90 |
|
|
// return above.
|
91 |
|
|
std.offset = -int(i.Bias+i.StandardBias) * 60
|
92 |
|
|
|
93 |
|
|
dst := &l.zone[1]
|
94 |
|
|
dst.name = abbrev(i.DaylightName[0:])
|
95 |
|
|
dst.offset = -int(i.Bias+i.DaylightBias) * 60
|
96 |
|
|
dst.isDST = true
|
97 |
|
|
|
98 |
|
|
// Arrange so that d0 is first transition date, d1 second,
|
99 |
|
|
// i0 is index of zone after first transition, i1 second.
|
100 |
|
|
d0 := &i.StandardDate
|
101 |
|
|
d1 := &i.DaylightDate
|
102 |
|
|
i0 := 0
|
103 |
|
|
i1 := 1
|
104 |
|
|
if d0.Month > d1.Month {
|
105 |
|
|
d0, d1 = d1, d0
|
106 |
|
|
i0, i1 = i1, i0
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// 2 tx per year, 100 years on each side of this year
|
110 |
|
|
l.tx = make([]zoneTrans, 400)
|
111 |
|
|
|
112 |
|
|
t := Now().UTC()
|
113 |
|
|
year := t.Year()
|
114 |
|
|
txi := 0
|
115 |
|
|
for y := year - 100; y < year+100; y++ {
|
116 |
|
|
tx := &l.tx[txi]
|
117 |
|
|
tx.when = pseudoUnix(y, d0) - int64(l.zone[i1].offset)
|
118 |
|
|
tx.index = uint8(i0)
|
119 |
|
|
txi++
|
120 |
|
|
|
121 |
|
|
tx = &l.tx[txi]
|
122 |
|
|
tx.when = pseudoUnix(y, d1) - int64(l.zone[i0].offset)
|
123 |
|
|
tx.index = uint8(i1)
|
124 |
|
|
txi++
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
var usPacific = syscall.Timezoneinformation{
|
129 |
|
|
Bias: 8 * 60,
|
130 |
|
|
StandardName: [32]uint16{
|
131 |
|
|
'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'S', 't', 'a', 'n', 'd', 'a', 'r', 'd', ' ', 'T', 'i', 'm', 'e',
|
132 |
|
|
},
|
133 |
|
|
StandardDate: syscall.Systemtime{Month: 11, Day: 1, Hour: 2},
|
134 |
|
|
DaylightName: [32]uint16{
|
135 |
|
|
'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'D', 'a', 'y', 'l', 'i', 'g', 'h', 't', ' ', 'T', 'i', 'm', 'e',
|
136 |
|
|
},
|
137 |
|
|
DaylightDate: syscall.Systemtime{Month: 3, Day: 2, Hour: 2},
|
138 |
|
|
DaylightBias: -60,
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
func initTestingZone() {
|
142 |
|
|
initLocalFromTZI(&usPacific)
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
func initLocal() {
|
146 |
|
|
var i syscall.Timezoneinformation
|
147 |
|
|
if _, err := syscall.GetTimeZoneInformation(&i); err != nil {
|
148 |
|
|
localLoc.name = "UTC"
|
149 |
|
|
return
|
150 |
|
|
}
|
151 |
|
|
initLocalFromTZI(&i)
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
// TODO(rsc): Implement.
|
155 |
|
|
func loadLocation(name string) (*Location, error) {
|
156 |
|
|
return nil, errors.New("unknown time zone " + name)
|
157 |
|
|
}
|