-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
334 lines (307 loc) · 11.4 KB
/
test.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"use strict";
var TZ = require("./");
function testUtcToTz(t) {
var result = TZ.toTimeZone.apply(TZ, t.inputs).toISOString();
var result2 = TZ.toTimeZoneISOString.apply(TZ, t.inputs);
if (result !== result2 || t.result !== result) {
throw new Error(
`Invalid UTC/ISO+Offset to TZ conversion for ${t.desc}:\n` +
`\tExpected: ${t.result}\n` +
`\tActual: ${result}\n`
);
}
}
function testTzToUtc(t) {
var result = TZ.fromTimeZone.apply(TZ, t.inputs).toISOString();
var result2 = TZ.toOffsetISOString.apply(TZ, t.inputs);
var result3 = TZ.toUTC.apply(TZ, t.inputs).toISOString();
if (t.result !== result || t.result !== result2 || t.result !== result3) {
console.error(result);
throw new Error(
`Invalid TZ to UTC conversion for ${t.desc}:\n` +
`\tExpected: ${t.result}\n` +
`\tActual: ${result.toISOString()}\n`
);
}
}
// At this real UTC time, what does the timezone translate it to?
[
//
// Start-of-DST Tests
//
// [Start]
// What time is '2021-03-14 01:15:59.000 in New York' in UTC? // 2021-03-14 06:15:59.000
// // 2021-03-14T01:15:59.000-0500
// What time is '2021-03-14 02:15:59.000 in New York' in UTC? // 2021-03-14 07:15:59.000
// // 2021-03-14T03:15:59.000-0400
// What time is '2021-03-14 03:15:59.000 in New York' in UTC? // 2021-03-14 07:15:59.000
// // 2021-03-14T03:15:59.000-0400
// What time is '2021-03-14 04:15:59.000 in New York' in UTC? // 2021-03-14 08:15:59.000
// // 2021-03-14T04:15:59.000-0400
// [End]
// 12:15am NY -0500 => -0400
{
desc: "UTC Zulu to 12:15am NY EST",
inputs: ["2021-03-14T05:15:59.000Z", "America/New_York"],
result: "2021-03-14T00:15:59.000-0500",
},
{
desc: "ISO+Offset to 12:15am NY EST (2)",
inputs: ["2021-03-14T00:15:59.000-0500", "America/New_York"],
result: "2021-03-14T00:15:59.000-0500",
},
// 1:15am NY (non-DST)
{
desc: "UTC Zulu to 1:15am NY EST",
inputs: ["2021-03-14T06:15:59.000Z", "America/New_York"],
result: "2021-03-14T01:15:59.000-0500",
},
{
desc: "ISO+Offset to 1:15am NY EST (2)",
inputs: ["2021-03-14T01:15:59.000-0500", "America/New_York"],
result: "2021-03-14T01:15:59.000-0500",
},
// NOTE: Can't 2:15am NY, because it does not exist (skipped by DST)
// 3:15am NY (DST)
{
desc: "UTC Zulu to 3:15am NY EDT",
inputs: ["2021-03-14T07:15:59.000Z", "America/New_York"],
result: "2021-03-14T03:15:59.000-0400",
},
{
desc: "ISO+Offset to 3:15am NY EDT (2)",
inputs: ["2021-03-14T03:15:59.000-0400", "America/New_York"],
result: "2021-03-14T03:15:59.000-0400",
},
// 4:15am NY
{
desc: "UTC Zulu to 4:15am NY EDT",
inputs: ["2021-03-14T08:15:59.000Z", "America/New_York"],
result: "2021-03-14T04:15:59.000-0400",
},
{
desc: "ISO+Offset to 4:15am NY EDT (2)",
inputs: ["2021-03-14T04:15:59.000-0400", "America/New_York"],
result: "2021-03-14T04:15:59.000-0400",
},
//
// End-of-DST Tests
//
// [Start]
// What time is '2021-11-07 01:15:59.000 in New York' in UTC? // 2021-11-07 05:15:59.000
// // 2021-11-07T01:15:59.000-0400
// // 2021-11-07 06:15:59.000
// // 2021-11-07T01:15:59.000-0500
// What time is '2021-11-07 02:15:59.000 in New York' in UTC? // 2021-11-07 07:15:59.000
// // 2021-11-07T02:15:59.000-0500
// What time is '2021-11-07 03:15:59.000 in New York' in UTC? // 2021-11-07 08:15:59.000
// [End]
// 12:15am NY -0400 => -0500
{
desc: "UTC Zulu to 2021 Nov 7, 12:15am NY EDT",
inputs: ["2021-11-07T04:15:59.000Z", "America/New_York"],
result: "2021-11-07T00:15:59.000-0400",
},
{
desc: "ISO+Offset to 2021 Nov 7, 12:15am NY EDT (2)",
inputs: ["2021-11-07T00:15:59.000-0400", "America/New_York"],
result: "2021-11-07T00:15:59.000-0400",
},
// 1:15am NY (DST) -0400
// NOTE: 1:15am happens TWICE (with different offsets)
{
desc: "UTC Zulu to 2021 Nov 7, 1:15am NY EDT",
inputs: ["2021-11-07T05:15:59.000Z", "America/New_York"],
result: "2021-11-07T01:15:59.000-0400",
},
{
desc: "ISO+Offset to 2021 Nov 7, 1:15am NY EDT (2)",
inputs: ["2021-11-07T01:15:59.000-0400", "America/New_York"],
result: "2021-11-07T01:15:59.000-0400",
},
// 1:15am NY (non-DST) -0500
{
desc: "UTC Zulu to 2021 Nov 7, 1:15am NY EST",
inputs: ["2021-11-07T06:15:59.000Z", "America/New_York"],
result: "2021-11-07T01:15:59.000-0500",
},
{
desc: "ISO+Offset to 2021 Nov 7, 1:15am NY EST (2)",
inputs: ["2021-11-07T01:15:59.000-0500", "America/New_York"],
result: "2021-11-07T01:15:59.000-0500",
},
// 2:15am NY -0500
{
desc: "UTC Zulu to 2021 Nov 7, 2:15am NY EST",
inputs: ["2021-11-07T07:15:59.000Z", "America/New_York"],
result: "2021-11-07T02:15:59.000-0500",
},
{
desc: "ISO+Offset to 2021 Nov 7, 2:15am NY EST (2)",
inputs: ["2021-11-07T02:15:59.000-0500", "America/New_York"],
result: "2021-11-07T02:15:59.000-0500",
},
// 3:15am NY
{
desc: "UTC Zulu to 2021 Nov 7, 3:15am NY EST",
inputs: ["2021-11-07T08:15:59.000Z", "America/New_York"],
result: "2021-11-07T03:15:59.000-0500",
},
{
desc: "ISO+Offset to 2021 Nov 7, 3:15am NY EST (2)",
inputs: ["2021-11-07T03:15:59.000-0500", "America/New_York"],
result: "2021-11-07T03:15:59.000-0500",
},
//
// Positive Offset Test
//
// Colombo +0530 (not DST)
{
desc: "UTC Zulu to Asia/Colombo (1)",
inputs: ["2021-03-14T08:15:59.000Z", "Asia/Colombo"],
result: "2021-03-14T13:45:59.000+0530",
},
{
desc: "ISO+Offset to Asia/Colombo (2)",
inputs: ["2021-03-14T13:45:59.000+0530", "Asia/Colombo"],
result: "2021-03-14T13:45:59.000+0530",
},
{
desc: "UTC Zulu to Asia/Colombo (3)",
inputs: ["2021-11-07T08:15:59.000Z", "Asia/Colombo"],
result: "2021-11-07T13:45:59.000+0530",
},
{
desc: "ISO+Offset to Asia/Colombo (4)",
inputs: ["2021-11-07T13:45:59.000+0530", "Asia/Colombo"],
result: "2021-11-07T13:45:59.000+0530",
},
].forEach(testUtcToTz);
console.info(
"Pass: UTC/ISO+Offset to TZ for America/New_York and Asia/Colombo"
);
[
//
// Start-of-DST Tests
//
// [Start]
// What time is '2021-03-14 01:15:59.000 in New York' in UTC? // 2021-03-14 06:15:59.000
// // 2021-03-14T01:15:59.000-0500
// What time is '2021-03-14 02:15:59.000 in New York' in UTC? // 2021-03-14 07:15:59.000
// // 2021-03-14T03:15:59.000-0400
// What time is '2021-03-14 03:15:59.000 in New York' in UTC? // 2021-03-14 07:15:59.000
// // 2021-03-14T03:15:59.000-0400
// What time is '2021-03-14 04:15:59.000 in New York' in UTC? // 2021-03-14 08:15:59.000
// // 2021-03-14T04:15:59.000-0400
// [End]
// 12:15am NY -0500 => -0400
{
// 2021-03-14T05:15:59.000Z
desc: "2021 Mar 14, 12:15am NY EST to UTC",
inputs: ["2021-03-14 00:15:59.000", "America/New_York"],
result: "2021-03-14T00:15:59.000-0500",
},
// 1:15am NY (non-DST)
{
// 2021-03-14T06:15:59.000Z
desc: "2021 Mar 14, 1:15am NY EST to UTC",
inputs: ["2021-03-14 01:15:59.000", "America/New_York"],
result: "2021-03-14T01:15:59.000-0500",
},
// NOTE: Can't 2:15am NY, because it does not exist (skipped by DST)
// This test is here to document the "undefined" behavior
{
// Both 2021-03-14T06:15:59.000Z and 2021-03-14T07:15:59.000Z
// would be reasonable substitutions, I think
desc: "2021 Mar 14, 2:15am NY ExT to UTC",
inputs: ["2021-03-14 02:15:59.000", "America/New_York"],
//result: "2021-03-14T01:15:59.000-0500", // 2021-03-14T06:15:59.000Z
result: "2021-03-14T02:15:59.000-0400", // 2021-03-14T06:15:59.000Z
//result: "2021-03-14T02:15:59.000-0500", // 2021-03-14T07:15:59.000Z
},
// 3:15am NY (DST)
{
// 2021-03-14T07:15:59.000Z
desc: "2021 Mar 14, 3:15am NY EDT to UTC",
inputs: ["2021-03-14 03:15:59.000", "America/New_York"],
result: "2021-03-14T03:15:59.000-0400",
},
// 4:15am NY
{
// 2021-03-14T08:15:59.000Z
desc: "2021 Mar 14, 4:15am NY EDT to UTC",
inputs: ["2021-03-14 04:15:59.000", "America/New_York"],
result: "2021-03-14T04:15:59.000-0400",
},
//
// End-of-DST Tests
//
// [Start]
// What time is '2021-11-07 01:15:59.000 in New York' in UTC? // 2021-11-07 05:15:59.000
// // 2021-11-07T01:15:59.000-0400
// // 2021-11-07 06:15:59.000
// // 2021-11-07T01:15:59.000-0500
// What time is '2021-11-07 02:15:59.000 in New York' in UTC? // 2021-11-07 07:15:59.000
// // 2021-11-07T02:15:59.000-0500
// What time is '2021-11-07 03:15:59.000 in New York' in UTC? // 2021-11-07 08:15:59.000
// [End]
// 12:15am NY -0400 => -0500
{
// 2021-11-07T04:15:59.000Z
desc: "2021 Nov 7, 12:15am NY EDT to UTC",
inputs: ["2021-11-07 00:15:59.000", "America/New_York"],
result: "2021-11-07T00:15:59.000-0400",
},
// 1:15am NY (DST) -0400
// NOTE: 1:15am happens TWICE (with different offsets), so we skip one
{
// ==> 2021-11-07T05:15:59.000Z
// [Skip] 2021-11-07T06:15:59.000Z
desc: "2021 Nov 7, 1:15am NY ExT to UTC",
inputs: ["2021-11-07 01:15:59.000", "America/New_York"],
result: "2021-11-07T01:15:59.000-0400", // 2021-11-07T05:15:59.000Z
//result: "2021-11-07T01:15:59.000-0500", // 2021-11-07T06:15:59.000Z
},
// 2:15am NY -0500
{
// 2021-11-07T07:15:59.000Z
desc: "2021 Nov 7, 2:15am NY EST to UTC",
inputs: ["2021-11-07 02:15:59.000", "America/New_York"],
result: "2021-11-07T02:15:59.000-0500",
},
// 3:15am NY
{
// 2021-11-07T08:15:59.000Z
desc: "2021 Nov 7, 3:15am NY EST to UTC",
inputs: ["2021-11-07 03:15:59.000", "America/New_York"],
result: "2021-11-07T03:15:59.000-0500",
},
//
// Positive Offset Test
//
// Colombo +0530 (not DST)
{
// 2021-03-14T08:15:59.000Z
desc: "Asia/Colombo to UTC (1)",
inputs: ["2021-03-14 13:45:59.000", "Asia/Colombo"],
result: "2021-03-14T13:45:59.000+0530",
},
{
// 2021-03-14T08:15:59.000Z
desc: "Asia/Colombo to UTC (2)",
inputs: ["2021-11-07 13:45:59.000", "Asia/Colombo"],
result: "2021-11-07T13:45:59.000+0530",
},
].forEach(testTzToUtc);
console.info("Pass: TZ to UTC for America/New_York and Asia/Colombo");
var localISOString = TZ.toLocalISOString();
var reISOString = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d$/;
if (!reISOString.test(localISOString)) {
throw new Error("Couldn't get local time as iso+offset");
}
console.info("Pass: can get local time as ISO+Offset");
var tzName = TZ.timeZone();
if (!/^[A-Z]\w+\/[A-Z]\w+$/.test(tzName)) {
throw new Error("Couldn't get local Time Zone");
}
console.info("Pass: can get local timezone");