forked from tc39/proposal-temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoneddatetime.mjs
769 lines (725 loc) · 35.2 KB
/
zoneddatetime.mjs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#! /usr/bin/env -S node --experimental-modules
/*
** Copyright (C) 2018-2019 Bloomberg LP. All rights reserved.
** This code is governed by the license found in the LICENSE file.
*/
import Demitasse from '@pipobscure/demitasse';
const { describe, it, report } = Demitasse;
import Pretty from '@pipobscure/demitasse-pretty';
const { reporter } = Pretty;
import { strict as assert } from 'assert';
const { equal, throws } = assert;
import * as Temporal from 'proposal-temporal';
const { DateTime, ZonedDateTime } = Temporal;
describe('ZonedDateTime', () => {
const tz = new Temporal.TimeZone('America/Los_Angeles');
// const dstEnd = ZonedDateTime.from({ ...new DateTime(2020, 11, 1, 2).getFields(), timeZone: tz });
const hourBeforeDstStart = ZonedDateTime.from({ ...new DateTime(2020, 3, 8, 1).getFields(), timeZone: tz });
const dayBeforeDstStart = ZonedDateTime.from({ ...new DateTime(2020, 3, 7, 2, 30).getFields(), timeZone: tz });
describe('toZonedDateTime() on other Temporal objects', () => {
it('Date.toZonedDateTime() works', () => {
const date = Temporal.Date.from('2020-01-01');
const time = Temporal.Time.from('12:00');
const zdt = date.toZonedDateTime('America/Los_Angeles', time);
equal(zdt.toString(), '2020-01-01T12:00-08:00[America/Los_Angeles]');
});
it('Date.toZonedDateTime() works with time omitted', () => {
const date = Temporal.Date.from('2020-01-01');
const zdt = date.toZonedDateTime('America/Los_Angeles');
equal(zdt.toString(), '2020-01-01T00:00-08:00[America/Los_Angeles]');
});
it('Date.toZonedDateTime() works with disambiguation option', () => {
const date = Temporal.Date.from('2020-03-08');
const time = Temporal.Time.from('02:00');
const zdt = date.toZonedDateTime('America/Los_Angeles', time, { disambiguation: 'earlier' });
equal(zdt.toString(), '2020-03-08T01:00-08:00[America/Los_Angeles]');
});
it('Time.toZonedDateTime works', () => {
const date = Temporal.Date.from('2020-01-01');
const time = Temporal.Time.from('12:00');
const zdt = time.toZonedDateTime('America/Los_Angeles', date);
equal(zdt.toString(), '2020-01-01T12:00-08:00[America/Los_Angeles]');
});
it('Time.toZonedDateTime() works with disambiguation option', () => {
const date = Temporal.Date.from('2020-03-08');
const time = Temporal.Time.from('02:00');
const zdt = time.toZonedDateTime('America/Los_Angeles', date, { disambiguation: 'earlier' });
equal(zdt.toString(), '2020-03-08T01:00-08:00[America/Los_Angeles]');
});
it('Instant.toZonedDateTime works', () => {
const instant = Temporal.Instant.from('2020-01-01T00:00-08:00');
const zdt = instant.toZonedDateTime('America/Los_Angeles');
equal(zdt.toString(), '2020-01-01T00:00-08:00[America/Los_Angeles]');
});
it('DateTime.toZonedDateTime works', () => {
const dt = Temporal.DateTime.from('2020-01-01T00:00');
const zdt = dt.toZonedDateTime('America/Los_Angeles');
equal(zdt.toString(), '2020-01-01T00:00-08:00[America/Los_Angeles]');
});
it('DateTime.toZonedDateTime works with disambiguation option', () => {
const dt = Temporal.DateTime.from('2020-03-08T02:00');
const zdt = dt.toZonedDateTime('America/Los_Angeles', { disambiguation: 'earlier' });
equal(zdt.toString(), '2020-03-08T01:00-08:00[America/Los_Angeles]');
});
});
describe('string parsing', () => {
it('parses with an IANA zone', () => {
const zdt = Temporal.ZonedDateTime.from('2020-03-08T01:00-08:00[America/Los_Angeles]');
equal(zdt.toString(), '2020-03-08T01:00-08:00[America/Los_Angeles]');
});
it('parses with an IANA zone but no offset', () => {
const zdt = Temporal.ZonedDateTime.from('2020-03-08T01:00[America/Los_Angeles]');
equal(zdt.toString(), '2020-03-08T01:00-08:00[America/Los_Angeles]');
});
it('parses with an IANA zone but no offset (with disambiguation)', () => {
const zdt = Temporal.ZonedDateTime.from('2020-03-08T02:30[America/Los_Angeles]', { disambiguation: 'earlier' });
equal(zdt.toString(), '2020-03-08T01:30-08:00[America/Los_Angeles]');
});
it('parses with an offset in brackets', () => {
const zdt = Temporal.ZonedDateTime.from('2020-03-08T01:00-08:00[-08:00]');
equal(zdt.toString(), '2020-03-08T01:00-08:00[-08:00]');
});
it('parses with "Z" used as an offset', () => {
const iso = '2020-03-08T01:00-08:00[America/Los_Angeles]';
const instant = Temporal.ZonedDateTime.from(iso).toInstant();
equal(instant.toString(), '2020-03-08T09:00Z');
const zdt = Temporal.ZonedDateTime.from(`${instant.toString()}[America/Los_Angeles]`);
equal(zdt.toString(), iso);
});
it('throws if no brackets', () => {
throws(() => Temporal.ZonedDateTime.from('2020-03-08T01:00-08:00'));
});
it("{ offset: 'reject' } throws if offset does not match offset time zone", () => {
throws(() => Temporal.ZonedDateTime.from('2020-03-08T01:00-04:00[-08:00]', { offset: 'reject' }));
});
it("{ offset: 'reject' } throws if offset does not match IANA time zone", () => {
throws(() => Temporal.ZonedDateTime.from('2020-03-08T01:00-04:00[America/Chicago]', { offset: 'reject' }));
});
it("{ offset: 'prefer' } if offset matches time zone (first 1:30 when DST ends)", () => {
const zdt = Temporal.ZonedDateTime.from('2020-11-01T01:30-07:00[America/Los_Angeles]', { offset: 'prefer' });
equal(zdt.toString(), '2020-11-01T01:30-07:00[America/Los_Angeles]');
});
it("{ offset: 'prefer' } if offset matches time zone (second 1:30 when DST ends)", () => {
const zdt = Temporal.ZonedDateTime.from('2020-11-01T01:30-08:00[America/Los_Angeles]', { offset: 'prefer' });
equal(zdt.toString(), '2020-11-01T01:30-08:00[America/Los_Angeles]');
});
it("{ offset: 'prefer' } if offset does not match time zone", () => {
const zdt = Temporal.ZonedDateTime.from('2020-11-01T04:00-07:00[America/Los_Angeles]', { offset: 'prefer' });
equal(zdt.toString(), '2020-11-01T04:00-08:00[America/Los_Angeles]');
});
it("{ offset: 'ignore' } uses time zone only", () => {
const zdt = Temporal.ZonedDateTime.from('2020-11-01T04:00-12:00[America/Los_Angeles]', { offset: 'ignore' });
equal(zdt.toString(), '2020-11-01T04:00-08:00[America/Los_Angeles]');
});
it("{ offset: 'use' } uses offset only", () => {
const zdt = Temporal.ZonedDateTime.from('2020-11-01T04:00-07:00[America/Los_Angeles]', { offset: 'use' });
equal(zdt.toString(), '2020-11-01T03:00-08:00[America/Los_Angeles]');
});
});
describe('properties around DST', () => {
it('hoursInDay works with DST start', () => {
equal(hourBeforeDstStart.hoursInDay, 23);
});
it('hoursInDay works with non-DST days', () => {
equal(dayBeforeDstStart.hoursInDay, 24);
});
it('hoursInDay works with DST end', () => {
const dstEnd = ZonedDateTime.from('2020-11-01T01:00-08:00[America/Los_Angeles]');
equal(dstEnd.hoursInDay, 25);
});
it('hoursInDay works when day starts at 1:00 due to DST start at midnight', () => {
const zdt = Temporal.ZonedDateTime.from('2015-10-18T12:00:00-02:00[America/Sao_Paulo]');
equal(zdt.hoursInDay, 23);
});
it('startOfDay works', () => {
const start = dayBeforeDstStart.startOfDay;
equal(start.toDate().toString(), dayBeforeDstStart.toDate().toString());
equal('00:00', start.toTime().toString());
});
it('startOfDay works when day starts at 1:00 due to DST start at midnight', () => {
const zdt = ZonedDateTime.from('2015-10-18T12:00:00-02:00[America/Sao_Paulo]');
const start = zdt.startOfDay;
equal('01:00', start.toTime().toString());
});
const dayAfterSamoaDateLineChange = ZonedDateTime.from('2011-12-31T22:00+14:00[Pacific/Apia]');
const dayBeforeSamoaDateLineChange = ZonedDateTime.from('2011-12-29T22:00-10:00[Pacific/Apia]');
it('startOfDay works after Samoa date line change', () => {
const start = dayAfterSamoaDateLineChange.startOfDay;
equal('00:00', start.toTime().toString());
});
it('hoursInDay works after Samoa date line change', () => {
equal(dayAfterSamoaDateLineChange.hoursInDay, 24);
});
it('hoursInDay works before Samoa date line change', () => {
equal(dayBeforeSamoaDateLineChange.hoursInDay, 24);
});
it('isOffsetTransition normally returns false', () => {
equal(hourBeforeDstStart.isOffsetTransition, false);
});
it('isOffsetTransition returns true at a DST start transition', () => {
const dstStart = hourBeforeDstStart.add({ hours: 1 });
equal(dstStart.isOffsetTransition, true);
});
it('isOffsetTransition returns true at a DST end transition', () => {
const dstEnd = ZonedDateTime.from('2020-11-01T01:00-08:00[America/Los_Angeles]');
equal(dstEnd.isOffsetTransition, true);
});
it('isOffsetTransition returns true right after Samoa date line change', () => {
const rightAfterSamoaDateLineChange = ZonedDateTime.from('2011-12-31T00:00+14:00[Pacific/Apia]');
equal(rightAfterSamoaDateLineChange.isOffsetTransition, true);
});
});
describe('math around DST', () => {
it('add 1 hour to get to DST start', () => {
const added = hourBeforeDstStart.add({ hours: 1 });
equal(added.hour, 3);
const diff = added.difference(hourBeforeDstStart, { largestUnit: 'hours' });
equal(diff.days, 0);
equal(diff.hours, 1);
equal(diff.minutes, 0);
const undo = added.subtract(diff);
equal(`${undo}`, `${hourBeforeDstStart}`);
});
it('add 2 hours to get to DST start +1', () => {
const added = hourBeforeDstStart.add({ hours: 2 });
equal(added.hour, 4);
const diff = added.difference(hourBeforeDstStart, { largestUnit: 'hours' });
equal(diff.days, 0);
equal(diff.hours, 2);
equal(diff.minutes, 0);
const undo = added.subtract(diff);
equal(`${undo}`, `${hourBeforeDstStart}`);
});
it('add 1.5 hours to get to 0.5 hours after DST start', () => {
const added = hourBeforeDstStart.add({ hours: 1, minutes: 30 });
equal(added.hour, 3);
equal(added.minute, 30);
const diff = added.difference(hourBeforeDstStart, { largestUnit: 'hours' });
equal(diff.days, 0);
equal(diff.hours, 1);
equal(diff.minutes, 30);
const undo = added.subtract(diff);
equal(`${undo}`, `${hourBeforeDstStart}`);
});
it('Samoa date line change (add): 10:00PM 29 Dec 2011 -> 11:00PM 31 Dec 2011', () => {
const dayBeforeSamoaDateLineChangeAbs = new Temporal.DateTime(2011, 12, 29, 22).toInstant('Pacific/Apia');
const start = dayBeforeSamoaDateLineChangeAbs.toZonedDateTime('Pacific/Apia');
const added = start.add({ days: 1, hours: 1 });
equal(added.day, 31);
equal(added.hour, 23);
equal(added.minute, 0);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 1);
equal(diff.days, 2);
const undo = added.subtract(diff);
equal(`${undo}`, `${start}`);
});
it('Samoa date line change (subtract): 11:00PM 31 Dec 2011 -> 10:00PM 29 Dec 2011', () => {
const dayAfterSamoaDateLineChangeAbs = new Temporal.DateTime(2011, 12, 31, 23).toInstant('Pacific/Apia');
const start = dayAfterSamoaDateLineChangeAbs.toZonedDateTime('Pacific/Apia');
const skipped = start.subtract({ days: 1, hours: 1 });
equal(skipped.day, 31);
equal(skipped.hour, 22);
equal(skipped.minute, 0);
const end = start.subtract({ days: 2, hours: 1 });
equal(end.day, 29);
equal(end.hour, 22);
equal(end.minute, 0);
const diff = end.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, -1);
equal(diff.days, -2);
const undo = start.add(diff);
equal(`${undo}`, `${end}`);
});
it('3:30 day before DST start -> 3:30 day of DST start', () => {
const start = dayBeforeDstStart.add({ hours: 1 }); // 3:30AM
const added = start.add({ days: 1 });
equal(added.day, 8);
equal(added.hour, 3);
equal(added.minute, 30);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, 1);
const undo = added.subtract(diff);
equal(`${undo}`, `${start}`);
});
it('2:30 day before DST start -> 3:30 day of DST start', () => {
const added = dayBeforeDstStart.add({ days: 1 });
equal(added.day, 8);
equal(added.hour, 3);
equal(added.minute, 30);
const diff = added.difference(dayBeforeDstStart, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, 1);
const undo = dayBeforeDstStart.add(diff);
equal(`${undo}`, `${added}`);
});
it('1:30 day DST starts -> 4:30 day DST starts', () => {
const start = dayBeforeDstStart.add({ hours: 23 }); // 1:30AM
const added = start.add({ hours: 2 });
equal(added.day, 8);
equal(added.hour, 4);
equal(added.minute, 30);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 2);
equal(diff.days, 0);
const undo = added.subtract(diff);
equal(`${undo}`, `${start}`);
});
it('2:00 day before DST starts -> 3:00 day DST starts', () => {
const start = hourBeforeDstStart.subtract({ days: 1 }).add({ hours: 1 }); // 2:00AM
const added = start.add({ days: 1 });
equal(added.day, 8);
equal(added.hour, 3);
equal(added.minute, 0);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, 1);
const undo = start.add(diff);
equal(`${undo}`, `${added}`);
});
it('1:00AM day DST starts -> (add 24 hours) -> 2:00AM day after DST starts', () => {
const start = hourBeforeDstStart; // 1:00AM
const added = start.add({ hours: 24 });
equal(added.day, 9);
equal(added.hour, 2);
equal(added.minute, 0);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 1);
equal(diff.days, 1);
const undo = added.subtract(diff);
equal(`${undo}`, `${start}`);
});
it('12:00AM day DST starts -> (add 24 hours) -> 1:00AM day after DST starts', () => {
const start = hourBeforeDstStart.subtract({ hours: 1 }); // 1:00AM
const added = start.add({ hours: 24 });
equal(added.day, 9);
equal(added.hour, 1);
equal(added.minute, 0);
const diff = added.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 1);
equal(diff.days, 1);
const undo = added.subtract(diff);
equal(`${undo}`, `${start}`);
});
it('Difference can return day length > 24 hours', () => {
const start = ZonedDateTime.from('2020-10-30T01:45-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-11-02T01:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 30);
equal(diff.hours, 24);
equal(diff.days, 2);
const undo = start.add(diff);
equal(`${undo}`, `${end}`);
});
it('Difference rounding (nearest day) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { smallestUnit: 'days' }); // roundingMode: 'nearest'
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, -3);
});
it('Difference rounding (ceil day) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { smallestUnit: 'days', roundingMode: 'ceil' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, -2);
});
it('Difference rounding (trunc day) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { smallestUnit: 'days', roundingMode: 'trunc' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, -2);
});
it('Difference rounding (floor day) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { smallestUnit: 'days', roundingMode: 'floor' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, -3);
});
it('Difference rounding (nearest hour) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days', smallestUnit: 'hours' }); // roundingMode: 'nearest'
equal(diff.minutes, 0);
equal(diff.hours, -12);
equal(diff.days, -2);
});
it('Difference rounding (ceil hour) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days', smallestUnit: 'hours', roundingMode: 'ceil' });
equal(diff.minutes, 0);
equal(diff.hours, -12);
equal(diff.days, -2);
});
it('Difference rounding (trunc hour) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days', smallestUnit: 'hours', roundingMode: 'trunc' });
equal(diff.minutes, 0);
equal(diff.hours, -12);
equal(diff.days, -2);
});
it('Difference rounding (floor hour) is DST-aware', () => {
const start = ZonedDateTime.from('2020-03-10T02:30-07:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-07T14:15-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days', smallestUnit: 'hours', roundingMode: 'floor' });
equal(diff.minutes, 0);
equal(diff.hours, -13);
equal(diff.days, -2);
});
it('Difference when date portion ends inside a DST-skipped period', () => {
const start = ZonedDateTime.from('2020-03-07T02:30-08:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-03-08T03:15-07:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 45);
equal(diff.hours, 23);
equal(diff.days, 0);
});
it("Difference when date portion ends inside day skipped by Samoa's 24hr 2011 transition", () => {
const end = ZonedDateTime.from('2011-12-31T05:00+14:00[Pacific/Apia]');
const start = ZonedDateTime.from('2011-12-28T10:00-10:00[Pacific/Apia]');
const diff = end.difference(start, { largestUnit: 'days' });
equal(diff.minutes, 0);
equal(diff.hours, 19);
equal(diff.days, 1);
});
it('Rounding up to hours causes one more day of overflow (positive)', () => {
const start = ZonedDateTime.from('2020-01-01T00:00-08:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-01-03T23:59-08:00[America/Los_Angeles]');
const diff = end.difference(start, { largestUnit: 'days', smallestUnit: 'hours' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, 3);
});
it('Rounding up to hours causes one more day of overflow (negative)', () => {
const start = ZonedDateTime.from('2020-01-01T00:00-08:00[America/Los_Angeles]');
const end = ZonedDateTime.from('2020-01-03T23:59-08:00[America/Los_Angeles]');
const diff = start.difference(end, { largestUnit: 'days', smallestUnit: 'hours' });
equal(diff.minutes, 0);
equal(diff.hours, 0);
equal(diff.days, -3);
});
it('addition and difference work near DST start', () => {
// Test the difference between different distances near DST start
const stepsPerHour = 2;
const minutesPerStep = 60 / stepsPerHour;
const hoursUntilEnd = 26;
const startHourRange = 3;
for (let i = 0; i < startHourRange * stepsPerHour; i++) {
const start = hourBeforeDstStart.add({ minutes: minutesPerStep * i });
for (let j = 0; j < hoursUntilEnd * stepsPerHour; j++) {
const end = start.add({ minutes: j * minutesPerStep });
const diff = end.difference(start, { largestUnit: 'days' });
const expectedMinutes = minutesPerStep * (j % stepsPerHour);
equal(diff.minutes, expectedMinutes);
const diff60 = Math.floor(j / stepsPerHour);
if (i >= stepsPerHour) {
// DST transition already happened
const expectedDays = diff60 < 24 ? 0 : diff60 < 48 ? 1 : 2;
const expectedHours = diff60 < 24 ? diff60 : diff60 < 48 ? diff60 - 24 : diff60 - 48;
equal(diff.hours, expectedHours);
equal(diff.days, expectedDays);
} else {
// DST transition hasn't happened yet
const expectedDays = diff60 < 23 ? 0 : diff60 < 47 ? 1 : 2;
const expectedHours = diff60 < 23 ? diff60 : diff60 < 47 ? diff60 - 23 : diff60 - 47;
equal(diff.hours, expectedHours);
equal(diff.days, expectedDays);
}
}
}
});
});
describe('math order of operations and options', () => {
const breakoutUnits = (op, zdt, d, options) =>
zdt[op]({ years: d.years }, options)
[op]({ months: d.months }, options)
[op]({ weeks: d.weeks }, options)
[op]({ days: d.days }, options)
[op](
{
hours: d.hours,
minutes: d.minutes,
seconds: d.seconds,
milliseconds: d.milliseconds,
microseconds: d.microseconds,
nanoseconds: d.nanoseconds
},
options
);
it('order of operations: add / none', () => {
const zdt = ZonedDateTime.from('2020-01-31T00:00-08:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = undefined;
const result = zdt.add(d, options);
equal(result.toString(), '2020-03-01T00:00-08:00[America/Los_Angeles]');
equal(breakoutUnits('add', zdt, d, options).toString(), result.toString());
});
it('order of operations: add / constrain', () => {
const zdt = ZonedDateTime.from('2020-01-31T00:00-08:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = { overflow: 'constrain' };
const result = zdt.add(d, options);
equal(result.toString(), '2020-03-01T00:00-08:00[America/Los_Angeles]');
equal(breakoutUnits('add', zdt, d, options).toString(), result.toString());
});
it('order of operations: add / reject', () => {
const zdt = ZonedDateTime.from('2020-01-31T00:00-08:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = { overflow: 'reject' };
throws(() => zdt.add(d, options));
});
it('order of operations: subtract / none', () => {
const zdt = ZonedDateTime.from('2020-03-31T00:00-07:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = undefined;
const result = zdt.subtract(d, options);
equal(result.toString(), '2020-02-28T00:00-08:00[America/Los_Angeles]');
equal(breakoutUnits('subtract', zdt, d, options).toString(), result.toString());
});
it('order of operations: subtract / constrain', () => {
const zdt = ZonedDateTime.from('2020-03-31T00:00-07:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = { overflow: 'constrain' };
const result = zdt.subtract(d, options);
equal(result.toString(), '2020-02-28T00:00-08:00[America/Los_Angeles]');
equal(breakoutUnits('subtract', zdt, d, options).toString(), result.toString());
});
it('order of operations: subtract / reject', () => {
const zdt = ZonedDateTime.from('2020-03-31T00:00-07:00[America/Los_Angeles]');
const d = Temporal.Duration.from({ months: 1, days: 1 });
const options = { overflow: 'reject' };
throws(() => zdt.subtract(d, options));
});
});
describe('Structure', () => {
it('ZonedDateTime is a Function', () => {
equal(typeof ZonedDateTime, 'function');
});
it('ZonedDateTime has a prototype', () => {
assert(ZonedDateTime.prototype);
equal(typeof ZonedDateTime.prototype, 'object');
});
describe('ZonedDateTime.prototype', () => {
it('ZonedDateTime.prototype has year', () => {
assert('year' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has month', () => {
assert('month' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has day', () => {
assert('day' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has hour', () => {
assert('hour' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has minute', () => {
assert('minute' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has second', () => {
assert('second' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has millisecond', () => {
assert('millisecond' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has microsecond', () => {
assert('microsecond' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has nanosecond', () => {
assert('nanosecond' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has dayOfWeek', () => {
assert('dayOfWeek' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has dayOfYear', () => {
assert('dayOfYear' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has weekOfYear', () => {
assert('weekOfYear' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype.with is a Function', () => {
equal(typeof ZonedDateTime.prototype.with, 'function');
});
it('ZonedDateTime.prototype.add is a Function', () => {
equal(typeof ZonedDateTime.prototype.add, 'function');
});
it('ZonedDateTime.prototype.subtract is a Function', () => {
equal(typeof ZonedDateTime.prototype.subtract, 'function');
});
it('ZonedDateTime.prototype.difference is a Function', () => {
equal(typeof ZonedDateTime.prototype.difference, 'function');
});
it('ZonedDateTime.prototype.equals is a Function', () => {
equal(typeof ZonedDateTime.prototype.equals, 'function');
});
it('ZonedDateTime.prototype.getDate is a Function', () => {
equal(typeof ZonedDateTime.prototype.toDate, 'function');
});
it('ZonedDateTime.prototype.getTime is a Function', () => {
equal(typeof ZonedDateTime.prototype.toTime, 'function');
});
it('ZonedDateTime.prototype.getFields is a Function', () => {
equal(typeof ZonedDateTime.prototype.getFields, 'function');
});
it('ZonedDateTime.prototype.getISOFields is a Function', () => {
equal(typeof ZonedDateTime.prototype.getISOFields, 'function');
});
it('ZonedDateTime.prototype.toString is a Function', () => {
equal(typeof ZonedDateTime.prototype.toString, 'function');
});
it('ZonedDateTime.prototype.toJSON is a Function', () => {
equal(typeof ZonedDateTime.prototype.toJSON, 'function');
});
it('ZonedDateTime.prototype has toInstant', () => {
assert('toInstant' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has timeZone', () => {
assert('timeZone' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has hoursInDay', () => {
assert('hoursInDay' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has offsetNanoseconds', () => {
assert('offsetNanoseconds' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype has offsetString', () => {
assert('offset' in ZonedDateTime.prototype);
});
it('ZonedDateTime.prototype.getDateTime is a Function', () => {
equal(typeof ZonedDateTime.prototype.toDateTime, 'function');
});
});
it('ZonedDateTime.from is a Function', () => {
equal(typeof ZonedDateTime.from, 'function');
});
it('ZonedDateTime.compare is a Function', () => {
equal(typeof ZonedDateTime.compare, 'function');
});
});
describe('Construction', () => {
const epochMillis = Date.UTC(1976, 10, 18, 15, 23, 30, 123);
const epochNanos = BigInt(epochMillis) * BigInt(1e6) + BigInt(456789);
const zdt = new ZonedDateTime(epochNanos, tz);
assert(zdt);
equal(typeof zdt, 'object');
equal(zdt.toInstant().epochSeconds, Math.floor(Date.UTC(1976, 10, 18, 15, 23, 30, 123) / 1e3), 'epochSeconds');
equal(zdt.toInstant().epochMilliseconds, Date.UTC(1976, 10, 18, 15, 23, 30, 123), 'epochMilliseconds');
describe('ZonedDateTime for (1976, 11, 18, 15, 23, 30, 123, 456, 789)', () => {
it('datetime can be constructed', () => {
const zonedDateTime = new ZonedDateTime(epochNanos, new Temporal.TimeZone('UTC'));
assert(zonedDateTime);
equal(typeof zonedDateTime, 'object');
});
const zonedDateTime = new ZonedDateTime(epochNanos, new Temporal.TimeZone('UTC'));
it('zonedDateTime.year is 1976', () => equal(zonedDateTime.year, 1976));
it('zonedDateTime.month is 11', () => equal(zonedDateTime.month, 11));
it('zonedDateTime.day is 18', () => equal(zonedDateTime.day, 18));
it('zonedDateTime.hour is 15', () => equal(zonedDateTime.hour, 15));
it('zonedDateTime.minute is 23', () => equal(zonedDateTime.minute, 23));
it('zonedDateTime.second is 30', () => equal(zonedDateTime.second, 30));
it('zonedDateTime.millisecond is 123', () => equal(zonedDateTime.millisecond, 123));
it('zonedDateTime.microsecond is 456', () => equal(zonedDateTime.microsecond, 456));
it('zonedDateTime.nanosecond is 789', () => equal(zonedDateTime.nanosecond, 789));
it('zonedDateTime.dayOfWeek is 4', () => equal(zonedDateTime.dayOfWeek, 4));
it('zonedDateTime.dayOfYear is 323', () => equal(zonedDateTime.dayOfYear, 323));
it('zonedDateTime.weekOfYear is 47', () => equal(zonedDateTime.weekOfYear, 47));
it('`${zonedDateTime}` is 1976-11-18T15:23:30.123456789+00:00[UTC]', () =>
equal(`${zonedDateTime}`, '1976-11-18T15:23:30.123456789+00:00[UTC]'));
});
describe('epochXXX properties', () => {
const ins = Temporal.Instant.from('1976-11-18T15:23:30.123456789Z');
const zdt = ins.toZonedDateTime('America/Los_Angeles');
it('zonedDateTime.epochNanoseconds is 217178610123456789n', () => equal(zdt.epochMicroseconds, 217178610123456n));
it('zonedDateTime.epochMicroseconds is 217178610123456n', () => equal(zdt.epochMicroseconds, 217178610123456n));
it('zonedDateTime.epochMilliseconds is 217178610123', () => equal(zdt.epochMilliseconds, 217178610123));
it('zonedDateTime.epochSeconds is 217178610', () => equal(zdt.epochSeconds, 217178610));
});
});
describe('.with manipulation', () => {
const dt = new Temporal.DateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);
const zonedDateTime = ZonedDateTime.from({ ...dt.getFields(), timeZone: 'UTC' });
it('zonedDateTime.with({ year: 2019 } works', () => {
equal(`${zonedDateTime.with({ year: 2019 })}`, '2019-11-18T15:23:30.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ month: 5 } works', () => {
equal(`${zonedDateTime.with({ month: 5 })}`, '1976-05-18T15:23:30.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ day: 5 } works', () => {
equal(`${zonedDateTime.with({ day: 5 })}`, '1976-11-05T15:23:30.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ hour: 5 } works', () => {
equal(`${zonedDateTime.with({ hour: 5 })}`, '1976-11-18T05:23:30.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ minute: 5 } works', () => {
equal(`${zonedDateTime.with({ minute: 5 })}`, '1976-11-18T15:05:30.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ second: 5 } works', () => {
equal(`${zonedDateTime.with({ second: 5 })}`, '1976-11-18T15:23:05.123456789+00:00[UTC]');
});
it('zonedDateTime.with({ millisecond: 5 } works', () => {
equal(`${zonedDateTime.with({ millisecond: 5 })}`, '1976-11-18T15:23:30.005456789+00:00[UTC]');
});
it('zonedDateTime.with({ microsecond: 5 } works', () => {
equal(`${zonedDateTime.with({ microsecond: 5 })}`, '1976-11-18T15:23:30.123005789+00:00[UTC]');
});
it('zonedDateTime.with({ nanosecond: 5 } works', () => {
equal(`${zonedDateTime.with({ nanosecond: 5 })}`, '1976-11-18T15:23:30.123456005+00:00[UTC]');
});
it('zonedDateTime.with({ month: 5, second: 15 } works', () => {
equal(`${zonedDateTime.with({ month: 5, second: 15 })}`, '1976-05-18T15:23:15.123456789+00:00[UTC]');
});
it('invalid overflow', () => {
['', 'CONSTRAIN', 'balance', 3, null].forEach((overflow) =>
throws(() => zonedDateTime.with({ day: 5 }, { overflow }), RangeError)
);
});
});
describe('.withTimeZone manipulation', () => {
it("zonedDateTime.withTimeZone('America/Los_Angeles') works", () => {
const instant = Temporal.Instant.from('2019-11-18T15:23:30.123456789-08:00[America/Los_Angeles]');
const zonedDateTime = instant.toZonedDateTime('UTC');
equal(
`${zonedDateTime.withTimeZone('America/Los_Angeles')}`,
'2019-11-18T15:23:30.123456789-08:00[America/Los_Angeles]'
);
});
});
describe('rounding', () => {
it('round to nearest day', () => {
const zdt = ZonedDateTime.from('2019-11-18T15:23:30.123456789-08:00[America/Los_Angeles]');
const rounded = zdt.round({ smallestUnit: 'day' });
equal(`${rounded}`, '2019-11-19T00:00-08:00[America/Los_Angeles]');
});
it('round to nearest hour', () => {
const zdt = ZonedDateTime.from('2019-11-18T15:23:30.123456789-08:00[America/Los_Angeles]');
const rounded = zdt.round({ smallestUnit: 'hour' });
equal(`${rounded}`, '2019-11-18T15:00-08:00[America/Los_Angeles]');
});
it('round to nearest minute', () => {
const zdt = ZonedDateTime.from('2019-11-18T15:23:30.123456789-08:00[America/Los_Angeles]');
const rounded = zdt.round({ smallestUnit: 'minute' });
equal(`${rounded}`, '2019-11-18T15:24-08:00[America/Los_Angeles]');
});
});
});
import { normalize } from 'path';
if (normalize(import.meta.url.slice(8)) === normalize(process.argv[1])) {
report(reporter).then((failed) => process.exit(failed ? 1 : 0));
}