-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
129 lines (118 loc) · 3.83 KB
/
time_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
package lexy_test
import (
"testing"
"time"
"github.com/phiryll/lexy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func timeTestCases() []testCase[time.Time] {
// West of UTC, negative timezone offset
locNYC, err := time.LoadLocation("America/New_York")
if err != nil {
panic(err)
}
// East of UTC, positive timezone offset
locBerlin, err := time.LoadLocation("Europe/Berlin")
if err != nil {
panic(err)
}
var zero time.Time
// Before the epoch start on Jan 1, 1970
past := time.Date(1900, 1, 2, 3, 4, 5, 600_000_000, time.UTC)
local := time.Date(2000, 1, 2, 3, 4, 5, 6, time.Local)
utc := time.Date(2000, 1, 2, 3, 4, 5, 600_000_000, time.UTC)
nyc := time.Date(2000, 1, 2, 3, 4, 5, 999_999_999, locNYC)
berlin := time.Date(2000, 1, 2, 3, 4, 5, 999_999_999, locBerlin)
noZoneName, err := time.Parse(time.RFC3339Nano, "2000-01-02T03:04:05.6-05:00")
if err != nil {
panic(err)
}
return []testCase[time.Time]{
{"zero", zero, nil},
{"past", past, nil},
{"utc", utc, nil},
{"local", local, nil},
{"nyc", nyc, nil},
{"berlin", berlin, nil},
{"no zone name", noZoneName, nil},
}
}
func TestTimeWithZoneNames(t *testing.T) {
t.Parallel()
codec := lexy.Time()
assert.False(t, codec.RequiresTerminator())
for _, tt := range timeTestCases() {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
when := tt.value
_, expectedOffset := when.Zone()
buf := codec.Append(nil, when)
got, _ := codec.Get(buf)
_, actualOffset := got.Zone()
// Can't use == because it doesn't work for time.Time
assert.True(t, when.Equal(got), "round trip")
assert.Equal(t, expectedOffset, actualOffset, "offsets")
})
}
}
func TestTimeStripZoneName(t *testing.T) {
t.Parallel()
codec := lexy.Time()
var testCases []testCase[time.Time]
for _, tt := range timeTestCases() {
_, offset := tt.value.Zone()
testCases = append(testCases, testCase[time.Time]{
tt.name,
tt.value.In(time.FixedZone("", offset)),
tt.data,
})
}
testCodec(t, codec, fillTestData(codec, testCases))
}
func TestTimeOrdering(t *testing.T) {
t.Parallel()
// in order from west to east, expected sort order,
// UTC is between NYC and Berlin.
locFixed := time.FixedZone("fixed", -12*3600)
locLA, err := time.LoadLocation("America/Los_Angeles")
require.NoError(t, err)
locNYC, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
locBerlin, err := time.LoadLocation("Europe/Berlin")
require.NoError(t, err)
// UTC times in order, pos/neg relative to epoch and 6/7 nanoseconds
// test each of these in multiple timezones
negUTC6 := time.Date(1900, 1, 2, 3, 4, 5, 6, time.UTC)
negUTC7 := time.Date(1900, 1, 2, 3, 4, 5, 7, time.UTC)
posUTC6 := time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
posUTC7 := time.Date(2000, 1, 2, 3, 4, 5, 7, time.UTC)
testOrdering(t, lexy.Time(), []testCase[time.Time]{
// Encodings should sort in this order.
//
// before and after the epoch start (Jan 1, 1970)
// with different nanoseconds within the same second
// with different timezones
{"neg fixed 6", negUTC6.In(locFixed), nil},
{"neg LA 6", negUTC6.In(locLA), nil},
{"neg NYC 6", negUTC6.In(locNYC), nil},
{"neg UTC 6", negUTC6, nil},
{"neg Berlin 6", negUTC6.In(locBerlin), nil},
{"neg fixed 7", negUTC7.In(locFixed), nil},
{"neg LA 7", negUTC7.In(locLA), nil},
{"neg NYC 7", negUTC7.In(locNYC), nil},
{"neg UTC 7", negUTC7, nil},
{"neg Berlin 7", negUTC7.In(locBerlin), nil},
{"pos fixed 6", posUTC6.In(locFixed), nil},
{"pos LA 6", posUTC6.In(locLA), nil},
{"pos NYC 6", posUTC6.In(locNYC), nil},
{"pos UTC 6", posUTC6, nil},
{"pos Berlin 6", posUTC6.In(locBerlin), nil},
{"pos fixed 7", posUTC7.In(locFixed), nil},
{"pos LA 7", posUTC7.In(locLA), nil},
{"pos NYC 7", posUTC7.In(locNYC), nil},
{"pos UTC 7", posUTC7, nil},
{"pos Berlin 7", posUTC7.In(locBerlin), nil},
})
}