Move all tests from staging/Temporal/Instant/old to test/built-ins/Temporal/Instant (#4282)

* Temporal: Move all tests from staging/Temporal/Instant/old to test/built-ins/Temporal/instant
This commit is contained in:
Tim Chevalier 2024-10-30 14:29:02 -07:00 committed by GitHub
parent f178db57fe
commit 3f2486e37f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
36 changed files with 966 additions and 710 deletions

View File

@ -2,20 +2,20 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.compare works
esid: sec-temporal.instant.compare
description: Temporal.Instant.compare works cross-epoch.
features: [Temporal]
---*/
var i1 = Temporal.Instant.from("1963-02-13T09:36:29.123456789Z");
var i2 = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
var i3 = Temporal.Instant.from("1981-12-15T14:34:31.987654321Z");
const i1 = Temporal.Instant.from("1963-02-13T09:36:29.123456789Z");
const i2 = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const i3 = Temporal.Instant.from("1981-12-15T14:34:31.987654321Z");
// pre epoch equal
assert.sameValue(Temporal.Instant.compare(i1, Temporal.Instant.from(i1)), 0)
assert.sameValue(Temporal.Instant.compare(i1, i1), 0)
// epoch equal
assert.sameValue(Temporal.Instant.compare(i2, Temporal.Instant.from(i2)), 0)
assert.sameValue(Temporal.Instant.compare(i2, i2), 0)
// cross epoch smaller/larger
assert.sameValue(Temporal.Instant.compare(i1, i2), -1)

View File

@ -0,0 +1,20 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.fromepochmilliseconds
description: Min/max range.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// constructing from ms
var limit = 8640000000000000;
assert.throws(RangeError, () => Temporal.Instant.fromEpochMilliseconds(-limit - 1));
assert.throws(RangeError, () => Temporal.Instant.fromEpochMilliseconds(limit + 1));
TemporalHelpers.assertInstantsEqual(Temporal.Instant.fromEpochMilliseconds(-limit),
Temporal.Instant.from("-271821-04-20T00:00:00Z"));
TemporalHelpers.assertInstantsEqual(Temporal.Instant.fromEpochMilliseconds(limit),
Temporal.Instant.from("+275760-09-13T00:00:00Z"));

View File

@ -0,0 +1,19 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant
description: Min/max range.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
// constructing from ns
var limit = 8640000000000000000000n;
assert.throws(RangeError, () => new Temporal.Instant(-limit - 1n));
assert.throws(RangeError, () => new Temporal.Instant(limit + 1n));
TemporalHelpers.assertInstantsEqual(new Temporal.Instant(-limit),
Temporal.Instant.from("-271821-04-20T00:00:00Z"));
TemporalHelpers.assertInstantsEqual(new Temporal.Instant(limit),
Temporal.Instant.from("+275760-09-13T00:00:00Z"));

View File

@ -0,0 +1,35 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant.prototype.add
description: Cross-epoch arithmetic.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const inst = Temporal.Instant.from("1969-12-25T12:23:45.678901234Z");
// cross epoch in ms
const one = inst.subtract({
hours: 240,
nanoseconds: 800
});
const two = inst.add({
hours: 240,
nanoseconds: 800
});
const three = two.subtract({
hours: 480,
nanoseconds: 1600
});
const four = one.add({
hours: 480,
nanoseconds: 1600
});
TemporalHelpers.assertInstantsEqual(one, Temporal.Instant.from("1969-12-15T12:23:45.678900434Z"),
`(${ inst }).subtract({ hours: 240, nanoseconds: 800 }) = ${ one }`);
TemporalHelpers.assertInstantsEqual(two, Temporal.Instant.from("1970-01-04T12:23:45.678902034Z"),
`(${ inst }).add({ hours: 240, nanoseconds: 800 }) = ${ two }`);
TemporalHelpers.assertInstantsEqual(three, one, `(${ two }).subtract({ hours: 480, nanoseconds: 1600 }) = ${ one }`);
TemporalHelpers.assertInstantsEqual(four, two, `(${ one }).add({ hours: 480, nanoseconds: 1600 }) = ${ two }`);

View File

@ -2,14 +2,14 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.equals works
esid: sec-temporal.instant.prototype.equals
description: Temporal.Instant.equals works cross-epoch
features: [Temporal]
---*/
var i1 = Temporal.Instant.from("1963-02-13T09:36:29.123456789Z");
var i2 = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
var i3 = Temporal.Instant.from("1981-12-15T14:34:31.987654321Z");
const i1 = Temporal.Instant.from("1963-02-13T09:36:29.123456789Z");
const i2 = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const i3 = Temporal.Instant.from("1981-12-15T14:34:31.987654321Z");
// pre epoch equal
assert(i1.equals(i1))

View File

@ -0,0 +1,23 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() accepts plural units.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const inst = new Temporal.Instant(1_000_000_000_123_456_789n);
[
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
TemporalHelpers.assertInstantsEqual(inst.round({ smallestUnit }),
inst.round({ smallestUnit: `${ smallestUnit }s` }));
});

