Temporal: Remove BigInt arithmetic and loops in BalanceDurationRelative

A few results change because the algorithm previously used for rounding
didn't always add duration units to dates in RFC 5545 order, and we also
introduce a special case for rounding with largestUnit years or months and
smallestUnit weeks.
This commit is contained in:
Philip Chimento 2023-05-12 15:53:54 -07:00 committed by Philip Chimento
parent 50abc12d97
commit e43e20a885
35 changed files with 331 additions and 579 deletions

View File

@ -20,13 +20,11 @@ const relativeTo = new Temporal.ZonedDateTime(0n, timeZone, calendar);
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// MoveRelativeDate -> calendar.dateAdd()
// BalanceDurationRelative ->
// MoveRelativeDate -> calendar.dateAdd() (2x)
// calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
const instance1 = new Temporal.Duration(1, 1, 1, 1, 1);
instance1.round({ smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 5, "rounding with calendar smallestUnit");
assert.sameValue(calendar.dateAddCallCount, 3, "rounding with calendar smallestUnit");
// Rounding with a non-default largestUnit to cover the path in
// UnbalanceDurationRelative where larger units are converted into smaller
@ -37,8 +35,7 @@ assert.sameValue(calendar.dateAddCallCount, 5, "rounding with calendar smallestU
// UnbalanceDurationRelative -> MoveRelativeDate -> calendar.dateAdd()
// RoundDuration ->
// MoveRelativeDate -> calendar.dateAdd() (5x)
// BalanceDurationRelative
// MoveRelativeDate -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
calendar.dateAddCallCount = 0;
@ -52,12 +49,10 @@ assert.sameValue(calendar.dateAddCallCount, 8, "rounding with non-default larges
// Duration.round() ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// BalanceDurationRelative ->
// MoveRelativeDate -> calendar.dateAdd() (2x)
// calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
calendar.dateAddCallCount = 0;
const instance3 = new Temporal.Duration(1, 1, 1, 1, 1);
instance3.round({ smallestUnit: "days", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 4, "rounding with days smallestUnit");
assert.sameValue(calendar.dateAddCallCount, 2, "rounding with days smallestUnit");

View File

@ -74,8 +74,8 @@ features: [Temporal]
// should result in one call to dateUntil() originating from
// AdjustRoundedDurationDays, with largestUnit equal to the largest unit in
// the duration higher than "day".
// Additionally one call with largestUnit: "month" in BalanceDurationRelative
// when the largestUnit given to round() is "year".
// Additionally one call in BalanceDateDurationRelative with the same
// largestUnit.
// Other calls have largestUnit: "day" so the difference is taken in ISO
// calendar space.
@ -99,9 +99,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
duration.round({ largestUnit, roundingIncrement: 2, roundingMode: 'ceil', relativeTo });
},
{
years: ["year", "month"],
months: ["month"],
weeks: ["week"],
years: ["year", "year"],
months: ["month", "month"],
weeks: ["week", "week"],
days: [],
hours: [],
minutes: [],
@ -122,9 +122,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
duration.round({ largestUnit, relativeTo });
},
{
years: ["month", "month", "month", "month", "month", "month"],
months: ["month"],
weeks: [],
years: ["year"],
months: ["month", "month"],
weeks: ["week"],
days: [],
hours: [],
minutes: [],
@ -147,8 +147,8 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
duration.round({ largestUnit, smallestUnit: largestUnit, relativeTo });
}, {
years: ["year"],
months: [],
weeks: [],
months: ["month"],
weeks: ["week"],
days: []
}
);

View File

@ -45,8 +45,8 @@ assert.throws(
"rounding a week Duration fails without largest/smallest unit"
);
TemporalHelpers.assertDuration(duration3.round(relativeToYears), 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, "round week duration to years");
TemporalHelpers.assertDuration(duration3.round(relativeToMonths), 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, "round week duration to months");
TemporalHelpers.assertDuration(duration3.round(relativeToYears), 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, "round week duration to years");
TemporalHelpers.assertDuration(duration3.round(relativeToMonths), 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, "round week duration to months");
TemporalHelpers.assertDuration(duration3.round(relativeToWeeks), 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, "round week duration to weeks");
TemporalHelpers.assertDuration(duration3.round(relativeToDays), 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, "round week duration to days");

View File

@ -4,8 +4,8 @@
/*---
esid: sec-temporal.duration.prototype.round
description: >
When consulting calendar.dateUntil() to calculate the number of months in a
year, the months property is not accessed on the result Duration
When consulting calendar.dateUntil() to balance or unbalance a duration, no
properties are accessed on the result Duration
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/
@ -16,7 +16,10 @@ class CalendarDateUntilObservable extends Temporal.Calendar {
dateUntil(...args) {
actual.push("call dateUntil");
const returnValue = super.dateUntil(...args);
TemporalHelpers.observeProperty(actual, returnValue, "years", Infinity);
TemporalHelpers.observeProperty(actual, returnValue, "months", Infinity);
TemporalHelpers.observeProperty(actual, returnValue, "weeks", Infinity);
TemporalHelpers.observeProperty(actual, returnValue, "days", Infinity);
return returnValue;
}
}
@ -24,28 +27,12 @@ class CalendarDateUntilObservable extends Temporal.Calendar {
const calendar = new CalendarDateUntilObservable("iso8601");
const relativeTo = new Temporal.PlainDate(2018, 10, 12, calendar);
// One path, through UnbalanceDateDurationRelative, calls dateUntil()
const expected1 = [
"call dateUntil",
const expected = [
"call dateUntil", // UnbalanceDateDurationRelative
"call dateUntil", // BalanceDateDurationRelative
];
const years = new Temporal.Duration(2);
const result1 = years.round({ largestUnit: "months", relativeTo });
TemporalHelpers.assertDuration(result1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, "result");
assert.compareArray(actual, expected1, "operations");
// There is a second path, through BalanceDurationRelative, that calls
// dateUntil() in a loop for each year in the duration plus one extra time
actual.splice(0); // reset calls for next test
const expected2 = [
"call dateUntil",
"call dateUntil",
"call dateUntil",
];
const months = new Temporal.Duration(0, 24);
const result2 = months.round({ largestUnit: "years", relativeTo });
TemporalHelpers.assertDuration(result2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "result");
assert.compareArray(actual, expected2, "operations");
const result = years.round({ largestUnit: "months", relativeTo });
TemporalHelpers.assertDuration(result, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, "result");
assert.compareArray(actual, expected, "operations");

View File

@ -10,8 +10,8 @@ features: [Temporal]
// Based on a test case by André Bargull <andre.bargull@gmail.com>
// Note: February in a leap year.
const relativeTo = new Temporal.PlainDate(1972, 2, 1);
// Note: One day after February in a leap year.
const relativeTo = new Temporal.PlainDate(1972, 3, 1);
const options = {
largestUnit: "years",

View File

@ -139,10 +139,8 @@ const expectedOpsForYearRounding = expectedOpsForPlainRelativeTo.concat([
"call options.relativeTo.calendar.dateAdd", // 7.s MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 7.y MoveRelativeDate
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 11.c MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.g MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.k
"call options.relativeTo.calendar.dateUntil", // 11.p
"call options.relativeTo.calendar.dateAdd", // 9.c
"call options.relativeTo.calendar.dateUntil", // 9.d
]);
const instanceYears = new Temporal.Duration(1, 12, 0, 0, /* hours = */ 2400);
instanceYears.round(createOptionsObserver({ smallestUnit: "years", relativeTo: plainRelativeTo }));
@ -159,8 +157,9 @@ const expectedOpsForMonthRounding = expectedOpsForPlainRelativeTo.concat([
"call options.relativeTo.calendar.dateAdd", // 10.e
"call options.relativeTo.calendar.dateAdd", // 10.k MoveRelativeDate
], Array(2).fill("call options.relativeTo.calendar.dateAdd"), [ // 2× 10.n.iii MoveRelativeDate
// BalanceDurationRelative
"call options.relativeTo.calendar.dateAdd",
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 10.d
"call options.relativeTo.calendar.dateUntil", // 10.e
]);
const instance2 = new Temporal.Duration(1, 0, 0, 62);
instance2.round(createOptionsObserver({ largestUnit: "months", smallestUnit: "months", relativeTo: plainRelativeTo }));
@ -174,8 +173,9 @@ const expectedOpsForWeekRounding = expectedOpsForPlainRelativeTo.concat([
// RoundDuration
"call options.relativeTo.calendar.dateAdd", // 11.d MoveRelativeDate
], Array(58).fill("call options.relativeTo.calendar.dateAdd"), [ // 58× 11.g.iii MoveRelativeDate (52 + 4 + 2)
// BalanceDurationRelative
"call options.relativeTo.calendar.dateAdd", // 12.c
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 16
"call options.relativeTo.calendar.dateUntil", // 17
]);
const instance3 = new Temporal.Duration(1, 1, 0, 15);
instance3.round(createOptionsObserver({ largestUnit: "weeks", smallestUnit: "weeks", relativeTo: plainRelativeTo }));
@ -191,14 +191,10 @@ instance4.round(createOptionsObserver({ largestUnit: "days", smallestUnit: "days
assert.compareArray(actual, expectedOpsForDayRounding, "order of operations with largestUnit = smallestUnit = days");
actual.splice(0); // clear
// code path through BalanceDurationRelative balancing from days up to years:
// code path through BalanceDateDurationRelative balancing from days up to years:
const expectedOpsForDayToYearBalancing = expectedOpsForPlainRelativeTo.concat([
"call options.relativeTo.calendar.dateAdd", // 10.b MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.e.iv MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.f MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.i.iv MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.j
"call options.relativeTo.calendar.dateUntil", // 10.n
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateUntil", // 9.d
]);
const instance5 = new Temporal.Duration(0, 0, 0, 0, /* hours = */ 396 * 24);
instance5.round(createOptionsObserver({ largestUnit: "years", smallestUnit: "days", relativeTo: plainRelativeTo }));
@ -211,13 +207,9 @@ const expectedOpsForMonthToYearBalancing = expectedOpsForPlainRelativeTo.concat(
"call options.relativeTo.calendar.dateAdd", // 10.c
"call options.relativeTo.calendar.dateAdd", // 10.e
"call options.relativeTo.calendar.dateAdd", // 10.k MoveRelativeDate
// BalanceDurationRelative
"call options.relativeTo.calendar.dateAdd", // 10.b MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.f MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.j
"call options.relativeTo.calendar.dateUntil", // 10.n
"call options.relativeTo.calendar.dateAdd", // 10.p.iv
"call options.relativeTo.calendar.dateUntil", // 10.p.vii
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 9.c
"call options.relativeTo.calendar.dateUntil", // 9.d
]);
const instance6 = new Temporal.Duration(0, 12);
instance6.round(createOptionsObserver({ largestUnit: "years", smallestUnit: "months", relativeTo: plainRelativeTo }));
@ -225,9 +217,8 @@ assert.compareArray(actual, expectedOpsForMonthToYearBalancing, "order of operat
actual.splice(0); // clear
const expectedOpsForDayToMonthBalancing = expectedOpsForPlainRelativeTo.concat([
// BalanceDurationRelative
"call options.relativeTo.calendar.dateAdd", // 11.b MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.e.iv MoveRelativeDate
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateUntil", // 10.e
]);
const instance7 = new Temporal.Duration(0, 0, 0, 0, /* hours = */ 32 * 24);
instance7.round(createOptionsObserver({ largestUnit: "months", smallestUnit: "days", relativeTo: plainRelativeTo }));
@ -235,9 +226,8 @@ assert.compareArray(actual, expectedOpsForDayToMonthBalancing, "order of operati
actual.splice(0); // clear
const expectedOpsForDayToWeekBalancing = expectedOpsForPlainRelativeTo.concat([
// BalanceDurationRelative
"call options.relativeTo.calendar.dateAdd", // 12.c MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 12.f.iv MoveRelativeDate
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateUntil", // 17
]);
const instance8 = new Temporal.Duration(0, 0, 0, 0, /* hours = */ 8 * 24);
instance8.round(createOptionsObserver({ largestUnit: "weeks", smallestUnit: "days", relativeTo: plainRelativeTo }));
@ -403,10 +393,8 @@ const expectedOpsForYearRoundingZoned = expectedOpsForZonedRelativeTo.concat([
"call options.relativeTo.calendar.dateAdd", // 7.s MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 7.y MoveRelativeDate
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 11.c MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.g MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 11.k
"call options.relativeTo.calendar.dateUntil", // 11.p
"call options.relativeTo.calendar.dateAdd", // 9.c
"call options.relativeTo.calendar.dateUntil", // 9.d
]);
instanceYears.round(createOptionsObserver({ smallestUnit: "years", relativeTo: zonedRelativeTo }));
assert.compareArray(
@ -431,12 +419,35 @@ const expectedOpsForUnbalanceRoundBalance = expectedOpsForZonedRelativeTo.concat
// RoundDuration
"call options.relativeTo.calendar.dateAdd", // 8.g MoveRelativeDate
// BalanceDateDurationRelative
"call options.relativeTo.calendar.dateAdd", // 13.c MoveRelativeDate
"call options.relativeTo.calendar.dateAdd", // 10.d
"call options.relativeTo.calendar.dateUntil", // 10.e
]);
new Temporal.Duration(0, 1, 1).round(createOptionsObserver({ largestUnit: "years", smallestUnit: "weeks", relativeTo: zonedRelativeTo }));
assert.compareArray(
actual,
expectedOpsForUnbalanceRoundBalance,
"order of operations with largestUnit = years, smallestUnit = weeks, and ZonedDateTime relativeTo"
);
actual.splice(0); // clear
// code path that skips user code calls in BalanceDateDurationRelative due to
// special case for largestUnit months and smallestUnit weeks
const expectedOpsForWeeksSpecialCase = expectedOpsForZonedRelativeTo.concat([
// ToTemporalDate
"call options.relativeTo.timeZone.getOffsetNanosecondsFor",
// lookup in Duration.p.round
"get options.relativeTo.calendar.dateAdd",
"get options.relativeTo.calendar.dateUntil",
// RoundDuration → MoveRelativeZonedDateTime → AddZonedDateTime
"call options.relativeTo.calendar.dateAdd",
"call options.relativeTo.timeZone.getPossibleInstantsFor", // 13. GetInstantFor
// RoundDuration
"call options.relativeTo.calendar.dateAdd", // 14.p MoveRelativeDate
]);
new Temporal.Duration(0, 1, 1).round(createOptionsObserver({ largestUnit: "months", smallestUnit: "weeks", relativeTo: zonedRelativeTo }));
assert.compareArray(
actual,
expectedOpsForUnbalanceRoundBalance,
expectedOpsForWeeksSpecialCase,
"order of operations with largestUnit = months, smallestUnit = weeks, and ZonedDateTime relativeTo"
);
actual.splice(0); // clear

View File

@ -1,52 +0,0 @@
// Copyright (C) 2022 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
BalanceDurationRelative computes on exact mathematical values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
months: Number.MAX_SAFE_INTEGER,
days: 31 + 28 + 31,
});
let result = duration.round({
largestUnit: "months",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
0, 9007199254740994, 0, 0,
0, 0, 0,
0, 0, 0,
);
}
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
months: Number.MAX_SAFE_INTEGER,
days: 31 + 28 + 31 + 1,
});
let result = duration.round({
largestUnit: "months",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
0, 9007199254740994, 0, 1,
0, 0, 0,
0, 0, 0,
);
}

