-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisdst_test.go
172 lines (151 loc) · 5.07 KB
/
isdst_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package isdst
import (
"testing"
"time"
)
// parsedTime is the struct representing a parsed time value.
type testCase struct {
Unix int64
Year int
Month time.Month
Day int
Hour, Minute, Second int // 15:04:05 is 15, 4, 5.
Nanosecond int // Fractional second.
Location string
IsDST bool // flag a DST
}
var utctests = []testCase{
{0, 1970, time.January, 1, 0, 0, 0, 0, "", false},
{1221681866, 2008, time.September, 17, 20, 4, 26, 0, "", false},
{-1221681866, 1931, time.April, 16, 3, 55, 34, 0, "", false},
{-11644473600, 1601, time.January, 1, 0, 0, 0, 0, "", false},
{599529660, 1988, time.December, 31, 0, 1, 0, 0, "", false},
{978220860, 2000, time.December, 31, 0, 1, 0, 0, "", false},
}
var nanoutctests = []testCase{
{0, 1970, time.January, 1, 0, 0, 0, 1e8, "", false},
{1221681866, 2008, time.September, 17, 20, 4, 26, 2e8, "", false},
}
var localtests = []testCase{
{0, 1969, time.December, 31, 16, 0, 0, 0, "America/Los_Angeles", false},
{1221681866, 2008, time.September, 17, 13, 4, 26, 0, "America/Los_Angeles", true},
{1969898400, 2032, time.June, 3, 11, 0, 0, 0, "America/Los_Angeles", true},
{1962871199, 2032, time.March, 14, 1, 59, 59, 0, "America/Los_Angeles", false},
{1962871200, 2032, time.March, 14, 3, 0, 0, 0, "America/Los_Angeles", true},
{1962871201, 2032, time.March, 14, 3, 0, 1, 0, "America/Los_Angeles", true},
{1983430799, 2032, time.November, 7, 1, 59, 59, 0, "America/Los_Angeles", true},
{1983430800, 2032, time.November, 7, 1, 0, 0, 0, "America/Los_Angeles", false},
{1983430801, 2032, time.November, 7, 1, 0, 1, 0, "America/Los_Angeles", false},
{1901721599, 2030, time.April, 7, 2, 59, 59, 0, "Australia/Sydney", true},
{1901721600, 2030, time.April, 7, 2, 0, 0, 0, "Australia/Sydney", false},
{1901721601, 2030, time.April, 7, 2, 0, 1, 0, "Australia/Sydney", false},
{1917446399, 2030, time.October, 6, 1, 59, 59, 0, "Australia/Sydney", false},
{1917446400, 2030, time.October, 6, 3, 0, 0, 0, "Australia/Sydney", true},
{1917446401, 2030, time.October, 6, 3, 0, 1, 0, "Australia/Sydney", true},
}
var nanolocaltests = []testCase{
{0, 1969, time.December, 31, 16, 0, 0, 1e8, "America/Los_Angeles", false},
{1221681866, 2008, time.September, 17, 13, 4, 26, 3e8, "America/Los_Angeles", true},
}
func same(t time.Time, tc testCase) bool {
// Check aggregates.
year, month, day := t.Date()
hour, min, sec := t.Clock()
if year != tc.Year || month != tc.Month || day != tc.Day ||
hour != tc.Hour || min != tc.Minute || sec != tc.Second {
return false
}
return true
}
func TestIsDST_SecondsUTC(t *testing.T) {
for _, tc := range utctests {
loc, err := time.LoadLocation(tc.Location)
if err != nil {
t.Errorf("Cannot load timezone '%s'", tc.Location)
continue
}
ts := time.Unix(tc.Unix, 0).In(loc)
if !same(ts, tc) {
t.Errorf("same(%d):", ts.Unix())
t.Errorf(" want=%+v", tc)
t.Errorf(" have=%v", ts.Format(time.RFC3339))
}
isDST := IsDST(ts)
if isDST != tc.IsDST {
t.Errorf("IsDST(%d): // %s", ts.Unix(), ts.Format(time.RFC3339))
t.Errorf(" want=%t", tc.IsDST)
t.Errorf(" have=%t", isDST)
} else {
t.Logf("IsDST(%+v)=%t", ts, isDST)
}
}
}
func TestIsDST_NanosecondsUTC(t *testing.T) {
for _, tc := range nanoutctests {
loc, err := time.LoadLocation(tc.Location)
if err != nil {
t.Errorf("Cannot load timezone '%s'", tc.Location)
continue
}
ts := time.Unix(tc.Unix, 0).In(loc)
if !same(ts, tc) {
t.Errorf("same(%d):", ts.Unix())
t.Errorf(" want=%+v", tc)
t.Errorf(" have=%v", ts.Format(time.RFC3339))
}
isDST := IsDST(ts)
if isDST != tc.IsDST {
t.Errorf("IsDST(%d): // %s", ts.Unix(), ts.Format(time.RFC3339))
t.Errorf(" want=%t", tc.IsDST)
t.Errorf(" have=%t", isDST)
} else {
t.Logf("IsDST(%+v)=%t", ts, isDST)
}
}
}
func TestIsDST_SecondsLocal(t *testing.T) {
for _, tc := range localtests {
loc, err := time.LoadLocation(tc.Location)
if err != nil {
t.Errorf("Cannot load timezone '%s'", tc.Location)
continue
}
ts := time.Unix(tc.Unix, 0).In(loc)
if !same(ts, tc) {
t.Errorf("same(%d):", ts.Unix())
t.Errorf(" want=%+v", tc)
t.Errorf(" have=%v", ts.Format(time.RFC3339))
}
isDST := IsDST(ts)
if isDST != tc.IsDST {
t.Errorf("IsDST(%d): // %s", ts.Unix(), ts.Format(time.RFC3339))
t.Errorf(" want=%t", tc.IsDST)
t.Errorf(" have=%t", isDST)
} else {
t.Logf("IsDST(%+v)=%t", ts, isDST)
}
}
}
func TestIsDST_NanosecondsLocal(t *testing.T) {
for _, tc := range nanolocaltests {
loc, err := time.LoadLocation(tc.Location)
if err != nil {
t.Errorf("Cannot load timezone '%s'", tc.Location)
continue
}
ts := time.Unix(tc.Unix, 0).In(loc)
if !same(ts, tc) {
t.Errorf("same(%d):", ts.Unix())
t.Errorf(" want=%+v", tc)
t.Errorf(" have=%v", ts.Format(time.RFC3339))
}
isDST := IsDST(ts)
if isDST != tc.IsDST {
t.Errorf("IsDST(%d): // %s", ts.Unix(), ts.Format(time.RFC3339))
t.Errorf(" want=%t", tc.IsDST)
t.Errorf(" have=%t", isDST)
} else {
t.Logf("IsDST(%+v)=%t", ts, isDST)
}
}
}