-
Notifications
You must be signed in to change notification settings - Fork 156
/
duration.mjs
776 lines (746 loc) · 23.5 KB
/
duration.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
770
771
772
773
774
775
776
/* global __debug__ */
import bigInt from 'big-integer';
import * as ES from './ecmascript.mjs';
import { MakeIntrinsicClass } from './intrinsicclass.mjs';
import { CalendarMethodRecord } from './methodrecord.mjs';
import {
YEARS,
MONTHS,
WEEKS,
DAYS,
HOURS,
MINUTES,
SECONDS,
MILLISECONDS,
MICROSECONDS,
NANOSECONDS,
CALENDAR,
INSTANT,
CreateSlots,
GetSlot,
SetSlot
} from './slots.mjs';
const MathAbs = Math.abs;
const ObjectCreate = Object.create;
export class Duration {
constructor(
years = 0,
months = 0,
weeks = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0,
microseconds = 0,
nanoseconds = 0
) {
years = years === undefined ? 0 : ES.ToIntegerIfIntegral(years);
months = months === undefined ? 0 : ES.ToIntegerIfIntegral(months);
weeks = weeks === undefined ? 0 : ES.ToIntegerIfIntegral(weeks);
days = days === undefined ? 0 : ES.ToIntegerIfIntegral(days);
hours = hours === undefined ? 0 : ES.ToIntegerIfIntegral(hours);
minutes = minutes === undefined ? 0 : ES.ToIntegerIfIntegral(minutes);
seconds = seconds === undefined ? 0 : ES.ToIntegerIfIntegral(seconds);
milliseconds = milliseconds === undefined ? 0 : ES.ToIntegerIfIntegral(milliseconds);
microseconds = microseconds === undefined ? 0 : ES.ToIntegerIfIntegral(microseconds);
nanoseconds = nanoseconds === undefined ? 0 : ES.ToIntegerIfIntegral(nanoseconds);
ES.RejectDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
CreateSlots(this);
SetSlot(this, YEARS, years);
SetSlot(this, MONTHS, months);
SetSlot(this, WEEKS, weeks);
SetSlot(this, DAYS, days);
SetSlot(this, HOURS, hours);
SetSlot(this, MINUTES, minutes);
SetSlot(this, SECONDS, seconds);
SetSlot(this, MILLISECONDS, milliseconds);
SetSlot(this, MICROSECONDS, microseconds);
SetSlot(this, NANOSECONDS, nanoseconds);
if (typeof __debug__ !== 'undefined' && __debug__) {
Object.defineProperty(this, '_repr_', {
value: `Temporal.Duration <${ES.TemporalDurationToString(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
)}>`,
writable: false,
enumerable: false,
configurable: false
});
}
}
get years() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, YEARS);
}
get months() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, MONTHS);
}
get weeks() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, WEEKS);
}
get days() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, DAYS);
}
get hours() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, HOURS);
}
get minutes() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, MINUTES);
}
get seconds() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, SECONDS);
}
get milliseconds() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, MILLISECONDS);
}
get microseconds() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, MICROSECONDS);
}
get nanoseconds() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return GetSlot(this, NANOSECONDS);
}
get sign() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return ES.DurationSign(
GetSlot(this, YEARS),
GetSlot(this, MONTHS),
GetSlot(this, WEEKS),
GetSlot(this, DAYS),
GetSlot(this, HOURS),
GetSlot(this, MINUTES),
GetSlot(this, SECONDS),
GetSlot(this, MILLISECONDS),
GetSlot(this, MICROSECONDS),
GetSlot(this, NANOSECONDS)
);
}
get blank() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return (
ES.DurationSign(
GetSlot(this, YEARS),
GetSlot(this, MONTHS),
GetSlot(this, WEEKS),
GetSlot(this, DAYS),
GetSlot(this, HOURS),
GetSlot(this, MINUTES),
GetSlot(this, SECONDS),
GetSlot(this, MILLISECONDS),
GetSlot(this, MICROSECONDS),
GetSlot(this, NANOSECONDS)
) === 0
);
}
with(durationLike) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
const partialDuration = ES.PrepareTemporalFields(
durationLike,
[
'days',
'hours',
'microseconds',
'milliseconds',
'minutes',
'months',
'nanoseconds',
'seconds',
'weeks',
'years'
],
'partial'
);
let {
years = GetSlot(this, YEARS),
months = GetSlot(this, MONTHS),
weeks = GetSlot(this, WEEKS),
days = GetSlot(this, DAYS),
hours = GetSlot(this, HOURS),
minutes = GetSlot(this, MINUTES),
seconds = GetSlot(this, SECONDS),
milliseconds = GetSlot(this, MILLISECONDS),
microseconds = GetSlot(this, MICROSECONDS),
nanoseconds = GetSlot(this, NANOSECONDS)
} = partialDuration;
return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
}
negated() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return ES.CreateNegatedTemporalDuration(this);
}
abs() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return new Duration(
Math.abs(GetSlot(this, YEARS)),
Math.abs(GetSlot(this, MONTHS)),
Math.abs(GetSlot(this, WEEKS)),
Math.abs(GetSlot(this, DAYS)),
Math.abs(GetSlot(this, HOURS)),
Math.abs(GetSlot(this, MINUTES)),
Math.abs(GetSlot(this, SECONDS)),
Math.abs(GetSlot(this, MILLISECONDS)),
Math.abs(GetSlot(this, MICROSECONDS)),
Math.abs(GetSlot(this, NANOSECONDS))
);
}
add(other, options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return ES.AddDurationToOrSubtractDurationFromDuration('add', this, other, options);
}
subtract(other, options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return ES.AddDurationToOrSubtractDurationFromDuration('subtract', this, other, options);
}
round(roundTo) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
if (roundTo === undefined) throw new TypeError('options parameter is required');
let years = GetSlot(this, YEARS);
let months = GetSlot(this, MONTHS);
let weeks = GetSlot(this, WEEKS);
let days = GetSlot(this, DAYS);
let hours = GetSlot(this, HOURS);
let minutes = GetSlot(this, MINUTES);
let seconds = GetSlot(this, SECONDS);
let milliseconds = GetSlot(this, MILLISECONDS);
let microseconds = GetSlot(this, MICROSECONDS);
let nanoseconds = GetSlot(this, NANOSECONDS);
const existingLargestUnit = ES.DefaultTemporalLargestUnit(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
);
if (ES.Type(roundTo) === 'String') {
const stringParam = roundTo;
roundTo = ObjectCreate(null);
roundTo.smallestUnit = stringParam;
} else {
roundTo = ES.GetOptionsObject(roundTo);
}
let largestUnit = ES.GetTemporalUnit(roundTo, 'largestUnit', 'datetime', undefined, ['auto']);
let { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(roundTo);
const roundingIncrement = ES.ToTemporalRoundingIncrement(roundTo);
const roundingMode = ES.ToTemporalRoundingMode(roundTo, 'halfExpand');
let smallestUnit = ES.GetTemporalUnit(roundTo, 'smallestUnit', 'datetime', undefined);
let smallestUnitPresent = true;
if (!smallestUnit) {
smallestUnitPresent = false;
smallestUnit = 'nanosecond';
}
const defaultLargestUnit = ES.LargerOfTwoTemporalUnits(existingLargestUnit, smallestUnit);
let largestUnitPresent = true;
if (!largestUnit) {
largestUnitPresent = false;
largestUnit = defaultLargestUnit;
}
if (largestUnit === 'auto') largestUnit = defaultLargestUnit;
if (!smallestUnitPresent && !largestUnitPresent) {
throw new RangeError('at least one of smallestUnit or largestUnit is required');
}
if (ES.LargerOfTwoTemporalUnits(largestUnit, smallestUnit) !== largestUnit) {
throw new RangeError(`largestUnit ${largestUnit} cannot be smaller than smallestUnit ${smallestUnit}`);
}
const maximumIncrements = {
hour: 24,
minute: 60,
second: 60,
millisecond: 1000,
microsecond: 1000,
nanosecond: 1000
};
const maximum = maximumIncrements[smallestUnit];
if (maximum !== undefined) ES.ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false);
const roundingGranularityIsNoop = smallestUnit === 'nanosecond' && roundingIncrement === 1;
const balancingRequested = largestUnit !== existingLargestUnit;
const calendarUnitsPresent = years !== 0 || months !== 0 || weeks !== 0;
const timeUnitsOverflowWillOccur =
MathAbs(minutes) >= 60 ||
MathAbs(seconds) >= 60 ||
MathAbs(milliseconds) >= 1000 ||
MathAbs(microseconds) >= 1000 ||
MathAbs(nanoseconds) >= 1000;
const hoursToDaysConversionMayOccur = (days !== 0 && zonedRelativeTo) || MathAbs(hours) >= 24;
if (
roundingGranularityIsNoop &&
!balancingRequested &&
!calendarUnitsPresent &&
!timeUnitsOverflowWillOccur &&
!hoursToDaysConversionMayOccur
) {
return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
}
let precalculatedPlainDateTime;
const plainDateTimeOrRelativeToWillBeUsed =
!roundingGranularityIsNoop ||
largestUnit === 'year' ||
largestUnit === 'month' ||
largestUnit === 'week' ||
largestUnit === 'day' ||
calendarUnitsPresent ||
days !== 0;
if (zonedRelativeTo && plainDateTimeOrRelativeToWillBeUsed) {
// Convert a ZonedDateTime relativeTo to PlainDateTime and PlainDate only
// if either is needed in one of the operations below, because the
// conversion is user visible
precalculatedPlainDateTime = ES.GetPlainDateTimeFor(
timeZoneRec,
GetSlot(zonedRelativeTo, INSTANT),
GetSlot(zonedRelativeTo, CALENDAR)
);
plainRelativeTo = ES.TemporalDateTimeToDate(precalculatedPlainDateTime);
}
const calendarRec = CalendarMethodRecord.CreateFromRelativeTo(plainRelativeTo, zonedRelativeTo, [
'dateAdd',
'dateUntil'
]);
({ years, months, weeks, days } = ES.UnbalanceDateDurationRelative(
years,
months,
weeks,
days,
largestUnit,
plainRelativeTo,
calendarRec
));
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
ES.RoundDuration(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
roundingIncrement,
smallestUnit,
roundingMode,
plainRelativeTo,
calendarRec,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
));
if (zonedRelativeTo) {
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
ES.AdjustRoundedDurationDays(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
roundingIncrement,
smallestUnit,
roundingMode,
zonedRelativeTo,
calendarRec,
timeZoneRec,
precalculatedPlainDateTime
));
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDurationRelative(
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
largestUnit,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
));
} else {
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDuration(
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
largestUnit
));
}
({ years, months, weeks, days } = ES.BalanceDateDurationRelative(
years,
months,
weeks,
days,
largestUnit,
smallestUnit,
plainRelativeTo,
calendarRec
));
return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
}
total(totalOf) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
let years = GetSlot(this, YEARS);
let months = GetSlot(this, MONTHS);
let weeks = GetSlot(this, WEEKS);
let days = GetSlot(this, DAYS);
let hours = GetSlot(this, HOURS);
let minutes = GetSlot(this, MINUTES);
let seconds = GetSlot(this, SECONDS);
let milliseconds = GetSlot(this, MILLISECONDS);
let microseconds = GetSlot(this, MICROSECONDS);
let nanoseconds = GetSlot(this, NANOSECONDS);
if (totalOf === undefined) throw new TypeError('options argument is required');
if (ES.Type(totalOf) === 'String') {
const stringParam = totalOf;
totalOf = ObjectCreate(null);
totalOf.unit = stringParam;
} else {
totalOf = ES.GetOptionsObject(totalOf);
}
let { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(totalOf);
const unit = ES.GetTemporalUnit(totalOf, 'unit', 'datetime', ES.REQUIRED);
let precalculatedPlainDateTime;
const plainDateTimeOrRelativeToWillBeUsed =
unit === 'year' ||
unit === 'month' ||
unit === 'week' ||
unit === 'day' ||
years !== 0 ||
months !== 0 ||
weeks !== 0 ||
days !== 0;
if (zonedRelativeTo && plainDateTimeOrRelativeToWillBeUsed) {
// Convert a ZonedDateTime relativeTo to PlainDate only if needed in one
// of the operations below, because the conversion is user visible
precalculatedPlainDateTime = ES.GetPlainDateTimeFor(
timeZoneRec,
GetSlot(zonedRelativeTo, INSTANT),
GetSlot(zonedRelativeTo, CALENDAR)
);
plainRelativeTo = ES.TemporalDateTimeToDate(precalculatedPlainDateTime);
}
const calendarRec = CalendarMethodRecord.CreateFromRelativeTo(plainRelativeTo, zonedRelativeTo, [
'dateAdd',
'dateUntil'
]);
// Convert larger units down to days
({ years, months, weeks, days } = ES.UnbalanceDateDurationRelative(
years,
months,
weeks,
days,
unit,
plainRelativeTo,
calendarRec
));
// If the unit we're totalling is smaller than `days`, convert days down to that unit.
let balanceResult;
if (zonedRelativeTo) {
const intermediate = ES.MoveRelativeZonedDateTime(
zonedRelativeTo,
calendarRec,
timeZoneRec,
years,
months,
weeks,
0,
precalculatedPlainDateTime
);
balanceResult = ES.BalancePossiblyInfiniteTimeDurationRelative(
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
unit,
intermediate,
timeZoneRec
);
} else {
balanceResult = ES.BalancePossiblyInfiniteTimeDuration(
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
unit
);
}
if (balanceResult === 'positive overflow') {
return Infinity;
} else if (balanceResult === 'negative overflow') {
return -Infinity;
}
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = balanceResult);
// Finally, truncate to the correct unit and calculate remainder
const { total } = ES.RoundDuration(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
1,
unit,
'trunc',
plainRelativeTo,
calendarRec,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
);
return total;
}
toString(options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
options = ES.GetOptionsObject(options);
const digits = ES.ToFractionalSecondDigits(options);
const roundingMode = ES.ToTemporalRoundingMode(options, 'trunc');
const smallestUnit = ES.GetTemporalUnit(options, 'smallestUnit', 'time', undefined);
if (smallestUnit === 'hour' || smallestUnit === 'minute') {
throw new RangeError('smallestUnit must be a time unit other than "hours" or "minutes"');
}
const { precision, unit, increment } = ES.ToSecondsStringPrecisionRecord(smallestUnit, digits);
let years = GetSlot(this, YEARS);
let months = GetSlot(this, MONTHS);
let weeks = GetSlot(this, WEEKS);
let days = GetSlot(this, DAYS);
let hours = GetSlot(this, HOURS);
let minutes = GetSlot(this, MINUTES);
let seconds = GetSlot(this, SECONDS);
let milliseconds = GetSlot(this, MILLISECONDS);
let microseconds = GetSlot(this, MICROSECONDS);
let nanoseconds = GetSlot(this, NANOSECONDS);
if (unit !== 'nanosecond' || increment !== 1) {
const largestUnit = ES.DefaultTemporalLargestUnit(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
);
({ seconds, milliseconds, microseconds, nanoseconds } = ES.RoundDuration(
0,
0,
0,
0,
0,
0,
seconds,
milliseconds,
microseconds,
nanoseconds,
increment,
unit,
roundingMode
));
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDuration(
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
largestUnit
));
}
return ES.TemporalDurationToString(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
precision
);
}
toJSON() {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
return ES.TemporalDurationToString(
GetSlot(this, YEARS),
GetSlot(this, MONTHS),
GetSlot(this, WEEKS),
GetSlot(this, DAYS),
GetSlot(this, HOURS),
GetSlot(this, MINUTES),
GetSlot(this, SECONDS),
GetSlot(this, MILLISECONDS),
GetSlot(this, MICROSECONDS),
GetSlot(this, NANOSECONDS)
);
}
toLocaleString(locales = undefined, options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeError('invalid receiver');
if (typeof Intl !== 'undefined' && typeof Intl.DurationFormat !== 'undefined') {
return new Intl.DurationFormat(locales, options).format(this);
}
console.warn('Temporal.Duration.prototype.toLocaleString() requires Intl.DurationFormat.');
return ES.TemporalDurationToString(
GetSlot(this, YEARS),
GetSlot(this, MONTHS),
GetSlot(this, WEEKS),
GetSlot(this, DAYS),
GetSlot(this, HOURS),
GetSlot(this, MINUTES),
GetSlot(this, SECONDS),
GetSlot(this, MILLISECONDS),
GetSlot(this, MICROSECONDS),
GetSlot(this, NANOSECONDS)
);
}
valueOf() {
ES.ValueOfThrows('Duration');
}
static from(item) {
if (ES.IsTemporalDuration(item)) {
return new Duration(
GetSlot(item, YEARS),
GetSlot(item, MONTHS),
GetSlot(item, WEEKS),
GetSlot(item, DAYS),
GetSlot(item, HOURS),
GetSlot(item, MINUTES),
GetSlot(item, SECONDS),
GetSlot(item, MILLISECONDS),
GetSlot(item, MICROSECONDS),
GetSlot(item, NANOSECONDS)
);
}
return ES.ToTemporalDuration(item);
}
static compare(one, two, options = undefined) {
one = ES.ToTemporalDuration(one);
two = ES.ToTemporalDuration(two);
options = ES.GetOptionsObject(options);
const y1 = GetSlot(one, YEARS);
const mon1 = GetSlot(one, MONTHS);
const w1 = GetSlot(one, WEEKS);
let d1 = GetSlot(one, DAYS);
let h1 = GetSlot(one, HOURS);
const min1 = GetSlot(one, MINUTES);
const s1 = GetSlot(one, SECONDS);
const ms1 = GetSlot(one, MILLISECONDS);
const µs1 = GetSlot(one, MICROSECONDS);
let ns1 = GetSlot(one, NANOSECONDS);
const y2 = GetSlot(two, YEARS);
const mon2 = GetSlot(two, MONTHS);
const w2 = GetSlot(two, WEEKS);
let d2 = GetSlot(two, DAYS);
let h2 = GetSlot(two, HOURS);
const min2 = GetSlot(two, MINUTES);
const s2 = GetSlot(two, SECONDS);
const ms2 = GetSlot(two, MILLISECONDS);
const µs2 = GetSlot(two, MICROSECONDS);
let ns2 = GetSlot(two, NANOSECONDS);
if (
y1 === y2 &&
mon1 === mon2 &&
w1 === w2 &&
d1 === d2 &&
h1 === h2 &&
min1 === min2 &&
s1 === s2 &&
ms1 === ms2 &&
µs1 === µs2 &&
ns1 === ns2
) {
return 0;
}
const { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(options);
const calendarUnitsPresent = y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0;
const calendarRec = CalendarMethodRecord.CreateFromRelativeTo(plainRelativeTo, zonedRelativeTo, ['dateAdd']);
if (zonedRelativeTo && (calendarUnitsPresent || d1 != 0 || d2 !== 0)) {
const instant = GetSlot(zonedRelativeTo, INSTANT);
const precalculatedPlainDateTime = ES.GetPlainDateTimeFor(timeZoneRec, instant, calendarRec.receiver);
const after1 = ES.AddZonedDateTime(
instant,
timeZoneRec,
calendarRec,
y1,
mon1,
w1,
d1,
h1,
min1,
s1,
ms1,
µs1,
ns1,
precalculatedPlainDateTime
);
const after2 = ES.AddZonedDateTime(
instant,
timeZoneRec,
calendarRec,
y2,
mon2,
w2,
d2,
h2,
min2,
s2,
ms2,
µs2,
ns2,
precalculatedPlainDateTime
);
return ES.ComparisonResult(after1.minus(after2).toJSNumber());
}
if (calendarUnitsPresent) {
// plainRelativeTo may be undefined, and if so Unbalance will throw
({ days: d1 } = ES.UnbalanceDateDurationRelative(y1, mon1, w1, d1, 'day', plainRelativeTo, calendarRec));
({ days: d2 } = ES.UnbalanceDateDurationRelative(y2, mon2, w2, d2, 'day', plainRelativeTo, calendarRec));
}
h1 = bigInt(h1).add(bigInt(d1).multiply(24));
h2 = bigInt(h2).add(bigInt(d2).multiply(24));
ns1 = ES.TotalDurationNanoseconds(h1, min1, s1, ms1, µs1, ns1);
ns2 = ES.TotalDurationNanoseconds(h2, min2, s2, ms2, µs2, ns2);
return ES.ComparisonResult(ns1.minus(ns2).toJSNumber());
}
}
MakeIntrinsicClass(Duration, 'Temporal.Duration');