View File

@ -1,52 +0,0 @@
// Copyright (C) 2022 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
BalanceDurationRelative computes on exact mathematical values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
weeks: Number.MAX_SAFE_INTEGER,
days: 7 * 3,
});
let result = duration.round({
largestUnit: "weeks",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
0, 0, 9007199254740994, 0,
0, 0, 0,
0, 0, 0,
);
}
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
weeks: Number.MAX_SAFE_INTEGER,
days: 7 * 3 + 1,
});
let result = duration.round({
largestUnit: "weeks",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
0, 0, 9007199254740994, 1,
0, 0, 0,
0, 0, 0,
);
}

View File

@ -1,52 +0,0 @@
// Copyright (C) 2022 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
BalanceDurationRelative computes on exact mathematical values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
years: Number.MAX_SAFE_INTEGER,
days: 366 + 365 + 365,
});
let result = duration.round({
largestUnit: "years",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
9007199254740994, 0, 0, 0,
0, 0, 0,
0, 0, 0,
);
}
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
years: Number.MAX_SAFE_INTEGER,
days: 366 + 365 + 365 + 1,
});
let result = duration.round({
largestUnit: "years",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
9007199254740994, 0, 0, 1,
0, 0, 0,
0, 0, 0,
);
}

View File

@ -1,52 +0,0 @@
// Copyright (C) 2022 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
BalanceDurationRelative computes on exact mathematical values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
years: Number.MAX_SAFE_INTEGER,
months: 12 * 3,
});
let result = duration.round({
largestUnit: "years",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
9007199254740994, 0, 0, 0,
0, 0, 0,
0, 0, 0,
);
}
{
let date = new Temporal.PlainDate(1970, 1, 1);
let duration = Temporal.Duration.from({
years: Number.MAX_SAFE_INTEGER,
months: 12 * 3 + 1,
});
let result = duration.round({
largestUnit: "years",
relativeTo: date,
});
TemporalHelpers.assertDuration(
result,
9007199254740994, 1, 0, 0,
0, 0, 0,
0, 0, 0,
);
}

