Skip to content

Commit

Permalink
Normative: Don't convert relativeTo to PlainDate twice in Duration.co…
Browse files Browse the repository at this point in the history
…mpare

We call UnbalanceDurationRelative twice with the same relativeTo object.
UnbalanceDurationRelative only uses plain or undefined relativeTo, not
zoned, so it will convert it with ToTemporalDate. No need to do this
twice; pre-convert it before the first call.

See: #2247
  • Loading branch information
ptomato committed Jun 20, 2023
1 parent bd1ae6b commit 6f07bda
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ export class Duration {
one = ES.ToTemporalDuration(one);
two = ES.ToTemporalDuration(two);
options = ES.GetOptionsObject(options);
const relativeTo = ES.ToRelativeTemporalObject(options);
let relativeTo = ES.ToRelativeTemporalObject(options);
const y1 = GetSlot(one, YEARS);
const mon1 = GetSlot(one, MONTHS);
const w1 = GetSlot(one, WEEKS);
Expand All @@ -573,6 +573,7 @@ export class Duration {
const shift1 = ES.CalculateOffsetShift(relativeTo, y1, mon1, w1, d1);
const shift2 = ES.CalculateOffsetShift(relativeTo, y2, mon2, w2, d2);
if (y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0) {
if (ES.IsTemporalZonedDateTime(relativeTo)) relativeTo = ES.ToTemporalDate(relativeTo);
({ days: d1 } = ES.UnbalanceDateDurationRelative(y1, mon1, w1, d1, 'day', relativeTo));
({ days: d2 } = ES.UnbalanceDateDurationRelative(y2, mon2, w2, d2, 'day', relativeTo));
}
Expand Down
1 change: 1 addition & 0 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ <h1>Temporal.Duration.compare ( _one_, _two_ [ , _options_ ] )</h1>
1. Let _shift1_ be ? CalculateOffsetShift(_relativeTo_, _one_.[[Years]], _one_.[[Months]], _one_.[[Weeks]], _one_.[[Days]]).
1. Let _shift2_ be ? CalculateOffsetShift(_relativeTo_, _two_.[[Years]], _two_.[[Months]], _two_.[[Weeks]], _two_.[[Days]]).
1. If _one_.[[Years]] &ne; 0, or _two_.[[Years]] &ne; 0, or _one_.[[Months]] &ne; 0, or _two_.[[Months]] &ne; 0, or _one_.[[Weeks]] &ne; 0, or _two_.[[Weeks]] &ne; 0, then
1. If _relativeTo_ is an Object with an [[InitializedTemporalZonedDateTime]] internal slot, set _relativeTo_ to ? ToTemporalDate(_relativeTo_).
1. Let _unbalanceResult1_ be ? UnbalanceDateDurationRelative(_one_.[[Years]], _one_.[[Months]], _one_.[[Weeks]], _one_.[[Days]], *"day"*, _relativeTo_).
1. Let _unbalanceResult2_ be ? UnbalanceDateDurationRelative(_two_.[[Years]], _two_.[[Months]], _two_.[[Weeks]], _two_.[[Days]], *"day"*, _relativeTo_).
1. Let _days1_ be _unbalanceResult1_.[[Days]].
Expand Down

0 comments on commit 6f07bda

Please sign in to comment.