View File

@ -0,0 +1,23 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() accepts string parameter as shortcut for {smallestUnit}.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const inst = new Temporal.Instant(1_000_000_000_123_456_789n);
[
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
TemporalHelpers.assertInstantsEqual(inst.round(smallestUnit),
inst.round({ smallestUnit }));
});

View File

@ -0,0 +1,15 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() allows increments that divide evenly into solar days.
features: [Temporal]
---*/
const inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
assert(inst.round({
smallestUnit: "second",
roundingIncrement: 864
}) instanceof Temporal.Instant);

View File

@ -0,0 +1,29 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: Rounds to days by specifying increments of 86400 seconds in various units
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
const expected = Temporal.Instant.from("1976-11-19T00:00:00Z");
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "hour",
roundingIncrement: 24
}), expected);
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "minute",
roundingIncrement: 1440
}), expected);
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "second",
roundingIncrement: 86400
}), expected);
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "millisecond",
roundingIncrement: 86400000
}), expected);

View File

@ -0,0 +1,47 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() throws without required smallestUnit parameter.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
// rounds to an increment of hours
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "hour",
roundingIncrement: 4
}), Temporal.Instant.from("1976-11-18T16:00:00Z"));
// rounds to an increment of minutes
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "minute",
roundingIncrement: 15
}), Temporal.Instant.from("1976-11-18T14:30:00Z"));
// rounds to an increment of seconds
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "second",
roundingIncrement: 30
}), Temporal.Instant.from("1976-11-18T14:23:30Z"));
// rounds to an increment of milliseconds
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "millisecond",
roundingIncrement: 10
}), Temporal.Instant.from("1976-11-18T14:23:30.12Z"));
// rounds to an increment of microseconds
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "microsecond",
roundingIncrement: 10
}), Temporal.Instant.from("1976-11-18T14:23:30.12346Z"));
// rounds to an increment of nanoseconds
TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}), Temporal.Instant.from("1976-11-18T14:23:30.12345679Z"));

View File

@ -0,0 +1,35 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() throws on increments that do not divide evenly into solar days.
features: [Temporal]
---*/
const inst = new Temporal.Instant(0n);
assert.throws(RangeError, () => inst.round({
smallestUnit: "hour",
roundingIncrement: 7
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "minute",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "second",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "millisecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "microsecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "nanosecond",
roundingIncrement: 29
}));

View File

@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.round
description: round() throws without required smallestUnit parameter.
features: [Temporal]
---*/
const inst = new Temporal.Instant(0n);
assert.throws(RangeError, () => inst.round({}));
assert.throws(RangeError, () => inst.round({
roundingIncrement: 1,
roundingMode: "ceil"
}));

View File

@ -0,0 +1,18 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: since() works.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const diff = later.since(earlier);
TemporalHelpers.assertDurationsEqual(earlier.since(later), diff.negated());
TemporalHelpers.assertDurationsEqual(earlier.until(later), diff);
TemporalHelpers.assertInstantsEqual(earlier.add(diff), later);
TemporalHelpers.assertInstantsEqual(later.subtract(diff), earlier);

View File