View File

@ -1,53 +0,0 @@
// Copyright (C) 2022 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
BalanceDurationRelative computes on exact mathematical values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
let calendar = new class extends Temporal.Calendar {
#dateUntil = 0;
dateUntil(one, two, options) {
this.#dateUntil++;
// Subtract one six times from 9007199254740996.
if (this.#dateUntil <= 6) {
return Temporal.Duration.from({months: 1});
}
// After subtracting six in total, months is 9007199254740996 - 6 = 9007199254740990.
// |MAX_SAFE_INTEGER| = 9007199254740991 is larger than 9007199254740990, so we exit
// from the loop.
if (this.#dateUntil === 7) {
return Temporal.Duration.from({months: Number.MAX_SAFE_INTEGER});
}
// Any additional calls to dateUntil are incorrect.
throw new Test262Error("dateUntil called more times than expected");
}
}("iso8601");
let date = new Temporal.PlainDate(1970, 1, 1, calendar);
let duration = Temporal.Duration.from({
years: 0,
months: Number.MAX_SAFE_INTEGER + 4, // 9007199254740996
});
let result = duration.round({
largestUnit: "years",
relativeTo: date,
});
// Years is equal to the number of times we returned one month from dateUntil.
// Months is equal to 9007199254740996 - 6.
TemporalHelpers.assertDuration(
result,
6, 9007199254740990, 0, 0,
0, 0, 0,
0, 0, 0,
);

