forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
393 lines (364 loc) · 13.4 KB
/
test.ts
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import * as datetime from "./mod.ts";
Deno.test({
name: "[std/datetime] parse",
fn: () => {
assertEquals(
datetime.parse("01-03-2019 16:30", "MM-dd-yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("01.03.2019 16:30", "MM.dd.yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("01.03.2019 16:30", "MM.dd.yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("03-01-2019 16:31", "dd-MM-yyyy HH:mm"),
new Date(2019, 0, 3, 16, 31),
);
assertEquals(
datetime.parse("2019-01-03 16:32", "yyyy-MM-dd HH:mm"),
new Date(2019, 0, 3, 16, 32),
);
assertEquals(
datetime.parse("16:33 01-03-2019", "HH:mm MM-dd-yyyy"),
new Date(2019, 0, 3, 16, 33),
);
assertEquals(
datetime.parse("01-03-2019 16:33:23.123", "MM-dd-yyyy HH:mm:ss.SSS"),
new Date(2019, 0, 3, 16, 33, 23, 123),
);
assertEquals(
datetime.parse("01-03-2019 09:33 PM", "MM-dd-yyyy HH:mm a"),
new Date(2019, 0, 3, 21, 33),
);
assertEquals(
datetime.parse("16:34 03-01-2019", "HH:mm dd-MM-yyyy"),
new Date(2019, 0, 3, 16, 34),
);
assertEquals(
datetime.parse("16:35 2019-01-03", "HH:mm yyyy-MM-dd"),
new Date(2019, 0, 3, 16, 35),
);
assertEquals(
datetime.parse("01-03-2019", "MM-dd-yyyy"),
new Date(2019, 0, 3),
);
assertEquals(
datetime.parse("03-01-2019", "dd-MM-yyyy"),
new Date(2019, 0, 3),
);
assertEquals(
datetime.parse("31-10-2019", "dd-MM-yyyy"),
new Date(2019, 9, 31),
);
assertEquals(
datetime.parse("2019-01-03", "yyyy-MM-dd"),
new Date(2019, 0, 3),
);
},
});
Deno.test({
name: "[std/datetime] invalidParseDateTimeFormatThrows",
fn: () => {
assertThrows((): void => {
// deno-lint-ignore no-explicit-any
(datetime as any).parse("2019-01-01 00:00", "x-y-z");
}, Error);
assertThrows((): void => {
// deno-lint-ignore no-explicit-any
(datetime as any).parse("2019-01-01", "x-y-z");
}, Error);
},
});
Deno.test({
name: "[std/datetime] format",
fn: () => {
// 00 hours
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T01:00:00"), "HH:mm:ss"),
);
assertEquals(
"13:00:00",
datetime.format(new Date("2019-01-01T13:00:00"), "HH:mm:ss"),
);
// 12 hours
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T01:00:00"), "hh:mm:ss"),
);
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T13:00:00"), "hh:mm:ss"),
);
// milliseconds
assertEquals(
"13:00:00.000",
datetime.format(new Date("2019-01-01T13:00:00"), "HH:mm:ss.SSS"),
);
assertEquals(
"13:00:00.000",
datetime.format(new Date("2019-01-01T13:00:00.000"), "HH:mm:ss.SSS"),
);
assertEquals(
"13:00:00.123",
datetime.format(new Date("2019-01-01T13:00:00.123"), "HH:mm:ss.SSS"),
);
// day period
assertEquals(
"01:00:00 AM",
datetime.format(new Date("2019-01-01T01:00:00"), "HH:mm:ss a"),
);
assertEquals(
"01:00:00 AM",
datetime.format(new Date("2019-01-01T01:00:00"), "hh:mm:ss a"),
);
assertEquals(
"01:00:00 PM",
datetime.format(new Date("2019-01-01T13:00:00"), "hh:mm:ss a"),
);
assertEquals(
"21:00:00 PM",
datetime.format(new Date("2019-01-01T21:00:00"), "HH:mm:ss a"),
);
assertEquals(
"09:00:00 PM",
datetime.format(new Date("2019-01-01T21:00:00"), "hh:mm:ss a"),
);
// quoted literal
assertEquals(
datetime.format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"),
"today: 2019-01-20",
);
},
});
Deno.test({
name: "[std/datetime] dayOfYear",
fn: () => {
// from https://golang.org/src/time/time_test.go
// Test YearDay in several different scenarios
// and corner cases
// Non-leap-year tests
assertEquals(datetime.dayOfYear(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("2007-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("2007-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("2007-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("2007-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("2007-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("2007-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("2007-12-31T00:00:00.000Z")), 365);
// Leap-year tests
assertEquals(datetime.dayOfYear(new Date("2008-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("2008-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("2008-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("2008-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("2008-03-01T00:00:00.000Z")), 61);
assertEquals(datetime.dayOfYear(new Date("2008-03-15T00:00:00.000Z")), 75);
assertEquals(datetime.dayOfYear(new Date("2008-04-01T00:00:00.000Z")), 92);
assertEquals(datetime.dayOfYear(new Date("2008-12-31T00:00:00.000Z")), 366);
// Looks like leap-year (but isn't) tests
assertEquals(datetime.dayOfYear(new Date("1900-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("1900-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("1900-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("1900-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("1900-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("1900-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("1900-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("1900-12-31T00:00:00.000Z")), 365);
// Year one tests (non-leap)
assertEquals(datetime.dayOfYear(new Date("0001-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("0001-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("0001-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("0001-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("0001-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("0001-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("0001-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("0001-12-31T00:00:00.000Z")), 365);
// Year minus one tests (non-leap)
assertEquals(
datetime.dayOfYear(new Date("-000001-01-01T00:00:00.000Z")),
1,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-01-15T00:00:00.000Z")),
15,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-02-01T00:00:00.000Z")),
32,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-02-15T00:00:00.000Z")),
46,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-03-01T00:00:00.000Z")),
60,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-03-15T00:00:00.000Z")),
74,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-04-01T00:00:00.000Z")),
91,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-12-31T00:00:00.000Z")),
365,
);
// 400 BC tests (leap-year)
assertEquals(
datetime.dayOfYear(new Date("-000400-01-01T00:00:00.000Z")),
1,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-01-15T00:00:00.000Z")),
15,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-02-01T00:00:00.000Z")),
32,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-02-15T00:00:00.000Z")),
46,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-03-01T00:00:00.000Z")),
61,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-03-15T00:00:00.000Z")),
75,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-04-01T00:00:00.000Z")),
92,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-12-31T00:00:00.000Z")),
366,
);
// Special Cases
// Gregorian calendar change (no effect)
assertEquals(datetime.dayOfYear(new Date("1582-10-04T03:24:00.000Z")), 277);
assertEquals(datetime.dayOfYear(new Date("1582-10-15T03:24:00.000Z")), 288);
},
});
Deno.test({
name: "[std/datetime] weekOfYear",
fn: () => {
assertEquals(datetime.weekOfYear(new Date("2020-01-05T03:00:00.000Z")), 1);
assertEquals(datetime.weekOfYear(new Date("2020-06-28T03:00:00.000Z")), 26);
// iso weeks year starting sunday
assertEquals(datetime.weekOfYear(new Date(2012, 0, 1)), 52);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 2)), 1);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 8)), 1);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 9)), 2);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 15)), 2);
// iso weeks year starting monday
assertEquals(datetime.weekOfYear(new Date(2007, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 7)), 1);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 8)), 2);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 14)), 2);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 15)), 3);
// iso weeks year starting tuesday
assertEquals(datetime.weekOfYear(new Date(2007, 11, 31)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 6)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 7)), 2);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 13)), 2);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 14)), 3);
// iso weeks year starting wednesday
assertEquals(datetime.weekOfYear(new Date(2002, 11, 30)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 5)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 6)), 2);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 12)), 2);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 13)), 3);
// iso weeks year starting thursday
assertEquals(datetime.weekOfYear(new Date(2008, 11, 29)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 4)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 5)), 2);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 11)), 2);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 13)), 3);
// iso weeks year starting friday
assertEquals(datetime.weekOfYear(new Date(2009, 11, 28)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 1)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 3)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 4)), 1);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 10)), 1);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 11)), 2);
// iso weeks year starting saturday
assertEquals(datetime.weekOfYear(new Date(2010, 11, 27)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 1)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 2)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 3)), 1);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 9)), 1);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 10)), 2);
},
});
Deno.test({
name: "[std/datetime] to IMF",
fn(): void {
const actual = datetime.toIMF(new Date(Date.UTC(1994, 3, 5, 15, 32)));
const expected = "Tue, 05 Apr 1994 15:32:00 GMT";
assertEquals(actual, expected);
},
});
Deno.test({
name: "[std/datetime] to IMF 0",
fn(): void {
const actual = datetime.toIMF(new Date(0));
const expected = "Thu, 01 Jan 1970 00:00:00 GMT";
assertEquals(actual, expected);
},
});
Deno.test({
name: "[std/datetime] isLeap",
fn(): void {
assert(datetime.isLeap(1992));
assert(datetime.isLeap(2000));
assert(!datetime.isLeap(2003));
assert(!datetime.isLeap(2007));
},
});
Deno.test({
name: "[std/datetime] difference",
fn(): void {
const denoInit = new Date("2018/5/14");
const denoReleaseV1 = new Date("2020/5/13");
let difference = datetime.difference(denoReleaseV1, denoInit, {
units: ["days", "months", "years"],
});
assertEquals(difference.days, 730);
assertEquals(difference.months, 23);
assertEquals(difference.years, 1);
const birth = new Date("1998/2/23 10:10:10");
const old = new Date("1998/2/23 11:11:11");
difference = datetime.difference(birth, old, {
units: ["milliseconds", "minutes", "seconds", "hours"],
});
assertEquals(difference.milliseconds, 3661000);
assertEquals(difference.seconds, 3661);
assertEquals(difference.minutes, 61);
assertEquals(difference.hours, 1);
},
});
Deno.test({
name: "[std/datetime] constants",
fn(): void {
assertEquals(datetime.SECOND, 1e3);
assertEquals(datetime.MINUTE, datetime.SECOND * 60);
assertEquals(datetime.HOUR, datetime.MINUTE * 60);
assertEquals(datetime.DAY, datetime.HOUR * 24);
assertEquals(datetime.WEEK, datetime.DAY * 7);
},
});