@ -0,0 +1,77 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: Test invalid increments.
features: [Temporal]
---*/
const earlier = new Temporal.Instant(0n);
const later = new Temporal.Instant(1_000_000_000_000_000_000n);
const largestUnit = "hours";
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 11
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 24
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 1000
}));

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: Assumes a different default for largestUnit if smallestUnit is larger than seconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
TemporalHelpers.assertDuration(later.since(earlier, {
smallestUnit: "hours",
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(later.since(earlier, {
smallestUnit: "minutes",
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 0, 22586123, 0, 0, 0, 0);

View File

@ -0,0 +1,18 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: since() can return minutes and hours.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
// can return minutes and hours
TemporalHelpers.assertDuration(feb21.since(feb20, { largestUnit: "hours" }),
0, 0, 0, 0, 8784, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(feb21.since(feb20, { largestUnit: "minutes" }),
0, 0, 0, 0, 0, 527040, 0, 0, 0, 0);

View File

@ -0,0 +1,15 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: Options may be a function object.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
TemporalHelpers.assertDuration(feb21.since(feb20, () => {}),
0, 0, 0, 0, 0, 0, 31622400, 0, 0, 0);

View File

@ -0,0 +1,62 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: Test different rounding increments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const largestUnit = "hours";
// rounds to an increment of hours
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 3,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376434, 0, 0, 0, 0, 0);
// rounds to an increment of minutes
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 30,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 30, 0, 0, 0, 0);
// rounds to an increment of seconds
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 15,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 23, 15, 0, 0, 0);
// rounds to an increment of milliseconds
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 23, 8, 150, 0, 0);
// rounds to an increment of microseconds
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 23, 8, 148, 530, 0);
// rounds to an increment of nanoseconds
TemporalHelpers.assertDuration(later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 376435, 23, 8, 148, 529, 310);

View File

@ -0,0 +1,30 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: since() can return subseconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const latersub = feb20.add({
hours: 24,
milliseconds: 250,
microseconds: 250,
nanoseconds: 250
});
const msDiff = latersub.since(feb20, { largestUnit: "milliseconds" });
TemporalHelpers.assertDuration(msDiff,
0, 0, 0, 0, 0, 0, 0, 86400250, 250, 250);
const µsDiff = latersub.since(feb20, { largestUnit: "microseconds" });
TemporalHelpers.assertDuration(µsDiff,
0, 0, 0, 0, 0, 0, 0, 0, 86400250250, 250);
const nsDiff = latersub.since(feb20, { largestUnit: "nanoseconds" });
TemporalHelpers.assertDuration(nsDiff,
0, 0, 0, 0, 0, 0, 0, 0, 0, 86400250250250);

View File

@ -0,0 +1,89 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.since
description: Test valid increments.
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const largestUnit = "hours";
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit: "hours",
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
// valid increments divide into 60
[
"minutes",
"seconds"
].forEach(smallestUnit => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
});
// valid increments divide into 1000
[
"milliseconds",
"microseconds",
"nanoseconds"
].forEach(smallestUnit => {
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
});

View File

@ -0,0 +1,12 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tozoneddatetimeiso
description: toZonedDateTimeISO() throws without parameter.
features: [Temporal]
---*/
const inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
assert.throws(TypeError, () => inst.toZonedDateTimeISO());

View File

@ -0,0 +1,20 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tozoneddatetimeiso
description: Test time zone parameters.
features: [Temporal]
---*/
const inst = new Temporal.Instant(1_000_000_000_000_000_000n);
// time zone parameter UTC
const zdt = inst.toZonedDateTimeISO("UTC");
assert.sameValue(inst.epochNanoseconds, zdt.epochNanoseconds);
assert.sameValue(zdt.timeZoneId, "UTC");
// time zone parameter non-UTC
const zdtNonUTC = inst.toZonedDateTimeISO("-05:00");
assert.sameValue(inst.epochNanoseconds, zdtNonUTC.epochNanoseconds);
assert.sameValue(zdtNonUTC.timeZoneId, "-05:00");

View File

