From c04b417f6657395f4dea3e6cd77df60ec48f9fbc Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Wed, 13 Sep 2023 16:06:51 -0700 Subject: [PATCH] Temporal: Precalculate PlainDateTime from ZonedDateTime in more places --- .../prototype/add/order-of-operations.js | 5 +-- ...tetime-nanoseconds-to-days-range-errors.js | 9 ++-- .../prototype/round/order-of-operations.js | 4 -- ...tetime-nanoseconds-to-days-range-errors.js | 3 ++ .../prototype/subtract/order-of-operations.js | 5 +-- ...tetime-nanoseconds-to-days-range-errors.js | 9 ++-- .../prototype/total/order-of-operations.js | 44 ++++++++++++++----- ...tetime-nanoseconds-to-days-range-errors.js | 3 ++ .../prototype/since/order-of-operations.js | 8 +--- .../prototype/until/order-of-operations.js | 8 +--- 10 files changed, 54 insertions(+), 44 deletions(-) diff --git a/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js index 0cb316f5c75..c459a25e53e 100644 --- a/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/add/order-of-operations.js @@ -320,9 +320,10 @@ const expectedOpsForZonedRelativeTo = expected.concat([ "call options.relativeTo.timeZone.getPossibleInstantsFor", "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", - // AddDuration → AddZonedDateTime 1 + // AddDuration "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", + // AddDuration → AddZonedDateTime 1 "get options.relativeTo.calendar.dateAdd", "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", @@ -337,8 +338,6 @@ const expectedOpsForZonedRelativeTo = expected.concat([ // AddDuration → DifferenceZonedDateTime "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // AddDuration → DifferenceZonedDateTime → DifferenceISODateTime "get options.relativeTo.calendar.dateUntil", "call options.relativeTo.calendar.dateUntil", diff --git a/test/built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js b/test/built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js index ff3aed12f70..0ca8be8c111 100644 --- a/test/built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js +++ b/test/built-ins/Temporal/Duration/prototype/add/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js @@ -46,8 +46,7 @@ let zdt = new Temporal.ZonedDateTime( [epochInstant], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, @@ -74,8 +73,7 @@ zdt = new Temporal.ZonedDateTime( [epochInstant], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, @@ -103,8 +101,7 @@ zdt = new Temporal.ZonedDateTime( [new Temporal.Instant(-4n)], // Returned for NanosecondsToDays step 18.a, setting _oneDayFartherNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, diff --git a/test/built-ins/Temporal/Duration/prototype/round/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/round/order-of-operations.js index 8b6cc03d33a..5d7e8f32109 100644 --- a/test/built-ins/Temporal/Duration/prototype/round/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/round/order-of-operations.js @@ -408,8 +408,6 @@ const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([ "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", @@ -456,8 +454,6 @@ const expectedOpsForUnbalanceRoundBalance = expectedOpsForZonedRelativeTo.concat "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // No user code calls in UnbalanceDurationRelative // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", // 8. "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor diff --git a/test/built-ins/Temporal/Duration/prototype/round/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js b/test/built-ins/Temporal/Duration/prototype/round/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js index 08d50e84567..0dbb8bcd463 100644 --- a/test/built-ins/Temporal/Duration/prototype/round/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js +++ b/test/built-ins/Temporal/Duration/prototype/round/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js @@ -43,6 +43,7 @@ let zdt = new Temporal.ZonedDateTime( timeZoneSubstituteValues( [[epochInstant]], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ [ + TemporalHelpers.SUBSTITUTE_SKIP, // Pre-conversion in Duration.p.round dayNs - 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ -dayNs + 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] @@ -63,6 +64,7 @@ zdt = new Temporal.ZonedDateTime( timeZoneSubstituteValues( [[epochInstant]], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ [ + TemporalHelpers.SUBSTITUTE_SKIP, // Pre-conversion in Duration.p.round -dayNs + 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ dayNs - 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] @@ -86,6 +88,7 @@ zdt = new Temporal.ZonedDateTime( [new Temporal.Instant(-4n)], // Returned for NanosecondsToDays step 18.a, setting _oneDayFartherNs_ ], [ + TemporalHelpers.SUBSTITUTE_SKIP, // Pre-conversion in Duration.p.round dayNs - 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ -dayNs + 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js index 3c421fff54d..f9cdbb58918 100644 --- a/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/subtract/order-of-operations.js @@ -320,9 +320,10 @@ const expectedOpsForZonedRelativeTo = expected.concat([ "call options.relativeTo.timeZone.getPossibleInstantsFor", "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", - // AddDuration → AddZonedDateTime 1 + // AddDuration "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", + // AddDuration → AddZonedDateTime 1 "get options.relativeTo.calendar.dateAdd", "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", @@ -337,8 +338,6 @@ const expectedOpsForZonedRelativeTo = expected.concat([ // AddDuration → DifferenceZonedDateTime "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // AddDuration → DifferenceZonedDateTime → DifferenceISODateTime "get options.relativeTo.calendar.dateUntil", "call options.relativeTo.calendar.dateUntil", diff --git a/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js b/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js index 3e1e54e67c7..01976b5a043 100644 --- a/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js +++ b/test/built-ins/Temporal/Duration/prototype/subtract/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js @@ -46,8 +46,7 @@ let zdt = new Temporal.ZonedDateTime( [epochInstant], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, @@ -73,8 +72,7 @@ zdt = new Temporal.ZonedDateTime( [epochInstant], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, @@ -101,8 +99,7 @@ zdt = new Temporal.ZonedDateTime( [new Temporal.Instant(-4n)], // Returned for NanosecondsToDays step 18.a, setting _oneDayFartherNs_ ], [ - // Behave normally in 4 calls made prior to NanosecondsToDays - TemporalHelpers.SUBSTITUTE_SKIP, + // Behave normally in 3 calls made prior to NanosecondsToDays TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, TemporalHelpers.SUBSTITUTE_SKIP, diff --git a/test/built-ins/Temporal/Duration/prototype/total/order-of-operations.js b/test/built-ins/Temporal/Duration/prototype/total/order-of-operations.js index 8ad302108e0..3a8ddbd129e 100644 --- a/test/built-ins/Temporal/Duration/prototype/total/order-of-operations.js +++ b/test/built-ins/Temporal/Duration/prototype/total/order-of-operations.js @@ -290,8 +290,6 @@ const expectedOpsForMinimalYearRoundingZoned = expectedOpsForZonedRelativeTo.con ], [ // code path through RoundDuration that rounds to the nearest year: // MoveRelativeZonedDateTime → AddDaysToZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.timeZone.getPossibleInstantsFor", "call options.relativeTo.timeZone.getPossibleInstantsFor", "get options.relativeTo.calendar.dateAdd", // 7.c @@ -315,8 +313,6 @@ const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([ "get options.relativeTo.timeZone.getOffsetNanosecondsFor", "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", @@ -333,8 +329,6 @@ const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([ "get options.relativeTo.timeZone.getPossibleInstantsFor", "call options.relativeTo.timeZone.getPossibleInstantsFor", // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", @@ -363,15 +357,11 @@ const expectedOpsForUnbalanceRound = expectedOpsForZonedRelativeTo.concat([ "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // No user code calls in UnbalanceDateDurationRelative // MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", // 8. "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor "call options.relativeTo.timeZone.getPossibleInstantsFor", // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime - "get options.relativeTo.timeZone.getOffsetNanosecondsFor", // 5. GetPlainDateTimeFor - "call options.relativeTo.timeZone.getOffsetNanosecondsFor", "get options.relativeTo.calendar.dateAdd", // 8. "call options.relativeTo.calendar.dateAdd", "get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor @@ -389,3 +379,37 @@ assert.compareArray( "order of operations with unit = months and ZonedDateTime relativeTo" ); actual.splice(0); // clear + +// code path that avoids converting Zoned twice in BalanceTimeDurationRelative +const expectedOpsForBalanceRound = expectedOpsForZonedRelativeTo.concat([ + // ToTemporalDate + "get options.relativeTo.timeZone.getOffsetNanosecondsFor", + "call options.relativeTo.timeZone.getOffsetNanosecondsFor", + // No user code calls in UnbalanceDateDurationRelative + // No user code calls in AddZonedDateTime (years, months, weeks = 0) + // BalanceTimeDurationRelative + "get options.relativeTo.timeZone.getOffsetNanosecondsFor", + "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // 4.a + "get options.relativeTo.timeZone.getPossibleInstantsFor", + "call options.relativeTo.timeZone.getPossibleInstantsFor", // 4.b + "get options.relativeTo.timeZone.getOffsetNanosecondsFor", + "call options.relativeTo.timeZone.getOffsetNanosecondsFor", // NanosecondsToDays 9 + "get options.relativeTo.timeZone.getPossibleInstantsFor", + "call options.relativeTo.timeZone.getPossibleInstantsFor", // NanosecondsToDays 26 + "get options.relativeTo.timeZone.getPossibleInstantsFor", + "call options.relativeTo.timeZone.getPossibleInstantsFor", // NanosecondsToDays 31.a + // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime + "get options.relativeTo.timeZone.getPossibleInstantsFor", // 10. GetInstantFor + "call options.relativeTo.timeZone.getPossibleInstantsFor", + // RoundDuration + "get options.relativeTo.calendar.dateAdd", // 10.d.i + "call options.relativeTo.calendar.dateAdd", // 10.f + "call options.relativeTo.calendar.dateAdd", // 10.i.iii +]); +new Temporal.Duration(0, 0, 0, 1, 240).total(createOptionsObserver({ unit: "weeks", relativeTo: zonedRelativeTo })); +assert.compareArray( + actual, + expectedOpsForBalanceRound, + "order of operations with unit = weeks and no calendar units" +); +actual.splice(0); // clear diff --git a/test/built-ins/Temporal/Duration/prototype/total/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js b/test/built-ins/Temporal/Duration/prototype/total/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js index 9957cbda0a9..e289e8319b6 100644 --- a/test/built-ins/Temporal/Duration/prototype/total/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js +++ b/test/built-ins/Temporal/Duration/prototype/total/relativeto-zoneddatetime-nanoseconds-to-days-range-errors.js @@ -43,6 +43,7 @@ let zdt = new Temporal.ZonedDateTime( timeZoneSubstituteValues( [[epochInstant]], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ [ + TemporalHelpers.SUBSTITUTE_SKIP, // pre-conversion in Duration.p.total dayNs - 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ -dayNs + 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] @@ -63,6 +64,7 @@ zdt = new Temporal.ZonedDateTime( timeZoneSubstituteValues( [[epochInstant]], // Returned for NanosecondsToDays step 14, setting _intermediateNs_ [ + TemporalHelpers.SUBSTITUTE_SKIP, // pre-conversion in Duration.p.total -dayNs + 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ dayNs - 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] @@ -86,6 +88,7 @@ zdt = new Temporal.ZonedDateTime( [new Temporal.Instant(-4n)], // Returned for NanosecondsToDays step 18.a, setting _oneDayFartherNs_ ], [ + TemporalHelpers.SUBSTITUTE_SKIP, // pre-conversion in Duration.p.total dayNs - 1, // Returned for NanosecondsToDays step 7, setting _startDateTime_ -dayNs + 1, // Returned for NanosecondsToDays step 11, setting _endDateTime_ ] diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/since/order-of-operations.js b/test/built-ins/Temporal/ZonedDateTime/prototype/since/order-of-operations.js index 41af64d251d..23d2f72993c 100644 --- a/test/built-ins/Temporal/ZonedDateTime/prototype/since/order-of-operations.js +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/since/order-of-operations.js @@ -318,9 +318,10 @@ const expectedOpsForCalendarDifference = [ // TimeZoneEquals "get this.timeZone.id", "get other.timeZone.id", - // DifferenceZonedDateTime + // precalculate PlainDateTime "get this.timeZone.getOffsetNanosecondsFor", "call this.timeZone.getOffsetNanosecondsFor", + // DifferenceZonedDateTime "get this.timeZone.getOffsetNanosecondsFor", "call this.timeZone.getOffsetNanosecondsFor", // DifferenceISODateTime @@ -344,12 +345,7 @@ const expectedOpsForCalendarDifference = [ ]; const expectedOpsForCalendarRounding = [ - // ToTemporalDate - "get this.timeZone.getOffsetNanosecondsFor", - "call this.timeZone.getOffsetNanosecondsFor", // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime - "get this.timeZone.getOffsetNanosecondsFor", - "call this.timeZone.getOffsetNanosecondsFor", "get this.calendar.dateAdd", "call this.calendar.dateAdd", "get this.timeZone.getPossibleInstantsFor", diff --git a/test/built-ins/Temporal/ZonedDateTime/prototype/until/order-of-operations.js b/test/built-ins/Temporal/ZonedDateTime/prototype/until/order-of-operations.js index 9c8b1849346..a5aa73a17ae 100644 --- a/test/built-ins/Temporal/ZonedDateTime/prototype/until/order-of-operations.js +++ b/test/built-ins/Temporal/ZonedDateTime/prototype/until/order-of-operations.js @@ -318,9 +318,10 @@ const expectedOpsForCalendarDifference = [ // TimeZoneEquals "get this.timeZone.id", "get other.timeZone.id", - // DifferenceZonedDateTime + // precalculate PlainDateTime "get this.timeZone.getOffsetNanosecondsFor", "call this.timeZone.getOffsetNanosecondsFor", + // DifferenceZonedDateTime "get this.timeZone.getOffsetNanosecondsFor", "call this.timeZone.getOffsetNanosecondsFor", // DifferenceISODateTime @@ -344,12 +345,7 @@ const expectedOpsForCalendarDifference = [ ]; const expectedOpsForCalendarRounding = [ - // ToTemporalDate - "get this.timeZone.getOffsetNanosecondsFor", - "call this.timeZone.getOffsetNanosecondsFor", // RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime - "get this.timeZone.getOffsetNanosecondsFor", - "call this.timeZone.getOffsetNanosecondsFor", "get this.calendar.dateAdd", "call this.calendar.dateAdd", "get this.timeZone.getPossibleInstantsFor",