2
2
3
3
import * as ES from './ecmascript.mjs' ;
4
4
import { MakeIntrinsicClass } from './intrinsicclass.mjs' ;
5
+ import { MethodRecord } from './methodrecord.mjs' ;
5
6
import {
6
7
YEARS ,
7
8
MONTHS ,
@@ -308,13 +309,40 @@ export class Duration {
308
309
plainRelativeTo = ES . TemporalDateTimeToDate ( dt ) ;
309
310
}
310
311
312
+ let calendar , calendarRec ;
313
+ if ( zonedRelativeTo ) {
314
+ calendar = GetSlot ( zonedRelativeTo , CALENDAR ) ;
315
+ } else if ( plainRelativeTo ) {
316
+ calendar = GetSlot ( plainRelativeTo , CALENDAR ) ;
317
+ }
318
+ if ( calendar ) {
319
+ calendarRec = new MethodRecord ( calendar ) ;
320
+ if (
321
+ years !== 0 ||
322
+ months !== 0 ||
323
+ weeks !== 0 ||
324
+ largestUnit === 'year' ||
325
+ largestUnit === 'month' ||
326
+ largestUnit === 'week' ||
327
+ smallestUnit === 'year' ||
328
+ smallestUnit === 'month' ||
329
+ smallestUnit === 'week'
330
+ ) {
331
+ calendarRec . lookup ( 'dateAdd' ) ;
332
+ }
333
+ if ( largestUnit === 'year' || ( largestUnit === 'month' && years !== 0 ) || smallestUnit === 'year' ) {
334
+ calendarRec . lookup ( 'dateUntil' ) ;
335
+ }
336
+ }
337
+
311
338
( { years, months, weeks, days } = ES . UnbalanceDurationRelative (
312
339
years ,
313
340
months ,
314
341
weeks ,
315
342
days ,
316
343
largestUnit ,
317
- plainRelativeTo
344
+ plainRelativeTo ,
345
+ calendarRec
318
346
) ) ;
319
347
( { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
320
348
ES . RoundDuration (
@@ -332,6 +360,7 @@ export class Duration {
332
360
smallestUnit ,
333
361
roundingMode ,
334
362
plainRelativeTo ,
363
+ calendarRec ,
335
364
zonedRelativeTo ,
336
365
timeZoneRec
337
366
) ) ;
@@ -352,6 +381,7 @@ export class Duration {
352
381
smallestUnit ,
353
382
roundingMode ,
354
383
zonedRelativeTo ,
384
+ calendarRec ,
355
385
timeZoneRec
356
386
) ) ;
357
387
}
@@ -373,7 +403,8 @@ export class Duration {
373
403
weeks ,
374
404
days ,
375
405
largestUnit ,
376
- plainRelativeTo
406
+ plainRelativeTo ,
407
+ calendarRec
377
408
) ) ;
378
409
379
410
return new Duration ( years , months , weeks , days , hours , minutes , seconds , milliseconds , microseconds , nanoseconds ) ;
@@ -417,12 +448,36 @@ export class Duration {
417
448
plainRelativeTo = ES . TemporalDateTimeToDate ( dt ) ;
418
449
}
419
450
451
+ let calendar , calendarRec ;
452
+ if ( zonedRelativeTo ) {
453
+ calendar = GetSlot ( zonedRelativeTo , CALENDAR ) ;
454
+ } else if ( plainRelativeTo ) {
455
+ calendar = GetSlot ( plainRelativeTo , CALENDAR ) ;
456
+ }
457
+ if ( calendar ) {
458
+ calendarRec = new MethodRecord ( calendar ) ;
459
+ if ( years !== 0 || months !== 0 || weeks !== 0 || unit === 'year' || unit === 'month' || unit === 'week' ) {
460
+ calendarRec . lookup ( 'dateAdd' ) ;
461
+ }
462
+ if ( unit === 'year' || ( unit === 'month' && years !== 0 ) ) {
463
+ calendarRec . lookup ( 'dateUntil' ) ;
464
+ }
465
+ }
466
+
420
467
// Convert larger units down to days
421
- ( { years, months, weeks, days } = ES . UnbalanceDurationRelative ( years , months , weeks , days , unit , plainRelativeTo ) ) ;
468
+ ( { years, months, weeks, days } = ES . UnbalanceDurationRelative (
469
+ years ,
470
+ months ,
471
+ weeks ,
472
+ days ,
473
+ unit ,
474
+ plainRelativeTo ,
475
+ calendarRec
476
+ ) ) ;
422
477
// If the unit we're totalling is smaller than `days`, convert days down to that unit.
423
478
let intermediate ;
424
479
if ( zonedRelativeTo ) {
425
- intermediate = ES . MoveRelativeZonedDateTime ( zonedRelativeTo , timeZoneRec , years , months , weeks , 0 ) ;
480
+ intermediate = ES . MoveRelativeZonedDateTime ( zonedRelativeTo , calendarRec , timeZoneRec , years , months , weeks , 0 ) ;
426
481
}
427
482
let balanceResult = ES . BalancePossiblyInfiniteDuration (
428
483
days ,
@@ -458,6 +513,7 @@ export class Duration {
458
513
unit ,
459
514
'trunc' ,
460
515
plainRelativeTo ,
516
+ calendarRec ,
461
517
zonedRelativeTo ,
462
518
timeZoneRec
463
519
) ;
@@ -550,15 +606,20 @@ export class Duration {
550
606
551
607
const calendarUnitsPresent = y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0 ;
552
608
609
+ let calendarRec ;
610
+ if ( relativeTo ) {
611
+ calendarRec = new MethodRecord ( GetSlot ( relativeTo , CALENDAR ) ) ;
612
+ if ( calendarUnitsPresent ) calendarRec . lookup ( 'dateAdd' ) ;
613
+ }
614
+
553
615
if ( ES . IsTemporalZonedDateTime ( relativeTo ) && ( calendarUnitsPresent || d1 != 0 || d2 !== 0 ) ) {
554
616
const instant = GetSlot ( relativeTo , INSTANT ) ;
555
- const calendar = GetSlot ( relativeTo , CALENDAR ) ;
556
- const precalculatedDateTime = ES . GetPlainDateTimeFor ( timeZoneRec , instant , calendar ) ;
617
+ const precalculatedDateTime = ES . GetPlainDateTimeFor ( timeZoneRec , instant , calendarRec . receiver ) ;
557
618
558
619
const after1 = ES . AddZonedDateTime (
559
620
instant ,
560
621
timeZoneRec ,
561
- calendar ,
622
+ calendarRec ,
562
623
y1 ,
563
624
mon1 ,
564
625
w1 ,
@@ -574,7 +635,7 @@ export class Duration {
574
635
const after2 = ES . AddZonedDateTime (
575
636
instant ,
576
637
timeZoneRec ,
577
- calendar ,
638
+ calendarRec ,
578
639
y2 ,
579
640
mon2 ,
580
641
w2 ,
@@ -592,8 +653,8 @@ export class Duration {
592
653
593
654
if ( calendarUnitsPresent ) {
594
655
// relativeTo is PlainDate or undefined
595
- ( { days : d1 } = ES . UnbalanceDurationRelative ( y1 , mon1 , w1 , d1 , 'day' , relativeTo ) ) ;
596
- ( { days : d2 } = ES . UnbalanceDurationRelative ( y2 , mon2 , w2 , d2 , 'day' , relativeTo ) ) ;
656
+ ( { days : d1 } = ES . UnbalanceDurationRelative ( y1 , mon1 , w1 , d1 , 'day' , relativeTo , calendarRec ) ) ;
657
+ ( { days : d2 } = ES . UnbalanceDurationRelative ( y2 , mon2 , w2 , d2 , 'day' , relativeTo , calendarRec ) ) ;
597
658
}
598
659
ns1 = ES . TotalDurationNanoseconds ( d1 , h1 , min1 , s1 , ms1 , µs1 , ns1 ) ;
599
660
ns2 = ES . TotalDurationNanoseconds ( d2 , h2 , min2 , s2 , ms2 , µs2 , ns2 ) ;
0 commit comments