@ -0,0 +1,19 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: until() works.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const diff = earlier.until(later);
TemporalHelpers.assertDurationsEqual(later.until(earlier), diff.negated());
TemporalHelpers.assertDurationsEqual(later.since(earlier), diff);
TemporalHelpers.assertInstantsEqual(earlier.add(diff), later);
TemporalHelpers.assertInstantsEqual(later.subtract(diff), earlier);

View File

@ -0,0 +1,76 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Test various rounding increments.
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const largestUnit = "hours";
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 11
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 24
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 60
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 60
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 1000
}));

View File

@ -0,0 +1,21 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Assumes a different default for largestUnit if smallestUnit is larger than seconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
TemporalHelpers.assertDuration(earlier.until(later, {
smallestUnit: "hours",
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440610, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(earlier.until(later, {
smallestUnit: "minutes",
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 0, 26436596, 0, 0, 0, 0);

View File

@ -0,0 +1,17 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Can return minutes and hours.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
TemporalHelpers.assertDuration(feb20.until(feb21, { largestUnit: "hours" }),
0, 0, 0, 0, 8784, 0, 0, 0, 0, 0);
TemporalHelpers.assertDuration(feb20.until(feb21, { largestUnit: "minutes" }),
0, 0, 0, 0, 0, 527040, 0, 0, 0, 0);

View File

@ -0,0 +1,15 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Options may be a function object.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
TemporalHelpers.assertDuration(feb20.until(feb21, () => {}),
0, 0, 0, 0, 0, 0, 31622400, 0, 0, 0);

View File

@ -0,0 +1,62 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Test various rounding increments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const largestUnit = "hours";
// rounds to an increment of hours
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 4,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440608, 0, 0, 0, 0, 0);
// rounds to an increment of minutes
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 30,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440610, 0, 0, 0, 0, 0);
// rounds to an increment of seconds
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 15,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440609, 56, 0, 0, 0, 0);
// rounds to an increment of milliseconds
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440609, 56, 3, 150, 0, 0);
// rounds to an increment of microseconds
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440609, 56, 3, 148, 530, 0);
// rounds to an increment of nanoseconds
TemporalHelpers.assertDuration(earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), 0, 0, 0, 0, 440609, 56, 3, 148, 529, 310);

View File

@ -0,0 +1,31 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Can return subseconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/
const feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
const feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
const latersub = feb20.add({
hours: 24,
milliseconds: 250,
microseconds: 250,
nanoseconds: 250
});
const msDiff = feb20.until(latersub, { largestUnit: "milliseconds" });
TemporalHelpers.assertDuration(msDiff,
0, 0, 0, 0, 0, 0, 0, 86400250, 250, 250);
const µsDiff = feb20.until(latersub, { largestUnit: "microseconds" });
TemporalHelpers.assertDuration(µsDiff,
0, 0, 0, 0, 0, 0, 0, 0, 86400250250, 250);
const nsDiff = feb20.until(latersub, { largestUnit: "nanoseconds" });
TemporalHelpers.assertDuration(nsDiff,
0, 0, 0, 0, 0, 0, 0, 0, 0, 86400250250250);

View File

@ -0,0 +1,89 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.until
description: Test various rounding increments.
features: [Temporal]
---*/
const earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
const later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
const largestUnit = "hours";
// valid hour increments divide into 24
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit: "hours",
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
// valid increments divide into 60
[
"minutes",
"seconds"
].forEach(smallestUnit => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
});
// valid increments divide into 1000
[
"milliseconds",
"microseconds",
"nanoseconds"
].forEach(smallestUnit => {
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
const options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
});

View File

@ -1,32 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.add works
features: [Temporal]
---*/
var inst = Temporal.Instant.from("1969-12-25T12:23:45.678901234Z");
// cross epoch in ms
var one = inst.subtract({
hours: 240,
nanoseconds: 800
});
var two = inst.add({
hours: 240,
nanoseconds: 800
});
var three = two.subtract({
hours: 480,
nanoseconds: 1600
});
var four = one.add({
hours: 480,
nanoseconds: 1600
});
assert.sameValue(`${ one }`, "1969-12-15T12:23:45.678900434Z", `(${ inst }).subtract({ hours: 240, nanoseconds: 800 }) = ${ one }`);
assert.sameValue(`${ two }`, "1970-01-04T12:23:45.678902034Z", `(${ inst }).add({ hours: 240, nanoseconds: 800 }) = ${ two }`);
assert(three.equals(one), `(${ two }).subtract({ hours: 480, nanoseconds: 1600 }) = ${ one }`);
assert(four.equals(two), `(${ one }).add({ hours: 480, nanoseconds: 1600 }) = ${ two }`);

