Skip to content

Commit

Permalink
Editorial: Refactor duration adding operations to use CreateNegated
Browse files Browse the repository at this point in the history
Similar to DifferenceTemporal___, instead of picking a _sign_ of -1 or 1
and multiplying the duration components by it, create the Duration
instance and then pass it through CreateNegatedTemporalDuration if the
operation is ~subtract~.

This was already started in #2290, and brought into consistency everywhere
in this commit.

See: #2308
  • Loading branch information
ptomato authored and Ms2ger committed Sep 20, 2024
1 parent 2c9cfc2 commit b67cb35
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 48 deletions.
66 changes: 32 additions & 34 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4324,25 +4324,24 @@ export function AddDurationToOrSubtractDurationFromInstant(operation, instant, d
}

export function AddDurationToDate(operation, plainDate, durationLike, options) {
const sign = operation === 'subtract' ? -1 : 1;
const isoDate = TemporalObjectToISODateRecord(plainDate);
const calendar = GetSlot(plainDate, CALENDAR);

let duration = ToTemporalDurationRecord(durationLike);
let duration = ToTemporalDuration(durationLike);
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
const norm = TimeDuration.normalize(
sign * duration.hours,
sign * duration.minutes,
sign * duration.seconds,
sign * duration.milliseconds,
sign * duration.microseconds,
sign * duration.nanoseconds
GetSlot(duration, HOURS),
GetSlot(duration, MINUTES),
GetSlot(duration, SECONDS),
GetSlot(duration, MILLISECONDS),
GetSlot(duration, MICROSECONDS),
GetSlot(duration, NANOSECONDS)
);
const days = sign * duration.days + BalanceTimeDuration(norm, 'day').days;
const dateDuration = {
years: sign * duration.years,
months: sign * duration.months,
weeks: sign * duration.weeks,
days
years: GetSlot(duration, YEARS),
months: GetSlot(duration, MONTHS),
weeks: GetSlot(duration, WEEKS),
days: GetSlot(duration, DAYS) + BalanceTimeDuration(norm, 'day').days
};

options = GetOptionsObject(options);
Expand All @@ -4353,21 +4352,20 @@ export function AddDurationToDate(operation, plainDate, durationLike, options) {
}

export function AddDurationToOrSubtractDurationFromPlainDateTime(operation, dateTime, durationLike, options) {
const sign = operation === 'subtract' ? -1 : 1;
const { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
ToTemporalDurationRecord(durationLike);
let duration = ToTemporalDuration(durationLike);
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
options = GetOptionsObject(options);
const overflow = GetTemporalOverflowOption(options);

const calendar = GetSlot(dateTime, CALENDAR);

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 { year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = AddDateTime(
GetSlot(dateTime, ISO_YEAR),
Expand All @@ -4380,26 +4378,26 @@ export function AddDurationToOrSubtractDurationFromPlainDateTime(operation, date
GetSlot(dateTime, ISO_MICROSECOND),
GetSlot(dateTime, ISO_NANOSECOND),
calendar,
sign * years,
sign * months,
sign * weeks,
sign * days,
GetSlot(duration, YEARS),
GetSlot(duration, MONTHS),
GetSlot(duration, WEEKS),
GetSlot(duration, DAYS),
norm,
overflow
);
return CreateTemporalDateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar);
}

export function AddDurationToOrSubtractDurationFromPlainTime(operation, temporalTime, durationLike) {
const sign = operation === 'subtract' ? -1 : 1;
const { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ToTemporalDurationRecord(durationLike);
let duration = ToTemporalDuration(durationLike);
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
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)
);
let { hour, minute, second, millisecond, microsecond, nanosecond } = AddTime(
GetSlot(temporalTime, ISO_HOUR),
Expand Down
9 changes: 4 additions & 5 deletions spec/plaindate.html
Original file line number Diff line number Diff line change
Expand Up @@ -1092,14 +1092,13 @@ <h1>
<dd>It adds/subtracts _temporalDurationLike_ to/from _temporalDate_, returning a point in time that is in the future/past relative to _temporalDate_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _isoDate_ be TemporalObjectToISODateRecord(_temporalDate_).
1. Let _calendar_ be _temporalDate_.[[Calendar]].
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _norm_ be NormalizeTimeDuration(_sign_ × _duration_.[[Hours]], _sign_ × _duration_.[[Minutes]], _sign_ × _duration_.[[Seconds]], _sign_ × _duration_.[[Milliseconds]], _sign_ × _duration_.[[Microseconds]], _sign_ × _duration_.[[Nanoseconds]]).
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Let _norm_ be NormalizeTimeDuration(_duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _balanceResult_ be ! BalanceTimeDuration(_norm_, *"day"*).
1. Let _days_ be _sign_ × _duration_.[[Days]] + _balanceResult_.[[Days]].
1. Let _dateDuration_ be ? CreateDateDurationRecord(_sign_ × _duration_.[[Years]], _sign_ × _duration_.[[Months]], _sign_ × _duration_.[[Weeks]], _days_).
1. Let _dateDuration_ be ? CreateDateDurationRecord(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]] + _balanceResult_.[[Days]]).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _overflow_ be ? GetTemporalOverflowOption(_options_).
1. Let _result_ be ? CalendarDateAdd(_calendar_, _isoDate_, _dateDuration_, _overflow_).
Expand Down
8 changes: 4 additions & 4 deletions spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,12 @@ <h1>
<dd>It adds/subtracts _temporalDurationLike_ to/from _dateTime_, returning a point in time that is in the future/past relative to _datetime_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _overflow_ be ? GetTemporalOverflowOption(_options_).
1. Let _norm_ be NormalizeTimeDuration(_sign_ × _duration_.[[Hours]], _sign_ × _duration_.[[Minutes]], _sign_ × _duration_.[[Seconds]], _sign_ × _duration_.[[Milliseconds]], _sign_ × _duration_.[[Microseconds]], _sign_ × _duration_.[[Nanoseconds]]).
1. Let _result_ be ? AddDateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _dateTime_.[[Calendar]], _sign_ × _duration_.[[Years]], _sign_ × _duration_.[[Months]], _sign_ × _duration_.[[Weeks]], _sign_ × _duration_.[[Days]], _norm_, _overflow_).
1. Let _norm_ be NormalizeTimeDuration(_duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _result_ be ? AddDateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _dateTime_.[[Calendar]], _duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _norm_, _overflow_).
1. Assert: IsValidISODate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]]) is *true*.
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalDateTime(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]], _result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]], _dateTime_.[[Calendar]]).
Expand Down
6 changes: 3 additions & 3 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,9 @@ <h1>
<dd>It adds/subtracts _temporalDurationLike_ to/from _temporalTime_, returning a point in time that is in the future/past relative to _temporalTime_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _norm_ be NormalizeTimeDuration(_sign_ × _duration_.[[Hours]], _sign_ × _duration_.[[Minutes]], _sign_ × _duration_.[[Seconds]], _sign_ × _duration_.[[Milliseconds]], _sign_ × _duration_.[[Microseconds]], _sign_ × _duration_.[[Nanoseconds]]).
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Let _norm_ be NormalizeTimeDuration(_duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _result_ be AddTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _norm_).
1. Return ! CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
</emu-alg>
Expand Down
3 changes: 1 addition & 2 deletions spec/plainyearmonth.html
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ <h1>
</dl>
<emu-alg>
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, then
1. Set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _overflow_ be ? GetTemporalOverflowOption(_options_).
1. Let _sign_ be DurationSign(_duration_).
Expand Down

0 comments on commit b67cb35

Please sign in to comment.