Skip to content

Commit

Permalink
Temporal: Move tests for ZonedDateTime/p/since out of staging
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Oct 29, 2024
1 parent 01776c3 commit ab77bbb
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 279 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.zoneddatetime.prototype.since
description: Can return lower or higher units.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const feb20 = Temporal.ZonedDateTime.from("2020-02-01T00:00+01:00[+01:00]");
const feb21 = Temporal.ZonedDateTime.from("2021-02-01T00:00+01:00[+01:00]");
*/
const feb20 = new Temporal.ZonedDateTime(1580511600000000000n, "+01:00");
const feb21 = new Temporal.ZonedDateTime(1612134000000000000n, "+01:00");

TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "years" }),
new Temporal.Duration(1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "months" }),
new Temporal.Duration(0, 12, 0, 0, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "weeks" }),
new Temporal.Duration(0, 0, 52, 2, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "days" }),
new Temporal.Duration(0, 0, 0, 366, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "minutes" }),
new Temporal.Duration(0, 0, 0, 0, 0, 527040, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "seconds" }),
new Temporal.Duration(0, 0, 0, 0, 0, 0, 31622400, 0, 0, 0));
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.zoneddatetime.prototype.since
description: Can return subseconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const feb20 = Temporal.ZonedDateTime.from("2020-02-01T00:00+01:00[+01:00]");
*/
const feb20 = new Temporal.ZonedDateTime(1580511600000000000n, "+01:00");
const later = feb20.add({
days: 1,
milliseconds: 250,
microseconds: 250,
nanoseconds: 250
});

const msDiff = later.since(feb20, { largestUnit: "milliseconds" });
assert.sameValue(msDiff.seconds, 0);
assert.sameValue(msDiff.milliseconds, 86400250);
assert.sameValue(msDiff.microseconds, 250);
assert.sameValue(msDiff.nanoseconds, 250);
const µsDiff = later.since(feb20, { largestUnit: "microseconds" });
assert.sameValue(µsDiff.milliseconds, 0);
assert.sameValue(µsDiff.microseconds, 86400250250);
assert.sameValue(µsDiff.nanoseconds, 250);
const nsDiff = later.since(feb20, { largestUnit: "nanoseconds" });
assert.sameValue(nsDiff.microseconds, 0);
assert.sameValue(nsDiff.nanoseconds, 86400250250250);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.zoneddatetime.prototype.since
description: since() casts argument from object or string.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

// var zdt = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789+01:00[+01:00]");
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
// "2019-10-29T10:46:38.271986102+01:00[+01:00]"
const zdt2 = new Temporal.ZonedDateTime(1572342398271986102n, "+01:00");
const d1 = new Temporal.Duration(0, 0, 0, 0, -376434, -36, -29, -876, -543, -211);
const d2 = new Temporal.Duration(0, 0, 0, 0, -376435, -23, -8, -148, -529, -313);

TemporalHelpers.assertDurationsEqual(zdt.since({
year: 2019,
month: 10,
day: 29,
hour: 10,
timeZone: "+01:00"
}), d1);
TemporalHelpers.assertDurationsEqual(zdt.since(zdt2), d2);




Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.zoneddatetime.prototype.since
description: Can return subseconds.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const lastFeb20 = Temporal.ZonedDateTime.from("2020-02-29T00:00+01:00[+01:00]");
const lastFeb21 = Temporal.ZonedDateTime.from("2021-02-28T00:00+01:00[+01:00]");
*/
const lastFeb20 = new Temporal.ZonedDateTime(1582930800000000000n, "+01:00");
const lastFeb21 = new Temporal.ZonedDateTime(1614466800000000000n, "+01:00");

