Skip to content

Commit b63dd3a

Browse files
ptomato12wrigja
authored andcommitted
Update reference code with DifferenceInstant spec text changes
This implements the changes from #2400 in the reference polyfill. It doesn't change any of the results, but moves a BalanceDuration step into DifferenceInstant. Pre-balancing the nanosecond difference into s, ms, µs, and ns allows us to pass JS numbers to RoundDuration although the spec text uses integers there. UPSTREAM_COMMIT=35694b41c2019cbe805178cbb69e2bb5b68a5ee9
1 parent d043073 commit b63dd3a

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

lib/ecmascript.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,21 +4127,35 @@ function DifferenceInstant(
41274127
ns1: JSBI,
41284128
ns2: JSBI,
41294129
increment: number,
4130-
unit: keyof typeof nsPerTimeUnit,
4130+
smallestUnit: keyof typeof nsPerTimeUnit,
4131+
largestUnit: keyof typeof nsPerTimeUnit,
41314132
roundingMode: Temporal.RoundingMode
41324133
) {
41334134
const diff = JSBI.subtract(ns2, ns1);
41344135

4135-
const remainder = JSBI.remainder(diff, JSBI.BigInt(86400e9));
4136-
const wholeDays = JSBI.subtract(diff, remainder);
4137-
const roundedRemainder = RoundNumberToIncrement(remainder, nsPerTimeUnit[unit] * increment, roundingMode);
4138-
const roundedDiff = JSBI.add(wholeDays, roundedRemainder);
4136+
let hours = 0;
4137+
let minutes = 0;
4138+
let nanoseconds = JSBI.toNumber(JSBI.remainder(diff, THOUSAND));
4139+
let microseconds = JSBI.toNumber(JSBI.remainder(JSBI.divide(diff, THOUSAND), THOUSAND));
4140+
let milliseconds = JSBI.toNumber(JSBI.remainder(JSBI.divide(diff, MILLION), THOUSAND));
4141+
let seconds = JSBI.toNumber(JSBI.divide(diff, BILLION));
41394142

4140-
const nanoseconds = JSBI.toNumber(JSBI.remainder(roundedDiff, THOUSAND));
4141-
const microseconds = JSBI.toNumber(JSBI.remainder(JSBI.divide(roundedDiff, THOUSAND), THOUSAND));
4142-
const milliseconds = JSBI.toNumber(JSBI.remainder(JSBI.divide(roundedDiff, MILLION), THOUSAND));
4143-
const seconds = JSBI.toNumber(JSBI.divide(roundedDiff, BILLION));
4144-
return { seconds, milliseconds, microseconds, nanoseconds };
4143+
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = RoundDuration(
4144+
0,
4145+
0,
4146+
0,
4147+
0,
4148+
0,
4149+
0,
4150+
seconds,
4151+
milliseconds,
4152+
microseconds,
4153+
nanoseconds,
4154+
increment,
4155+
smallestUnit,
4156+
roundingMode
4157+
));
4158+
return BalanceDuration(0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, largestUnit);
41454159
}
41464160

41474161
function DifferenceISODateTime(
@@ -4335,24 +4349,14 @@ export function DifferenceTemporalInstant(
43354349
const roundingIncrement = ToTemporalRoundingIncrement(options, MAX_DIFFERENCE_INCREMENTS[smallestUnit], false);
43364350
const onens = GetSlot(first, EPOCHNANOSECONDS);
43374351
const twons = GetSlot(second, EPOCHNANOSECONDS);
4338-
let { seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
4352+
let { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
43394353
onens,
43404354
twons,
43414355
roundingIncrement,
43424356
smallestUnit,
4357+
largestUnit,
43434358
roundingMode
43444359
);
4345-
let hours, minutes;
4346-
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = BalanceDuration(
4347-
0,
4348-
0,
4349-
0,
4350-
seconds,
4351-
milliseconds,
4352-
microseconds,
4353-
nanoseconds,
4354-
largestUnit
4355-
));
43564360
const Duration = GetIntrinsic('%Temporal.Duration%');
43574361
return new Duration(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
43584362
}
@@ -4695,26 +4699,17 @@ export function DifferenceTemporalZonedDateTime(
46954699
months = 0;
46964700
weeks = 0;
46974701
days = 0;
4698-
({ seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
4702+
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
46994703
ns1,
47004704
ns2,
47014705
roundingIncrement,
47024706
// TODO this doesn't type-check as it includes >= day-size units
47034707
// This is probably safe as the typing for ToSmallestTemporalUnit isn't
47044708
// very good.
47054709
smallestUnit as any,
4710+
largestUnit,
47064711
roundingMode
47074712
));
4708-
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = BalanceDuration(
4709-
0,
4710-
0,
4711-
0,
4712-
seconds,
4713-
milliseconds,
4714-
microseconds,
4715-
nanoseconds,
4716-
largestUnit
4717-
));
47184713
} else {
47194714
const timeZone = GetSlot(zonedDateTime, TIME_ZONE);
47204715
if (!TimeZoneEquals(timeZone, GetSlot(other, TIME_ZONE))) {
@@ -4952,23 +4947,14 @@ function AddDuration(
49524947
months = 0;
49534948
weeks = 0;
49544949
days = 0;
4955-
({ seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
4950+
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = DifferenceInstant(
49564951
GetSlot(relativeTo, EPOCHNANOSECONDS),
49574952
endNs,
49584953
1,
49594954
'nanosecond',
4955+
largestUnit,
49604956
'halfExpand'
49614957
));
4962-
({ hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = BalanceDuration(
4963-
0,
4964-
0,
4965-
0,
4966-
seconds,
4967-
milliseconds,
4968-
microseconds,
4969-
nanoseconds,
4970-
largestUnit
4971-
));
49724958
} else {
49734959
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
49744960
DifferenceZonedDateTime(

0 commit comments

Comments
 (0)