From b8dd74aa5fdd183f1cfdfc647371f5655c2e984f Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 3 Mar 2023 15:34:14 -0800 Subject: [PATCH] Normative: Don't convert relativeTo to PlainDate twice in Duration.compare 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 --- polyfill/lib/duration.mjs | 3 ++- spec/duration.html | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/polyfill/lib/duration.mjs b/polyfill/lib/duration.mjs index 50c8d0cf26..5666e8e7da 100644 --- a/polyfill/lib/duration.mjs +++ b/polyfill/lib/duration.mjs @@ -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); @@ -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)); } diff --git a/spec/duration.html b/spec/duration.html index da61977233..cdfc6c8f9a 100644 --- a/spec/duration.html +++ b/spec/duration.html @@ -98,6 +98,7 @@

Temporal.Duration.compare ( _one_, _two_ [ , _options_ ] )

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]] ≠ 0, or _two_.[[Years]] ≠ 0, or _one_.[[Months]] ≠ 0, or _two_.[[Months]] ≠ 0, or _one_.[[Weeks]] ≠ 0, or _two_.[[Weeks]] ≠ 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]].