-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathiso8601_test.go
283 lines (252 loc) · 8.71 KB
/
iso8601_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package jsonx
import (
"encoding/json"
"testing"
"time"
)
func TestParseISO8601(t *testing.T) {
tests := []struct {
name string
input string
want ISO8601
wantErr bool
}{
{
name: "Timestamp with microseconds",
input: "2024-01-02T15:04:05.999999Z",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 999999*1000, time.UTC)),
wantErr: false,
},
{
name: "Timestamp with timezone but no microseconds",
input: "2024-01-02T15:04:05+07:00",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.FixedZone("", 7*3600))),
wantErr: false,
},
{
name: "Timestamp with timezone of UTC with microseconds",
input: "2024-04-08T08:05:04.830140+00:00",
// time.Date function interprets the nanosecond parameter. The time.Date function expects the nanosecond parameter to be the entire nanosecond part of the time, not just the microsecond part.
// When we pass 830140 as the nanosecond argument, Go interprets this as 830140 nanoseconds,
// which is equivalent to 000830140 microseconds (padded with leading zeros to fill the nanosecond precision).
// This is why we see 2024-04-08 08:05:04.00083014 +0000 UTC as the output.
// To correctly represent 830140 microseconds, we need to convert it to nanoseconds by multiplying by 1000 (or set the value to 830140000).
want: ISO8601(time.Date(2024, 04, 8, 8, 05, 04, 830140*1000, time.UTC)),
wantErr: false,
},
{
name: "Timestamp with timezone but no microseconds (2)",
input: "2024-04-08T04:47:10+03:00",
want: ISO8601(time.Date(2024, 04, 8, 4, 47, 10, 0, time.FixedZone("", 3*3600))),
wantErr: false,
},
{
name: "Timestamp with Zulu time",
input: "2024-01-02T15:04:05Z",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.UTC)),
wantErr: false,
},
{
name: "Timestamp with Zulu time with microseconds",
input: "2024-04-08T08:05:04.830140",
want: ISO8601(time.Date(2024, 04, 8, 8, 05, 04, 830140*1000, time.UTC)),
wantErr: false,
},
{
name: "Basic ISO8601 layout",
input: "2024-01-02T15:04:05",
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.UTC)),
wantErr: false,
},
{
name: "Invalid format",
input: "2024-01-02",
want: ISO8601{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseISO8601(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseISO8601() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !time.Time(got).Equal(time.Time(tt.want)) {
t.Errorf("ParseISO8601() = %v (%s), want %v (%s)", got, got.ToTime().String(), tt.want, tt.want.ToTime().String())
}
})
}
}
func TestISO8601(t *testing.T) {
data := `{"start": "2021-08-20T10:05:01", "end": "2021-12-01T17:05:06", "nothing": null, "empty": ""}`
v := struct {
Start ISO8601 `json:"start"`
End ISO8601 `json:"end"`
Nothing ISO8601 `json:"nothing"`
Empty ISO8601 `json:"empty"`
}{}
err := json.Unmarshal([]byte(data), &v)
if err != nil {
t.Fatal(err)
}
if !v.Nothing.IsZero() {
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
}
if !v.Empty.IsZero() {
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
}
loc := time.UTC
if expected, got := time.Date(2021, time.August, 20, 10, 5, 1, 0, loc), v.Start.ToTime(); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2021, time.December, 1, 17, 5, 6, 0, loc), v.End.ToTime(); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
}
func TestISO8601WithZoneUTCOffset(t *testing.T) {
data := `{"start": "2022-08-10T03:21:00.000000+03:00", "end": "2022-08-10T09:49:00.000000+03:00", "nothing": null, "empty": ""}`
v := struct {
Start ISO8601 `json:"start"`
End ISO8601 `json:"end"`
Nothing ISO8601 `json:"nothing"`
Empty ISO8601 `json:"empty"`
}{}
err := json.Unmarshal([]byte(data), &v)
if err != nil {
t.Fatalf("unmarshal: %v", err)
}
// t.Logf("Start: %s, location: %s\n", v.Start.String(), v.Start.ToTime().Location().String())
if !v.Nothing.IsZero() {
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
}
if !v.Empty.IsZero() {
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
}
loc := time.FixedZone("EEST", 10800)
if expected, got := time.Date(2022, time.August, 10, 3, 21, 0, 0, loc).String(), v.Start.ToTime().String(); expected != got {
t.Fatalf("expected 'start' string to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2022, time.August, 10, 9, 49, 0, 0, loc).String(), v.End.ToTime().String(); expected != got {
t.Fatalf("expected 'end' string to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2022, time.August, 10, 3, 21, 0, 0, loc), v.Start.ToTime().In(loc); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2022, time.August, 10, 9, 49, 0, 0, loc), v.End.ToTime().In(loc); expected != got {
t.Fatalf("expected 'end' to be: %v but got: %v", expected, got)
}
}
func TestISO8601WithZoneUTCOffsetWithoutMilliseconds(t *testing.T) {
data := `{"start": "2023-02-04T09:48:14+00:00", "end": "2023-02-05T00:03:16+00:00", "nothing": null, "empty": ""}`
v := struct {
Start ISO8601 `json:"start"`
End ISO8601 `json:"end"`
Nothing ISO8601 `json:"nothing"`
Empty ISO8601 `json:"empty"`
}{}
err := json.Unmarshal([]byte(data), &v)
if err != nil {
t.Fatalf("unmarshal: %v", err)
}
// t.Logf("Start: %s, location: %s\n", v.Start.String(), v.Start.ToTime().Location().String())
if !v.Nothing.IsZero() {
t.Fatalf("expected 'nothing' to be zero but got: %v", v.Nothing)
}
if !v.Empty.IsZero() {
t.Fatalf("expected 'empty' to be zero but got: %v", v.Empty)
}
loc := time.FixedZone("UTC", 0)
if expected, got := time.Date(2023, time.February, 04, 9, 48, 14, 0, loc).String(), v.Start.ToTime().String(); expected != got {
t.Fatalf("expected 'start' string to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2023, time.February, 05, 0, 3, 16, 0, loc).String(), v.End.ToTime().String(); expected != got {
t.Fatalf("expected 'end' string to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2023, time.February, 04, 9, 48, 14, 0, loc), v.Start.ToTime().In(loc); expected != got {
t.Fatalf("expected 'start' to be: %v but got: %v", expected, got)
}
if expected, got := time.Date(2023, time.February, 05, 0, 3, 16, 0, loc), v.End.ToTime().In(loc); expected != got {
t.Fatalf("expected 'end' to be: %v but got: %v", expected, got)
}
}
func TestParseISO8601_StandardLayouts(t *testing.T) {
tests := []struct {
input string
expected time.Time
hasError bool
}{
{
input: "2024-05-21T18:06:07Z",
expected: time.Date(2024, 5, 21, 18, 6, 7, 0, time.UTC),
hasError: false,
},
{
input: "2024-05-21T18:06:07-04:00",
expected: time.Date(2024, 5, 21, 22, 6, 7, 0, time.UTC),
hasError: false,
},
{
input: "2024-05-21T18:06:07",
expected: time.Date(2024, 5, 21, 18, 6, 7, 0, time.UTC), // no time local.
hasError: false,
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
parsedTime, err := ParseISO8601(tt.input)
if (err != nil) != tt.hasError {
t.Errorf("ParseISO8601() error = %v, wantErr %v", err, tt.hasError)
return
}
if !tt.hasError && !parsedTime.ToTime().Equal(tt.expected) {
t.Errorf("ParseISO8601() = %v, want %v", parsedTime, tt.expected)
}
})
}
}
func TestParseISO8601_UnconventionalOffset(t *testing.T) {
tests := []struct {
input string
expected time.Time
hasError bool
}{
{
input: "2024-05-21T18:06:07.000000-04:01:19",
expected: time.Date(2024, 5, 21, 22, 7, 26, 0, time.UTC),
hasError: false,
},
{
input: "2024-12-31T23:59:59.000000-00:00:59",
expected: time.Date(2025, 1, 1, 0, 0, 58, 0, time.UTC),
hasError: false,
},
{
input: "2024-05-21T18:06:07.000000+03:30:15",
expected: time.Date(2024, 5, 21, 14, 35, 52, 0, time.UTC),
hasError: false,
},
{
input: "2024-05-21T18:06:07.000000-24:00:00",
expected: time.Date(2024, 5, 22, 18, 6, 7, 0, time.UTC),
hasError: false,
},
{
input: "2024-05-21T18:06:07.000000-04:61:19", // Invalid minute part in offset
expected: time.Time{},
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
parsedTime, err := ParseISO8601(tt.input)
if (err != nil) != tt.hasError {
t.Errorf("ParseISO8601() error = %v, wantErr %v", err, tt.hasError)
return
}
if !tt.hasError && !parsedTime.ToTime().Equal(tt.expected) {
t.Errorf("ParseISO8601() = %v, want %v", parsedTime, tt.expected)
}
})
}
}