View File

@ -1,23 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Min/max range
features: [Temporal]
---*/
// constructing from ns
var limit = 8640000000000000000000n;
assert.throws(RangeError, () => new Temporal.Instant(-limit - 1n));
assert.throws(RangeError, () => new Temporal.Instant(limit + 1n));
assert.sameValue(`${ new Temporal.Instant(-limit) }`, "-271821-04-20T00:00:00Z");
assert.sameValue(`${ new Temporal.Instant(limit) }`, "+275760-09-13T00:00:00Z");
// constructing from ms
var limit = 8640000000000000;
assert.throws(RangeError, () => Temporal.Instant.fromEpochMilliseconds(-limit - 1));
assert.throws(RangeError, () => Temporal.Instant.fromEpochMilliseconds(limit + 1));
assert.sameValue(`${ Temporal.Instant.fromEpochMilliseconds(-limit) }`, "-271821-04-20T00:00:00Z");
assert.sameValue(`${ Temporal.Instant.fromEpochMilliseconds(limit) }`, "+275760-09-13T00:00:00Z");

View File

@ -1,128 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.round works
features: [Temporal]
---*/
var inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
// throws without required smallestUnit parameter
assert.throws(RangeError, () => inst.round({}));
assert.throws(RangeError, () => inst.round({
roundingIncrement: 1,
roundingMode: "ceil"
}));
// rounds to an increment of hours
assert.sameValue(`${ inst.round({
smallestUnit: "hour",
roundingIncrement: 4
}) }`, "1976-11-18T16:00:00Z");
// rounds to an increment of minutes
assert.sameValue(`${ inst.round({
smallestUnit: "minute",
roundingIncrement: 15
}) }`, "1976-11-18T14:30:00Z");
// rounds to an increment of seconds
assert.sameValue(`${ inst.round({
smallestUnit: "second",
roundingIncrement: 30
}) }`, "1976-11-18T14:23:30Z");
// rounds to an increment of milliseconds
assert.sameValue(`${ inst.round({
smallestUnit: "millisecond",
roundingIncrement: 10
}) }`, "1976-11-18T14:23:30.12Z");
// rounds to an increment of microseconds
assert.sameValue(`${ inst.round({
smallestUnit: "microsecond",
roundingIncrement: 10
}) }`, "1976-11-18T14:23:30.12346Z");
// rounds to an increment of nanoseconds
assert.sameValue(`${ inst.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}) }`, "1976-11-18T14:23:30.12345679Z");
// rounds to days by specifying increment of 86400 seconds in various units
var expected = "1976-11-19T00:00:00Z";
assert.sameValue(`${ inst.round({
smallestUnit: "hour",
roundingIncrement: 24
}) }`, expected);
assert.sameValue(`${ inst.round({
smallestUnit: "minute",
roundingIncrement: 1440
}) }`, expected);
assert.sameValue(`${ inst.round({
smallestUnit: "second",
roundingIncrement: 86400
}) }`, expected);
assert.sameValue(`${ inst.round({
smallestUnit: "millisecond",
roundingIncrement: 86400000
}) }`, expected);
// allows increments that divide evenly into solar days
assert(inst.round({
smallestUnit: "second",
roundingIncrement: 864
}) instanceof Temporal.Instant);
// throws on increments that do not divide evenly into solar days
assert.throws(RangeError, () => inst.round({
smallestUnit: "hour",
roundingIncrement: 7
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "minute",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "second",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "millisecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "microsecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => inst.round({
smallestUnit: "nanosecond",
roundingIncrement: 29
}));
// accepts plural units
[
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
assert(inst.round({ smallestUnit }).equals(inst.round({ smallestUnit: `${ smallestUnit }s` })));
});
// accepts string parameter as shortcut for {smallestUnit}
[
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
assert(inst.round(smallestUnit).equals(inst.round({ smallestUnit })));
});

