Skip to content

Commit

Permalink
Editorial: Pass year/month/day to DifferencePlainDateTimeWithRounding
Browse files Browse the repository at this point in the history
Since #2758 we no longer need to keep this PlainDate object for passing to
calendar methods. This allows removing an unnecessary object creation that
implementations would be able to optimize out anyway because it's
unobservable.

Closes: #2875
  • Loading branch information
ptomato authored and Ms2ger committed Jun 14, 2024
1 parent 012fc7e commit 9ceb1bb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
8 changes: 6 additions & 2 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ export class Duration {

({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
ES.DifferencePlainDateTimeWithRounding(
plainRelativeTo,
GetSlot(plainRelativeTo, ISO_YEAR),
GetSlot(plainRelativeTo, ISO_MONTH),
GetSlot(plainRelativeTo, ISO_DAY),
0,
0,
0,
Expand Down Expand Up @@ -489,7 +491,9 @@ export class Duration {
const targetDate = ES.AddDate(calendarRec, plainRelativeTo, dateDuration);

const { total } = ES.DifferencePlainDateTimeWithRounding(
plainRelativeTo,
GetSlot(plainRelativeTo, ISO_YEAR),
GetSlot(plainRelativeTo, ISO_MONTH),
GetSlot(plainRelativeTo, ISO_DAY),
0,
0,
0,
Expand Down
12 changes: 6 additions & 6 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4269,7 +4269,9 @@ function RoundRelativeDuration(
}

export function DifferencePlainDateTimeWithRounding(
plainDate1,
y1,
mon1,
d1,
h1,
min1,
s1,
Expand All @@ -4292,9 +4294,6 @@ export function DifferencePlainDateTimeWithRounding(
roundingMode,
resolvedOptions
) {
const y1 = GetSlot(plainDate1, ISO_YEAR);
const mon1 = GetSlot(plainDate1, ISO_MONTH);
const d1 = GetSlot(plainDate1, ISO_DAY);
if (CompareISODateTime(y1, mon1, d1, h1, min1, s1, ms1, µs1, ns1, y2, mon2, d2, h2, min2, s2, ms2, µs2, ns2) == 0) {
return {
years: 0,
Expand Down Expand Up @@ -4619,11 +4618,12 @@ export function DifferenceTemporalPlainDateTime(operation, plainDateTime, other,
return new Duration();
}

const plainDate1 = TemporalDateTimeToDate(plainDateTime);
const calendarRec = new CalendarMethodRecord(calendar, ['dateAdd', 'dateUntil']);
const { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
DifferencePlainDateTimeWithRounding(
plainDate1,
GetSlot(plainDateTime, ISO_YEAR),
GetSlot(plainDateTime, ISO_MONTH),
GetSlot(plainDateTime, ISO_DAY),
GetSlot(plainDateTime, ISO_HOUR),
GetSlot(plainDateTime, ISO_MINUTE),
GetSlot(plainDateTime, ISO_SECOND),
Expand Down
4 changes: 2 additions & 2 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ <h1>Temporal.Duration.prototype.round ( _roundTo_ )</h1>
1. Let _targetTime_ be AddTime(0, 0, 0, 0, 0, 0, _norm_).
1. Let _dateDuration_ be ? CreateTemporalDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]] + _targetTime_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _targetDate_ be ? AddDate(_calendarRec_, _plainRelativeTo_, _dateDuration_).
1. Let _roundRecord_ be ? DifferencePlainDateTimeWithRounding(_plainRelativeTo_, 0, 0, 0, 0, 0, 0, _targetDate_.[[ISOYear]], _targetDate_.[[ISOMonth]], _targetDate_.[[ISODay]], _targetTime_.[[Hours]], _targetTime_.[[Minutes]], _targetTime_.[[Seconds]], _targetTime_.[[Milliseconds]], _targetTime_.[[Microseconds]], _targetTime_.[[Nanoseconds]], _calendarRec_, _largestUnit_, _roundingIncrement_, _smallestUnit_, _roundingMode_, _emptyOptions_).
1. Let _roundRecord_ be ? DifferencePlainDateTimeWithRounding(_plainRelativeTo_.[[ISOYear]], _plainRelativeTo_.[[ISOMonth]], _plainRelativeTo_.[[ISODay]], 0, 0, 0, 0, 0, 0, _targetDate_.[[ISOYear]], _targetDate_.[[ISOMonth]], _targetDate_.[[ISODay]], _targetTime_.[[Hours]], _targetTime_.[[Minutes]], _targetTime_.[[Seconds]], _targetTime_.[[Milliseconds]], _targetTime_.[[Microseconds]], _targetTime_.[[Nanoseconds]], _calendarRec_, _largestUnit_, _roundingIncrement_, _smallestUnit_, _roundingMode_, _emptyOptions_).
1. Let _roundResult_ be _roundRecord_.[[DurationRecord]].
1. Else,
1. If _calendarUnitsPresent_ is *true*, or IsCalendarUnit(_largestUnit_) is *true*, or IsCalendarUnit(_smallestUnit_) is *true*, throw a *RangeError* exception.
Expand Down Expand Up @@ -531,7 +531,7 @@ <h1>Temporal.Duration.prototype.total ( _totalOf_ )</h1>
1. Let _targetTime_ be AddTime(0, 0, 0, 0, 0, 0, _norm_).
1. Let _dateDuration_ be ? CreateTemporalDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]] + _targetTime_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _targetDate_ be ? AddDate(_calendarRec_, _plainRelativeTo_, _dateDuration_).
1. Let _roundRecord_ be ? DifferencePlainDateTimeWithRounding(_plainRelativeTo_, 0, 0, 0, 0, 0, 0, _targetDate_.[[ISOYear]], _targetDate_.[[ISOMonth]], _targetDate_.[[ISODay]], _targetTime_.[[Hours]], _targetTime_.[[Minutes]], _targetTime_.[[Seconds]], _targetTime_.[[Milliseconds]], _targetTime_.[[Microseconds]], _targetTime_.[[Nanoseconds]], _calendarRec_, _unit_, 1, _unit_, *"trunc"*, _emptyOptions_).
1. Let _roundRecord_ be ? DifferencePlainDateTimeWithRounding(_plainRelativeTo_.[[ISOYear]], _plainRelativeTo_.[[ISOMonth]], _plainRelativeTo_.[[ISODay]], 0, 0, 0, 0, 0, 0, _targetDate_.[[ISOYear]], _targetDate_.[[ISOMonth]], _targetDate_.[[ISODay]], _targetTime_.[[Hours]], _targetTime_.[[Minutes]], _targetTime_.[[Seconds]], _targetTime_.[[Milliseconds]], _targetTime_.[[Microseconds]], _targetTime_.[[Nanoseconds]], _calendarRec_, _unit_, 1, _unit_, *"trunc"*, _emptyOptions_).
1. Else,
1. If _duration_.[[Years]] &ne; 0, or _duration_.[[Months]] &ne; 0, or _duration_.[[Weeks]] &ne; 0, or IsCalendarUnit(_unit_) is *true*, throw a *RangeError* exception.
1. Let _normWithDays_ be ? Add24HourDaysToNormalizedTimeDuration(_duration_.[[Days]], _norm_).
Expand Down
11 changes: 5 additions & 6 deletions spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,9 @@ <h1>
<emu-clause id="sec-temporal-differenceplaindatetimewithrounding" type="abstract operation">
<h1>
DifferencePlainDateTimeWithRounding (
_plainDate1_: a Temporal.PlainDate,
_y1_: an integer,
_mon1_: an integer,
_d1_: an integer,
_h1_: an integer in the inclusive interval from 0 to 23,
_min1_: an integer in the inclusive interval from 0 to 59,
_s1_: an integer in the inclusive interval from 0 to 59,
Expand Down Expand Up @@ -1344,10 +1346,8 @@ <h1>
<dd></dd>
</dl>
<emu-alg>
1. Assert: IsValidISODate(_y1_, _mon1_, _d1_) is *true*.
1. Assert: IsValidISODate(_y2_, _mon2_, _d2_) is *true*.
1. Let _y1_ be _plainDate1_.[[ISOYear]].
1. Let _mon1_ be _plainDate1_.[[ISOMonth]].
1. Let _d1_ be _plainDate1_.[[ISODay]].
1. If CompareISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_) = 0, then
1. Let _durationRecord_ be CreateDurationRecord(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
1. Return the Record { [[DurationRecord]]: _durationRecord_, [[Total]]: 0 }.
Expand Down Expand Up @@ -1388,9 +1388,8 @@ <h1>
1. Set _datePartsIdentical_ to *true*.
1. If _datePartsIdentical_ is *true*, and _dateTime_.[[ISOHour]] = _other_.[[ISOHour]], and _dateTime_.[[ISOMinute]] = _other_.[[ISOMinute]], and _dateTime_.[[ISOSecond]] = _other_.[[ISOSecond]], and _dateTime_.[[ISOMillisecond]] = _other_.[[ISOMillisecond]], and _dateTime_.[[ISOMicrosecond]] = _other_.[[ISOMicrosecond]], and _dateTime_.[[ISONanosecond]] = _other_.[[ISONanosecond]], then
1. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
1. Let _plainDate_ be ! CreateTemporalDate(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[Calendar]]).
1. Let _calendarRec_ be ? CreateCalendarMethodsRecord(_dateTime_.[[Calendar]], « ~date-add~, ~date-until~ »).
1. Let _resultRecord_ be ? DifferencePlainDateTimeWithRounding(_plainDate_, _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _other_.[[ISOYear]], _other_.[[ISOMonth]], _other_.[[ISODay]], _other_.[[ISOHour]], _other_.[[ISOMinute]], _other_.[[ISOSecond]], _other_.[[ISOMillisecond]], _other_.[[ISOMicrosecond]], _other_.[[ISONanosecond]], _calendarRec_, _settings_.[[LargestUnit]], _settings_.[[RoundingIncrement]], _settings_.[[SmallestUnit]], _settings_.[[RoundingMode]], _resolvedOptions_).
1. Let _resultRecord_ be ? DifferencePlainDateTimeWithRounding(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _other_.[[ISOYear]], _other_.[[ISOMonth]], _other_.[[ISODay]], _other_.[[ISOHour]], _other_.[[ISOMinute]], _other_.[[ISOSecond]], _other_.[[ISOMillisecond]], _other_.[[ISOMicrosecond]], _other_.[[ISONanosecond]], _calendarRec_, _settings_.[[LargestUnit]], _settings_.[[RoundingIncrement]], _settings_.[[SmallestUnit]], _settings_.[[RoundingMode]], _resolvedOptions_).
1. Let _result_ be _resultRecord_.[[DurationRecord]].
1. Return ? CreateTemporalDuration(_sign_ &times; _result_.[[Years]], _sign_ &times; _result_.[[Months]], _sign_ &times; _result_.[[Weeks]], _sign_ &times; _result_.[[Days]], _sign_ &times; _result_.[[Hours]], _sign_ &times; _result_.[[Minutes]], _sign_ &times; _result_.[[Seconds]], _sign_ &times; _result_.[[Milliseconds]], _sign_ &times; _result_.[[Microseconds]], _sign_ &times; _result_.[[Nanoseconds]]).
</emu-alg>
Expand Down

0 comments on commit 9ceb1bb

Please sign in to comment.