TemporalHelpers.assertDurationsEqual(lastFeb21.since(lastFeb20),
new Temporal.Duration(0, 0, 0, 0, 8760, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(lastFeb21.since(lastFeb20, { largestUnit: "months" }),
new Temporal.Duration(0, 11, 0, 28, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(lastFeb21.since(lastFeb20, { largestUnit: "years" }),
new Temporal.Duration(0, 11, 0, 28, 0, 0, 0, 0, 0, 0));
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.zoneddatetime.prototype.since
description: Rounds to various increments.
features: [Temporal]
---*/

/*
const earlier = Temporal.ZonedDateTime.from('2019-01-08T09:22:36.123456789+01:00[+01:00]');
const later = Temporal.ZonedDateTime.from('2021-09-07T13:39:40.987654321+01:00[+01:00]');
*/
const earlier = new Temporal.ZonedDateTime(1546935756123456789n, "+01:00");
const later = new Temporal.ZonedDateTime(1631018380987654321n, "+01:00");

// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "hours",
roundingIncrement: 11
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "minutes",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "seconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "milliseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "microseconds",
roundingIncrement: 29
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "nanoseconds",
roundingIncrement: 29
}));

// throws on increments that are equal to the next highest
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "hours",
roundingIncrement: 24
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "minutes",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "seconds",
roundingIncrement: 60
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "milliseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "microseconds",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => later.since(earlier, {
smallestUnit: "nanoseconds",
roundingIncrement: 1000
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.zoneddatetime.prototype.since
description: Assumes a different default for largestUnit if smallestUnit is larger than days
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const earlier = Temporal.ZonedDateTime.from('2019-01-08T09:22:36.123456789+01:00[+01:00]');
const later = Temporal.ZonedDateTime.from('2021-09-07T13:39:40.987654321+01:00[+01:00]');
*/
const earlier = new Temporal.ZonedDateTime(1546935756123456789n, "+01:00");
const later = new Temporal.ZonedDateTime(1631018380987654321n, "+01:00");

TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "years",
roundingMode: "halfExpand"
}), new Temporal.Duration(3, 0, 0, 0, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "months",
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 32, 0, 0, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "weeks",
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 139, 0, 0, 0, 0, 0, 0, 0));


Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.zoneddatetime.prototype.since
description: Defaults to returning hours.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const feb20 = Temporal.ZonedDateTime.from("2020-02-01T00:00+01:00[+01:00]");
const feb21 = Temporal.ZonedDateTime.from("2021-02-01T00:00+01:00[+01:00]");
*/
const feb20 = new Temporal.ZonedDateTime(1580511600000000000n, "+01:00");
const feb21 = new Temporal.ZonedDateTime(1612134000000000000n, "+01:00");
// "2021-02-01T00:00:00.000000001+01:00[+01:00]"
const feb1_2021 = new Temporal.ZonedDateTime(1612134000000000001n, "+01:00");
// "2020-02-01T00:00:00.000000001+01:00[+01:00]"
const feb1_2020 = new Temporal.ZonedDateTime(1580511600000000001n, "+01:00");
const d1 = new Temporal.Duration(0, 0, 0, 0, 8784, 0, 0, 0, 0, 0);
const d2 = new Temporal.Duration(0, 0, 0, 0, 8784, 0, 0, 0, 0, 1);
const d3 = new Temporal.Duration(0, 0, 0, 0, 8783, 59, 59, 999, 999, 999);

TemporalHelpers.assertDurationsEqual(feb21.since(feb20), d1);
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "auto" }), d1);
TemporalHelpers.assertDurationsEqual(feb21.since(feb20, { largestUnit: "hours" }), d1);
TemporalHelpers.assertDurationsEqual(feb1_2021.since(feb20), d2);
TemporalHelpers.assertDurationsEqual(feb21.since(feb1_2020), d3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.zoneddatetime.prototype.since
description: Rounds to various increments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const earlier = Temporal.ZonedDateTime.from('2019-01-08T09:22:36.123456789+01:00[+01:00]');
const later = Temporal.ZonedDateTime.from('2021-09-07T13:39:40.987654321+01:00[+01:00]');
*/
const earlier = new Temporal.ZonedDateTime(1546935756123456789n, "+01:00");
const later = new Temporal.ZonedDateTime(1631018380987654321n, "+01:00");

// rounds to an increment of hours
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "hours",
roundingIncrement: 3,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23355, 0, 0, 0, 0, 0));

// rounds to an increment of minutes
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "minutes",
roundingIncrement: 30,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23356, 30, 0, 0, 0, 0));

// rounds to an increment of seconds
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "seconds",
roundingIncrement: 15,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23356, 17, 0, 0, 0));

// rounds to an increment of milliseconds
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "milliseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23356, 17, 4, 860, 0, 0));

// rounds to an increment of microseconds
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "microseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23356, 17, 4, 864, 200, 0));

// rounds to an increment of nanoseconds
TemporalHelpers.assertDurationsEqual(later.since(earlier, {
smallestUnit: "nanoseconds",
roundingIncrement: 10,
roundingMode: "halfExpand"
}), new Temporal.Duration(0, 0, 0, 0, 23356, 17, 4, 864, 197, 530));
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.zoneddatetime.prototype.since
description: Rounds relative to the receiver.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

// rounds relative to the receiver
/*
const dt1 = Temporal.ZonedDateTime.from("2019-01-01T00:00+00:00[UTC]");
const dt2 = Temporal.ZonedDateTime.from("2020-07-02T00:00+00:00[UTC]");
*/
const dt1 = new Temporal.ZonedDateTime(1546300800000000000n, "UTC");
const dt2 = new Temporal.ZonedDateTime(1593648000000000000n, "UTC");

TemporalHelpers.assertDurationsEqual(dt2.since(dt1, {
smallestUnit: "years",
roundingMode: "halfExpand"
}), new Temporal.Duration(1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
TemporalHelpers.assertDurationsEqual(dt1.since(dt2, {
smallestUnit: "years",
roundingMode: "halfExpand"
}), new Temporal.Duration(-2, 0, 0, 0, 0, 0, 0, 0, 0, 0));
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.zoneddatetime.prototype.since
description: zdt.since(earlier) == earlier.until(zdt) with default options.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

// var zdt = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789+01:00[+01:00]");
const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
const earlier = new Temporal.ZonedDateTime(-120898800000000000n, "+01:00");

TemporalHelpers.assertDurationsEqual(zdt.since(earlier), earlier.until(zdt));


Loading

0 comments on commit ab77bbb

Please sign in to comment.