View File

@ -1,246 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.since() works
features: [Temporal]
---*/
var earlier = Temporal.Instant.from("1976-11-18T15:23:30.123456789Z");
var later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
var diff = later.since(earlier);
assert.sameValue(`${ earlier.since(later) }`, `${ diff.negated() }`)
assert.sameValue(`${ earlier.until(later) }`, `${ diff }`)
assert(earlier.add(diff).equals(later))
assert(later.subtract(diff).equals(earlier))
var feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
var feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
// can return minutes and hours
assert.sameValue(`${ feb21.since(feb20, { largestUnit: "hours" }) }`, "PT8784H");
assert.sameValue(`${ feb21.since(feb20, { largestUnit: "minutes" }) }`, "PT527040M");
// can return subseconds
var latersub = feb20.add({
hours: 24,
milliseconds: 250,
microseconds: 250,
nanoseconds: 250
});
var msDiff = latersub.since(feb20, { largestUnit: "milliseconds" });
assert.sameValue(msDiff.seconds, 0);
assert.sameValue(msDiff.milliseconds, 86400250);
assert.sameValue(msDiff.microseconds, 250);
assert.sameValue(msDiff.nanoseconds, 250);
var µsDiff = latersub.since(feb20, { largestUnit: "microseconds" });
assert.sameValue(µsDiff.milliseconds, 0);
assert.sameValue(µsDiff.microseconds, 86400250250);
assert.sameValue(µsDiff.nanoseconds, 250);
var nsDiff = latersub.since(feb20, { largestUnit: "nanoseconds" });
assert.sameValue(nsDiff.microseconds, 0);
assert.sameValue(nsDiff.nanoseconds, 86400250250250);
// options may be a function object
assert.sameValue(`${ feb21.since(feb20, () => {
}) }`, "PT31622400S");
// assumes a different default for largestUnit if smallestUnit is larger than seconds
assert.sameValue(`${ later.since(earlier, {
smallestUnit: "hours",
roundingMode: "halfExpand"
}) }`, "PT376435H");
assert.sameValue(`${ later.since(earlier, {
smallestUnit: "minutes",
roundingMode: "halfExpand"
}) }`, "PT22586123M");
var largestUnit = "hours";
// rounds to an increment of hours
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 3,
roundingMode: "halfExpand"
}) }`, "PT376434H");
// rounds to an increment of minutes
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 30,
roundingMode: "halfExpand"
}) }`, "PT376435H30M");
// rounds to an increment of seconds
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 15,
roundingMode: "halfExpand"
}) }`, "PT376435H23M15S");
// rounds to an increment of milliseconds
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT376435H23M8.15S");
// rounds to an increment of microseconds
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT376435H23M8.14853S");
// rounds to an increment of nanoseconds
assert.sameValue(`${ later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT376435H23M8.14852931S");
// valid hour increments divide into 24
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit: "hours",
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
// valid increments divide into 60
[
"minutes",
"seconds"
].forEach(smallestUnit => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
});
// valid increments divide into 1000
[
"milliseconds",
"microseconds",
"nanoseconds"
].forEach(smallestUnit => {
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(later.since(earlier, options) instanceof Temporal.Duration);
});
});
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 11
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 24
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 1000
}));

View File

@ -1,23 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.toZonedDateTimeISO() works
features: [Temporal]
---*/
var inst = Temporal.Instant.from("1976-11-18T14:23:30.123456789Z");
// throws without parameter
assert.throws(TypeError, () => inst.toZonedDateTimeISO());
// time zone parameter UTC
var zdt = inst.toZonedDateTimeISO("UTC");
assert.sameValue(inst.epochNanoseconds, zdt.epochNanoseconds);
assert.sameValue(`${ zdt }`, "1976-11-18T14:23:30.123456789+00:00[UTC]");
// time zone parameter non-UTC
var zdt = inst.toZonedDateTimeISO("-05:00");
assert.sameValue(inst.epochNanoseconds, zdt.epochNanoseconds);
assert.sameValue(`${ zdt }`, "1976-11-18T09:23:30.123456789-05:00[-05:00]");

