Skip to content

Commit

Permalink
Temporal: Move ZonedDateTime/prototype/round tests and one other Zone…
Browse files Browse the repository at this point in the history
…dDateTime test out of staging
  • Loading branch information
catamorphism authored and ptomato committed Oct 31, 2024
1 parent 70ef1ac commit de68177
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 250 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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.zoneddatetime.prototype.round
description: round() rounds to various increments.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");
// "1976-11-18T16:00:00+01:00[+01:00]"
const expectedHours = new Temporal.ZonedDateTime(217177200000000000n, "+01:00");
// "1976-11-18T15:30:00+01:00[+01:00]"
const expectedMinutes = new Temporal.ZonedDateTime(217175400000000000n, "+01:00");
// "1976-11-18T15:23:30+01:00[+01:00]"
const expectedSeconds = new Temporal.ZonedDateTime(217175010000000000n, "+01:00");
// "1976-11-18T15:23:30.12+01:00[+01:00]");
const expectedMilliseconds = new Temporal.ZonedDateTime(217175010120000000n, "+01:00");
// "1976-11-18T15:23:30.12346+01:00[+01:00]");
const expectedMicroseconds = new Temporal.ZonedDateTime(217175010123460000n, "+01:00");
// "1976-11-18T15:23:30.12345679+01:00[+01:00]");
const expectedNanoseconds = new Temporal.ZonedDateTime(217175010123456790n, "+01:00");
// "1976-11-19T00:00:00+01:00[+01:00]");
const expected1Day = new Temporal.ZonedDateTime(217206000000000000n, "+01:00");

// rounds to an increment of hours
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "hour",
roundingIncrement: 4
}), expectedHours);

// rounds to an increment of minutes
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "minute",
roundingIncrement: 15
}), expectedMinutes);

// rounds to an increment of seconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "second",
roundingIncrement: 30
}), expectedSeconds);

// rounds to an increment of milliseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 10
}), expectedMilliseconds);

// rounds to an increment of microseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 10
}), expectedMicroseconds);

// rounds to an increment of nanoseconds
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}), expectedNanoseconds);

// 1 day is a valid increment
TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
smallestUnit: "day",
roundingIncrement: 1
}), expected1Day);

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.round
description: Test various smallestUnit values.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

// const bal = Temporal.ZonedDateTime.from("1976-11-18T23:59:59.999999999+01:00[+01:00]");
const bal = new Temporal.ZonedDateTime(217205999999999999n, "+01:00");
// "1976-11-19T00:00:00+01:00[+01:00]"
const expected = new Temporal.ZonedDateTime(217206000000000000n, "+01:00");

[
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond"
].forEach(smallestUnit => {
TemporalHelpers.assertZonedDateTimesEqual(bal.round( { smallestUnit }),
expected);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.round
description: Throws on invalid increments.
features: [Temporal]
---*/

const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");

// throws on increments that do not divide evenly into the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "day",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 29
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 29
}));

// throws on increments that are equal to the next highest
assert.throws(RangeError, () => zdt.round({
smallestUnit: "hour",
roundingIncrement: 24
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "minute",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "second",
roundingIncrement: 60
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "millisecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "microsecond",
roundingIncrement: 1000
}));
assert.throws(RangeError, () => zdt.round({
smallestUnit: "nanosecond",
roundingIncrement: 1000
}));
Original file line number Diff line number Diff line change
@@ -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.zoneddatetime.prototype.round
description: Throws without parameter.
features: [Temporal]
---*/

const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");

assert.throws(TypeError, () => zdt.round());
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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.round
description: Throws without required smallestUnit parameter.
features: [Temporal]
---*/

const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");

assert.throws(RangeError, () => zdt.round({}));
assert.throws(RangeError, () => zdt.round({
roundingIncrement: 1,
roundingMode: "ceil"
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.zoneddatetime.prototype.round
description: Validity of increments depends on divisibility.
features: [Temporal]
---*/

const zdt = new Temporal.ZonedDateTime(217175010123456789n, "+01:00");

// valid hour increments divide into 24
const smallestUnit = "hour";
[
1,
2,
3,
4,
6,
8,
12
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
[
"minute",
"second"
].forEach(smallestUnit => {
// valid minutes/seconds increments divide into 60`, () => {
[
1,
2,
3,
4,
5,
6,
10,
12,
15,
20,
30
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});
[
"millisecond",
"microsecond",
"nanosecond"
].forEach(smallestUnit => {
// valid increments divide into 1000`
[
1,
2,
4,
5,
8,
10,
20,
25,
40,
50,
100,
125,
200,
250,
500
].forEach(roundingIncrement => {
assert(zdt.round({
smallestUnit,
roundingIncrement
}) instanceof Temporal.ZonedDateTime);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.prototype.since
description: Reversibility of differences.
includes: [temporalHelpers.js]
features: [Temporal]
---*/

/*
const earlier = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789-03:00[-03:00]");
const later = Temporal.ZonedDateTime.from("2019-10-29T10:46:38.271986102-03:00[-03:00]");
*/
const earlier = new Temporal.ZonedDateTime(217189410123456789n, "-03:00");
const later = new Temporal.ZonedDateTime(1572356798271986102n, "-03:00");

[
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
const diff = later.since(earlier, { largestUnit });
TemporalHelpers.assertDurationsEqual(earlier.since(later, { largestUnit }),
diff.negated())
TemporalHelpers.assertDurationsEqual(earlier.until(later, { largestUnit }),
diff);
// difference symmetrical with regard to negative durations
assert(earlier.subtract(diff.negated()).equals(later));
assert(later.add(diff.negated()).equals(earlier));
});

[
"years",
"months",
"weeks",
"days",
"hours",
"minutes",
"seconds"
].forEach(largestUnit => {
const diff1 = earlier.until(later, { largestUnit });
const diff2 = later.since(earlier, { largestUnit });
assert(earlier.add(diff1).equals(later));
assert(later.subtract(diff2).equals(earlier));
});

This file was deleted.

Loading

0 comments on commit de68177

Please sign in to comment.