Skip to content

Commit

Permalink
Polyfill: Bring AddDurationToOrSubtractDurationFrom in line with spec…
Browse files Browse the repository at this point in the history
… text

Implements the previous commit in the reference code.
  • Loading branch information
ptomato committed Sep 11, 2024
1 parent 1ef90bb commit 0a24165
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -754,18 +754,6 @@ export function ToTemporalPartialDurationRecord(temporalDurationLike) {
return result;
}

export function ToLimitedTemporalDuration(item, disallowedProperties) {
let record = ToTemporalDurationRecord(item);
for (const property of disallowedProperties) {
if (record[property] !== 0) {
throw new RangeError(
`Duration field ${property} not supported by Temporal.Instant. Try Temporal.ZonedDateTime instead.`
);
}
}
return record;
}

export function TemporalObjectToISODateRecord(temporalObject) {
return {
year: GetSlot(temporalObject, ISO_YEAR),
Expand Down Expand Up @@ -4230,20 +4218,21 @@ export function AddDurations(operation, duration, other) {
}

export function AddDurationToOrSubtractDurationFromInstant(operation, instant, durationLike) {
const sign = operation === 'subtract' ? -1 : 1;
const { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ToLimitedTemporalDuration(durationLike, [
'years',
'months',
'weeks',
'days'
]);
let duration = ToTemporalDuration(durationLike);
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
const largestUnit = DefaultTemporalLargestUnit(duration);
if (IsCalendarUnit(largestUnit) || largestUnit === 'day') {
throw new RangeError(
`Duration field ${largestUnit} not supported by Temporal.Instant. Try Temporal.ZonedDateTime instead.`
);
}
const norm = TimeDuration.normalize(
sign * hours,
sign * minutes,
sign * seconds,
sign * milliseconds,
sign * microseconds,
sign * nanoseconds
GetSlot(duration, HOURS),
GetSlot(duration, MINUTES),
GetSlot(duration, SECONDS),
GetSlot(duration, MILLISECONDS),
GetSlot(duration, MICROSECONDS),
GetSlot(duration, NANOSECONDS)
);
const ns = AddInstant(GetSlot(instant, EPOCHNANOSECONDS), norm);
const Instant = GetIntrinsic('%Temporal.Instant%');
Expand Down

0 comments on commit 0a24165

Please sign in to comment.