View File

@ -11,13 +11,13 @@ includes: [temporalHelpers.js]
features: [Temporal]
---*/
const instance = new Temporal.Duration(1);
const instance = new Temporal.Duration(0, 0, 0, 1);
const options = {
smallestUnit: "years",
smallestUnit: "days",
roundingMode: "expand",
relativeTo: new Temporal.PlainDate(2000, 1, 1),
};
const result = instance.round({ ...options, roundingIncrement: 2.5 });
TemporalHelpers.assertDuration(result, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 2.5 truncates to 2");
TemporalHelpers.assertDuration(result, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, "roundingIncrement 2.5 truncates to 2");
const result2 = instance.round({ ...options, roundingIncrement: 1e9 + 0.5 });
TemporalHelpers.assertDuration(result2, 1e9, 0, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 1e9 + 0.5 truncates to 1e9");
TemporalHelpers.assertDuration(result2, 0, 0, 0, 1e9, 0, 0, 0, 0, 0, 0, "roundingIncrement 1e9 + 0.5 truncates to 1e9");

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-5]],
["months", [5, 8], [-5, -7]],
["weeks", [5, 6, 9], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -9]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -16]],
["minutes", [5, 6, 7, 9, 16, 31], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 21], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -123]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 988], [-5, -6, -7, -9, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -27]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -16]],
["minutes", [5, 7, 0, 27, 16, 31], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 21], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -123]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 988], [-5, -7, 0, -27, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "ceil";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 9], [-5, -6, -9]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 31], [-5, -6, -7, -9, -16, -31]],
["seconds", [5, 6, 7, 9, 16, 30, 21], [-5, -6, -7, -9, -16, -30, -21]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 988], [-5, -6, -7, -9, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 31], [-5, -7, 0, -27, -16, -31]],
["seconds", [5, 7, 0, 27, 16, 30, 21], [-5, -7, 0, -27, -16, -30, -21]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 988], [-5, -7, 0, -27, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "expand";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [5], [-6]],
["months", [5, 7], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -9]],
["days", [5, 6, 7, 9], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 16], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -31]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -21]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 123], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 27], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 16], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -31]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -21]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 123], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987], [-5, -7, 0, -27, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "floor";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 988], [-5, -6, -7, -9, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 988], [-5, -7, 0, -27, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "halfCeil";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 988], [-5, -6, -7, -9, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 988], [-5, -7, 0, -27, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "halfEven";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 988], [-5, -6, -7, -9, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 988], [-5, -7, 0, -27, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "halfExpand";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987], [-5, -7, 0, -27, -16, -30, -20, -123, -988]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "halfFloor";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [6], [-6]],
["months", [5, 8], [-5, -8]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 10], [-5, -6, -7, -10]],
["hours", [5, 6, 7, 9, 17], [-5, -6, -7, -9, -17]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 124], [-5, -6, -7, -9, -16, -30, -20, -124]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 28], [-5, -7, 0, -28]],
["hours", [5, 7, 0, 27, 17], [-5, -7, 0, -27, -17]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 124], [-5, -7, 0, -27, -16, -30, -20, -124]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987], [-5, -7, 0, -27, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "halfTrunc";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -9,19 +9,23 @@ features: [Temporal]
---*/
const instance = new Temporal.Duration(5, 6, 7, 8, 40, 30, 20, 123, 987, 500);
const relativeTo = new Temporal.PlainDate(2020, 1, 1);
// Chosen such that 8 months forwards from relativeToForwards is the
// same number of days as 8 months backwards from relativeToBackwards
// (for convenience)
const relativeToForwards = new Temporal.PlainDate(2020, 4, 1);
const relativeToBackwards = new Temporal.PlainDate(2020, 12, 1);
const expected = [
["years", [5], [-5]],
["months", [5, 7], [-5, -7]],
["weeks", [5, 6, 8], [-5, -6, -8]],
["days", [5, 6, 7, 9], [-5, -6, -7, -9]],
["hours", [5, 6, 7, 9, 16], [-5, -6, -7, -9, -16]],
["minutes", [5, 6, 7, 9, 16, 30], [-5, -6, -7, -9, -16, -30]],
["seconds", [5, 6, 7, 9, 16, 30, 20], [-5, -6, -7, -9, -16, -30, -20]],
["milliseconds", [5, 6, 7, 9, 16, 30, 20, 123], [-5, -6, -7, -9, -16, -30, -20, -123]],
["microseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987], [-5, -6, -7, -9, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 6, 7, 9, 16, 30, 20, 123, 987, 500], [-5, -6, -7, -9, -16, -30, -20, -123, -987, -500]],
["days", [5, 7, 0, 27], [-5, -7, 0, -27]],
["hours", [5, 7, 0, 27, 16], [-5, -7, 0, -27, -16]],
["minutes", [5, 7, 0, 27, 16, 30], [-5, -7, 0, -27, -16, -30]],
["seconds", [5, 7, 0, 27, 16, 30, 20], [-5, -7, 0, -27, -16, -30, -20]],
["milliseconds", [5, 7, 0, 27, 16, 30, 20, 123], [-5, -7, 0, -27, -16, -30, -20, -123]],
["microseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987], [-5, -7, 0, -27, -16, -30, -20, -123, -987]],
["nanoseconds", [5, 7, 0, 27, 16, 30, 20, 123, 987, 500], [-5, -7, 0, -27, -16, -30, -20, -123, -987, -500]],
];
const roundingMode = "trunc";
@ -30,12 +34,12 @@ expected.forEach(([smallestUnit, expectedPositive, expectedNegative]) => {
const [py, pm = 0, pw = 0, pd = 0, ph = 0, pmin = 0, ps = 0, pms = 0, pµs = 0, pns = 0] = expectedPositive;
const [ny, nm = 0, nw = 0, nd = 0, nh = 0, nmin = 0, ns = 0, nms = 0, nµs = 0, nns = 0] = expectedNegative;
TemporalHelpers.assertDuration(
instance.round({ smallestUnit, relativeTo, roundingMode }),
instance.round({ smallestUnit, relativeTo: relativeToForwards, roundingMode }),
py, pm, pw, pd, ph, pmin, ps, pms, pµs, pns,
`rounds to ${smallestUnit} (roundingMode = ${roundingMode}, positive case)`
);
TemporalHelpers.assertDuration(
instance.negated().round({ smallestUnit, relativeTo, roundingMode }),
instance.negated().round({ smallestUnit, relativeTo: relativeToBackwards, roundingMode }),
ny, nm, nw, nd, nh, nmin, ns, nms, nµs, nns,
`rounds to ${smallestUnit} (rounding mode = ${roundingMode}, negative case)`
);

View File

@ -15,9 +15,11 @@ var duration = Temporal.Duration.from({
});
var cal = new class extends Temporal.Calendar {
called = 0;
dateUntil(one, two, options) {
++this.called;
var result = super.dateUntil(one, two, options);
return result.negated();
return this.called === 1 ? result.negated() : result;
}
}("iso8601");

View File

@ -138,11 +138,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -167,11 +165,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherDatePropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months/weeks");
@ -189,8 +185,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil" // 10.e
]);
instance.since(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");
@ -206,8 +203,9 @@ const expectedOpsForWeekRounding = expected.concat([
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil" // 17
]);
instance.since(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -139,11 +139,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -168,11 +166,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDatePropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months/weeks");
@ -190,8 +186,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil" // 10.e
]);
instance.until(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");
@ -207,8 +204,9 @@ const expectedOpsForWeekRounding = expected.concat([
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil" // 17
]);
instance.until(otherDatePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -168,11 +168,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -203,11 +201,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDatePropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months/weeks");
@ -225,8 +221,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil", // 10.e
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");
@ -242,8 +239,9 @@ const expectedOpsForWeekRounding = expected.concat([
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil", // 17
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -168,11 +168,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -203,11 +201,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDatePropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months/weeks");
@ -225,8 +221,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil" // 10.e
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = years");
@ -242,8 +239,9 @@ const expectedOpsForWeekRounding = expected.concat([
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil" // 17
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -146,11 +146,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherYearMonthPropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -168,11 +166,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherYearMonthPropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months");
@ -185,11 +181,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil" // 10.e
]);
instance.since(otherYearMonthPropertyBag, createOptionsObserver({ smallestUnit: "months", roundingIncrement: 2 }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");

View File

@ -20,5 +20,6 @@ const earlier = new Temporal.PlainYearMonth(2000, 5);
const later = new Temporal.PlainYearMonth(2000, 10);
const result = later.since(earlier, { roundingIncrement: 2.5, roundingMode: "trunc" });
TemporalHelpers.assertDuration(result, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 2.5 truncates to 2");
const result2 = later.since(earlier, { largestUnit: "months", smallestUnit: "months", roundingIncrement: 1e9 + 0.5, roundingMode: "expand" });
TemporalHelpers.assertDuration(result2, 0, 1e9, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 1e9 + 0.5 truncates to 1e9");
// Cannot test the upper bound of 1e9 + 0.5 here, because the duration is
// rounded relative to the receiver PlainYearMonth, and 1e9 months is outside of
// the PlainYearMonth range.

View File

@ -146,11 +146,9 @@ const expectedOpsForYearRounding = expected.concat([
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherYearMonthPropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -168,11 +166,9 @@ const expectedOpsForYearRoundingSameMonth = expected.concat([
"call this.calendar.dateAdd", // 7.g
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.o not called because months and weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherYearMonthPropertyBagSameMonth, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRoundingSameMonth, "order of operations with smallestUnit = years and no excess months");
@ -185,11 +181,9 @@ const expectedOpsForMonthRounding = expected.concat([
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil" // 10.e
]);
instance.until(otherYearMonthPropertyBag, createOptionsObserver({ smallestUnit: "months", roundingIncrement: 2 }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");

View File

@ -20,5 +20,6 @@ const earlier = new Temporal.PlainYearMonth(2000, 5);
const later = new Temporal.PlainYearMonth(2000, 10);
const result = earlier.until(later, { roundingIncrement: 2.5, roundingMode: "trunc" });
TemporalHelpers.assertDuration(result, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 2.5 truncates to 2");
const result2 = earlier.until(later, { largestUnit: "months", smallestUnit: "months", roundingIncrement: 1e9 + 0.5, roundingMode: "expand" });
TemporalHelpers.assertDuration(result2, 0, 1e9, 0, 0, 0, 0, 0, 0, 0, 0, "roundingIncrement 1e9 + 0.5 truncates to 1e9");
// Cannot test the upper bound of 1e9 + 0.5 here, because the duration is
// rounded relative to the receiver PlainYearMonth, and 1e9 months is outside of
// the PlainYearMonth range.

View File

@ -77,9 +77,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
later.since(earlier, { largestUnit, roundingIncrement: 2, roundingMode: 'ceil' });
},
{
years: ["year", "year", "month"],
months: ["month", "month"],
weeks: ["week", "week"],
years: ["year", "year", "year"],
months: ["month", "month", "month"],
weeks: ["week", "week", "week"],
days: [],
hours: [],
minutes: [],
@ -100,9 +100,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
later.since(earlier, { smallestUnit });
},
{
years: ["year", "year", "month"],
months: ["month"],
weeks: ["week"],
years: ["year", "year", "year"],
months: ["month", "month"],
weeks: ["week", "week"],
days: [],
hours: [],
minutes: [],

View File

@ -364,11 +364,9 @@ const expectedOpsForYearRounding = expected.concat(expectedOpsForCalendarDiffere
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -381,8 +379,9 @@ const expectedOpsForMonthRounding = expected.concat(expectedOpsForCalendarDiffer
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil", // 10.e
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");
@ -393,8 +392,9 @@ const expectedOpsForWeekRounding = expected.concat(expectedOpsForCalendarDiffere
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil", // 17
]);
instance.since(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -77,9 +77,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
earlier.until(later, { largestUnit, roundingIncrement: 2, roundingMode: 'ceil' });
},
{
years: ["year", "year", "month"],
months: ["month", "month"],
weeks: ["week", "week"],
years: ["year", "year", "year"],
months: ["month", "month", "month"],
weeks: ["week", "week", "week"],
days: [],
hours: [],
minutes: [],
@ -100,9 +100,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
earlier.until(later, { smallestUnit });
},
{
years: ["year", "year", "month"],
months: ["month"],
weeks: ["week"],
years: ["year", "year", "year"],
months: ["month", "month"],
weeks: ["week", "week"],
days: [],
hours: [],
minutes: [],

View File

@ -364,11 +364,9 @@ const expectedOpsForYearRounding = expected.concat(expectedOpsForCalendarDiffere
"call this.calendar.dateUntil", // 7.o
"call this.calendar.dateAdd", // 7.y MoveRelativeDate
// (7.s not called because other units can't add up to >1 year at this point)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 11.c MoveRelativeDate
"call this.calendar.dateAdd", // 11.g MoveRelativeDate
"call this.calendar.dateAdd", // 11.k
"call this.calendar.dateUntil" // 11.n
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 9.c
"call this.calendar.dateUntil" // 9.d
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "years" }));
assert.compareArray(actual, expectedOpsForYearRounding, "order of operations with smallestUnit = years");
@ -381,8 +379,9 @@ const expectedOpsForMonthRounding = expected.concat(expectedOpsForCalendarDiffer
"call this.calendar.dateAdd", // 10.e
"call this.calendar.dateAdd", // 10.k MoveRelativeDate
// (10.n.iii MoveRelativeDate not called because weeks == 0)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 12.b MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 10.d
"call this.calendar.dateUntil", // 10.e
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "months" }));
assert.compareArray(actual, expectedOpsForMonthRounding, "order of operations with smallestUnit = months");
@ -393,8 +392,9 @@ const expectedOpsForWeekRounding = expected.concat(expectedOpsForCalendarDiffere
// RoundDuration
"call this.calendar.dateAdd", // 11.d MoveRelativeDate
// (11.g.iii MoveRelativeDate not called because days already balanced)
// BalanceDurationRelative
"call this.calendar.dateAdd", // 13.c MoveRelativeDate
// BalanceDateDurationRelative
"call this.calendar.dateAdd", // 16
"call this.calendar.dateUntil", // 17
]);
instance.until(otherDateTimePropertyBag, createOptionsObserver({ smallestUnit: "weeks" }));
assert.compareArray(actual, expectedOpsForWeekRounding, "order of operations with smallestUnit = weeks");

View File

@ -208,7 +208,7 @@ assert.sameValue(`${ hours25.round({
assert.sameValue(`${ d.round({
smallestUnit: "seconds",
relativeTo
}) }`, "P5Y5M5W5DT5H5M5S");
}) }`, "P5Y6M10DT5H5M5S");
});
// does not accept non-string primitives for relativeTo
@ -325,24 +325,24 @@ var roundAndBalanceResults = {
years: "P6Y",
months: "P5Y6M",
weeks: "P5Y5M6W",
days: "P5Y5M5W5D",
hours: "P5Y5M5W5DT5H",
minutes: "P5Y5M5W5DT5H5M",
seconds: "P5Y5M5W5DT5H5M5S",
milliseconds: "P5Y5M5W5DT5H5M5.005S",
microseconds: "P5Y5M5W5DT5H5M5.005005S",
nanoseconds: "P5Y5M5W5DT5H5M5.005005005S"
days: "P5Y6M10D",
hours: "P5Y6M10DT5H",
minutes: "P5Y6M10DT5H5M",
seconds: "P5Y6M10DT5H5M5S",
milliseconds: "P5Y6M10DT5H5M5.005S",
microseconds: "P5Y6M10DT5H5M5.005005S",
nanoseconds: "P5Y6M10DT5H5M5.005005005S"
},
months: {
months: "P66M",
weeks: "P65M6W",
days: "P65M5W5D",
hours: "P65M5W5DT5H",
minutes: "P65M5W5DT5H5M",
seconds: "P65M5W5DT5H5M5S",
milliseconds: "P65M5W5DT5H5M5.005S",
microseconds: "P65M5W5DT5H5M5.005005S",
nanoseconds: "P65M5W5DT5H5M5.005005005S"
days: "P66M10D",
hours: "P66M10DT5H",
minutes: "P66M10DT5H5M",
seconds: "P66M10DT5H5M5S",
milliseconds: "P66M10DT5H5M5.005S",
microseconds: "P66M10DT5H5M5.005005S",
nanoseconds: "P66M10DT5H5M5.005005005S"
},
weeks: {
weeks: "P288W",
@ -521,42 +521,42 @@ assert.sameValue(`${ d.round({
smallestUnit: "hours",
roundingIncrement: 3,
relativeTo
}) }`, "P5Y5M5W5DT6H");
}) }`, "P5Y6M10DT6H");
// rounds to an increment of minutes
assert.sameValue(`${ d.round({
smallestUnit: "minutes",
roundingIncrement: 30,
relativeTo
}) }`, "P5Y5M5W5DT5H");
}) }`, "P5Y6M10DT5H");
// rounds to an increment of seconds
assert.sameValue(`${ d.round({
smallestUnit: "seconds",
roundingIncrement: 15,
relativeTo
}) }`, "P5Y5M5W5DT5H5M");
}) }`, "P5Y6M10DT5H5M");
// rounds to an increment of milliseconds
assert.sameValue(`${ d.round({
smallestUnit: "milliseconds",
roundingIncrement: 10,
relativeTo
}) }`, "P5Y5M5W5DT5H5M5.01S");
}) }`, "P5Y6M10DT5H5M5.01S");
// rounds to an increment of microseconds
assert.sameValue(`${ d.round({
smallestUnit: "microseconds",
roundingIncrement: 10,
relativeTo
}) }`, "P5Y5M5W5DT5H5M5.00501S");
}) }`, "P5Y6M10DT5H5M5.00501S");
// rounds to an increment of nanoseconds
assert.sameValue(`${ d.round({
smallestUnit: "nanoseconds",
roundingIncrement: 10,
relativeTo
}) }`, "P5Y5M5W5DT5H5M5.00500501S");
}) }`, "P5Y6M10DT5H5M5.00500501S");
// valid hour increments divide into 24
[