Skip to content

Commit

Permalink
Temporal: Do away with CalculateOffsetShift in Duration.compare
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomato committed Aug 18, 2023
1 parent 8e27d12 commit 262274e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ const relativeTo = new Temporal.ZonedDateTime(0n, timeZone, calendar);
const duration1 = new Temporal.Duration(0, 0, 1);
const duration2 = new Temporal.Duration(0, 0, 1, 1);
Temporal.Duration.compare(duration1, duration2, { relativeTo });
assert.sameValue(calendar.dateAddCallCount, 4);
// one call in CalculateOffsetShift for each duration argument, plus one in
// UnbalanceDurationRelative for each duration argument
assert.sameValue(calendar.dateAddCallCount, 2);
// one call for each duration argument to add it to relativeTo
63 changes: 12 additions & 51 deletions test/built-ins/Temporal/Duration/compare/order-of-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,19 @@ const expectedOpsForZonedRelativeTo = expected.concat([
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]);

const expectedOpsForCalculateOffsetShift = [
// CalculateOffsetShift on first argument
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// ...in AddZonedDateTime
const expectedOpsForZonedCalendarCompare = [
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime on first argument
"get options.relativeTo.calendar.dateAdd",
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
// ...done with AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// CalculateOffsetShift on second argument
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime on second argument
"get options.relativeTo.calendar.dateAdd",
"call options.relativeTo.calendar.dateAdd",
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
];

const zonedRelativeTo = TemporalHelpers.propertyBagObserver(actual, {
Expand All @@ -317,7 +305,7 @@ Temporal.Duration.compare(
);
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat(expectedOpsForCalculateOffsetShift),
expectedOpsForZonedRelativeTo.concat(expectedOpsForZonedCalendarCompare),
"order of operations with ZonedDateTime relativeTo and no calendar units except days"
);
actual.splice(0); // clear
Expand All @@ -330,47 +318,20 @@ Temporal.Duration.compare(
);
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat([
// CalculateOffsetShift on first arg
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// CalculateOffsetShift on second arg
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// AddZonedDateTime
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
]),
expectedOpsForZonedRelativeTo,
"order of operations with ZonedDateTime relativeTo and only time units"
);
actual.splice(0); // clear

// code path through UnbalanceDurationRelative that balances higher units down
// to days:
const expectedOpsForDayBalancing = expectedOpsForZonedRelativeTo.concat(
expectedOpsForCalculateOffsetShift,
[
// ToTemporalDate
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// UnbalanceDurationRelative
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
// UnbalanceDurationRelative again for the second argument:
"get options.relativeTo.calendar.dateAdd", // 11.a.ii
"call options.relativeTo.calendar.dateAdd", // 11.a.iii.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.iv.1 MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.a.v.1 MoveRelativeDate
]
);
// order of observable operations with zoned relativeTo and calendar units
Temporal.Duration.compare(
createDurationPropertyBagObserver("one", 1, 1, 1),
createDurationPropertyBagObserver("two", 1, 1, 1, 1),
createOptionsObserver(zonedRelativeTo)
);
assert.compareArray(actual, expectedOpsForDayBalancing, "order of operations with calendar units");
assert.compareArray(
actual,
expectedOpsForZonedRelativeTo.concat(expectedOpsForZonedCalendarCompare),
"order of operations with ZonedDateTime relativeTo and calendar units"
);
actual.splice(0); // clear

0 comments on commit 262274e

Please sign in to comment.