View File

@ -1,246 +0,0 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal-instant-objects
description: Temporal.Instant.until() works
features: [Temporal]
---*/
var earlier = Temporal.Instant.from("1969-07-24T16:50:35.123456789Z");
var later = Temporal.Instant.from("2019-10-29T10:46:38.271986102Z");
var diff = earlier.until(later);
assert.sameValue(`${ later.until(earlier) }`, `${ diff.negated() }`)
assert.sameValue(`${ later.since(earlier) }`, `${ diff }`)
assert(earlier.add(diff).equals(later))
assert(later.subtract(diff).equals(earlier))
var feb20 = Temporal.Instant.from("2020-02-01T00:00Z");
var feb21 = Temporal.Instant.from("2021-02-01T00:00Z");
// can return minutes and hours
assert.sameValue(`${ feb20.until(feb21, { largestUnit: "hours" }) }`, "PT8784H");
assert.sameValue(`${ feb20.until(feb21, { largestUnit: "minutes" }) }`, "PT527040M");
// can return subseconds
var latersub = feb20.add({
hours: 24,
milliseconds: 250,
microseconds: 250,
nanoseconds: 250
});
var msDiff = feb20.until(latersub, { largestUnit: "milliseconds" });
assert.sameValue(msDiff.seconds, 0);
assert.sameValue(msDiff.milliseconds, 86400250);
assert.sameValue(msDiff.microseconds, 250);
assert.sameValue(msDiff.nanoseconds, 250);
var µsDiff = feb20.until(latersub, { largestUnit: "microseconds" });
assert.sameValue(µsDiff.milliseconds, 0);
assert.sameValue(µsDiff.microseconds, 86400250250);
assert.sameValue(µsDiff.nanoseconds, 250);
var nsDiff = feb20.until(latersub, { largestUnit: "nanoseconds" });
assert.sameValue(nsDiff.microseconds, 0);
assert.sameValue(nsDiff.nanoseconds, 86400250250250);
// options may be a function object
assert.sameValue(`${ feb20.until(feb21, () => {
}) }`, "PT31622400S");
// assumes a different default for largestUnit if smallestUnit is larger than seconds
assert.sameValue(`${ earlier.until(later, {
smallestUnit: "hours",
roundingMode: "halfExpand"
}) }`, "PT440610H");
assert.sameValue(`${ earlier.until(later, {
smallestUnit: "minutes",
roundingMode: "halfExpand"
}) }`, "PT26436596M");
var largestUnit = "hours";
// rounds to an increment of hours
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 4,
roundingMode: "halfExpand"
}) }`, "PT440608H");
// rounds to an increment of minutes
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 30,
roundingMode: "halfExpand"
}) }`, "PT440610H");
// rounds to an increment of seconds
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 15,
roundingMode: "halfExpand"
}) }`, "PT440609H56M");
// rounds to an increment of milliseconds
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT440609H56M3.15S");
// rounds to an increment of microseconds
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT440609H56M3.14853S");
// rounds to an increment of nanoseconds
assert.sameValue(`${ earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}) }`, "PT440609H56M3.14852931S");
// valid hour increments divide into 24
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit: "hours",
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
// valid increments divide into 60
[
"minutes",
"seconds"
].forEach(smallestUnit => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
});
// valid increments divide into 1000
[
"milliseconds",
"microseconds",
"nanoseconds"
].forEach(smallestUnit => {
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
var options = {
largestUnit,
smallestUnit,
roundingIncrement
};
assert(earlier.until(later, options) instanceof Temporal.Duration);
});
});
// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 11
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 29
}));
// throws on increments that are equal to the next highest
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "hours",
roundingIncrement: 24
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "minutes",
roundingIncrement: 60
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "seconds",
roundingIncrement: 60
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "milliseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "microseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => earlier.until(later, {
largestUnit,
smallestUnit: "nanoseconds",
roundingIncrement: 1000
}));