Skip to content

Commit

Permalink
Temporal: Precalculate PlainDateTime from ZonedDateTime in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomato committed Oct 4, 2023
1 parent 3a57a42 commit c04b417
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_
]
Expand All @@ -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_
]
Expand All @@ -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_
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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_
]
Expand All @@ -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_
]
Expand All @@ -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_
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down

0 comments on commit c04b417

Please sign in to comment.