Temporal Helpers: Refactor sample time zone tests in harness tests

These two can be combined into one test, since they are very similar. This
will allow testing the sample DST time zone as well, which is not yet
tested.
This commit is contained in:
Philip Chimento 2023-09-05 16:31:37 -07:00 committed by Philip Chimento
parent 5c7f4009dc
commit d03ed25a21
2 changed files with 25 additions and 70 deletions

View File

@ -1,56 +0,0 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Verify the time zone arithmetic used in
TemporalHelpers.crossDateLineTimeZone() against the Pacific/Apia time zone in
the implementation's time zone database
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/
// No need to test this on hosts that don't provide an Intl object. It's
// sufficient that the logic is tested on at least one host.
if (typeof globalThis.Intl !== "undefined") {
const tz = TemporalHelpers.crossDateLineTimeZone();
const realTz = new Temporal.TimeZone('Pacific/Apia');
const shiftInstant = Temporal.Instant.from('2011-12-30T10:00:00Z');
assert.sameValue(
tz.getOffsetNanosecondsFor(shiftInstant),
realTz.getOffsetNanosecondsFor(shiftInstant),
'offset at shift instant'
);
const minus1 = shiftInstant.subtract({ hours: 1 });
assert.sameValue(
tz.getOffsetNanosecondsFor(minus1),
realTz.getOffsetNanosecondsFor(minus1),
'offset at 1 hour before shift'
);
const plus1 = shiftInstant.add({ hours: 1 });
assert.sameValue(
tz.getOffsetNanosecondsFor(plus1),
realTz.getOffsetNanosecondsFor(plus1),
'offset at 1 hour after shift'
);
const middleOfSkippedDayWallTime = Temporal.PlainDateTime.from("2011-12-30T12:00");
assert.compareArray(
tz.getPossibleInstantsFor(middleOfSkippedDayWallTime).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(middleOfSkippedDayWallTime).map((i) => i.epochNanoseconds),
'possible instants for middle of skipped day wall time'
);
const before1 = middleOfSkippedDayWallTime.subtract({ days: 1 });
assert.compareArray(
tz.getPossibleInstantsFor(before1).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(before1).map((i) => i.epochNanoseconds),
'possible instants for 1 day before skipped day'
);
const after1 = middleOfSkippedDayWallTime.add({ days: 1 });
assert.compareArray(
tz.getPossibleInstantsFor(after1).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(after1).map((i) => i.epochNanoseconds),
'possible instants for 1 day after skipped day'
);
}

View File

@ -9,13 +9,12 @@ includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/
function checkTimeZoneArithmetic(shiftInstant, shiftNs, realTimeZoneName, shiftWallTime) {
function checkTimeZoneArithmetic(shiftInstant, testWallTime, testWallDuration, tz, realTimeZoneName) {
// No need to test this on hosts that don't provide an Intl object. It's
// sufficient that the logic is tested on at least one host.
if (typeof globalThis.Intl === "undefined")
return;
const tz = TemporalHelpers.oneShiftTimeZone(shiftInstant, shiftNs);
const realTz = new Temporal.TimeZone(realTimeZoneName);
assert.sameValue(
@ -37,44 +36,56 @@ function checkTimeZoneArithmetic(shiftInstant, shiftNs, realTimeZoneName, shiftW
);
assert.compareArray(
tz.getPossibleInstantsFor(shiftWallTime).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(shiftWallTime).map((i) => i.epochNanoseconds),
tz.getPossibleInstantsFor(testWallTime).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(testWallTime).map((i) => i.epochNanoseconds),
'possible instants for wall time'
);
const before1 = shiftWallTime.subtract({ hours: 1 });
const before1 = testWallTime.subtract(testWallDuration);
assert.compareArray(
tz.getPossibleInstantsFor(before1).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(before1).map((i) => i.epochNanoseconds),
'possible instants for 1 hour before wall time'
'possible instants before wall time'
);
const after1 = shiftWallTime.add({ hours: 1 });
const after1 = testWallTime.add(testWallDuration);
assert.compareArray(
tz.getPossibleInstantsFor(after1).map((i) => i.epochNanoseconds),
realTz.getPossibleInstantsFor(after1).map((i) => i.epochNanoseconds),
'possible instants for 1 hour after wall time'
'possible instants after wall time'
);
}
// Check a positive DST shift from +00:00 to +01:00
checkTimeZoneArithmetic(
new Temporal.Instant(1616893200000000000n),
3600e9,
new Temporal.PlainDateTime(2021, 3, 28, 1),
{ hours: 1 },
TemporalHelpers.oneShiftTimeZone(new Temporal.Instant(1616893200000000000n), 3600e9),
'Europe/London',
new Temporal.PlainDateTime(2021, 3, 28, 1)
);
// Check a negative DST shift from +00:00 to -01:00
checkTimeZoneArithmetic(
new Temporal.Instant(1635642000000000000n),
-3600e9,
new Temporal.PlainDateTime(2021, 10, 31, 1),
{ hours: 1 },
TemporalHelpers.oneShiftTimeZone(new Temporal.Instant(1635642000000000000n), -3600e9),
'Atlantic/Azores',
new Temporal.PlainDateTime(2021, 10, 31, 1)
);
// Check the no-shift case
checkTimeZoneArithmetic(
new Temporal.Instant(0n),
0,
new Temporal.PlainDateTime(1970, 1, 1),
{ hours: 1 },
TemporalHelpers.oneShiftTimeZone(new Temporal.Instant(0n), 0),
'UTC',
new Temporal.PlainDateTime(1970, 1, 1)
);
// Check the cross-date-line sample time zone
checkTimeZoneArithmetic(
Temporal.Instant.from('2011-12-30T10:00:00Z'),
Temporal.PlainDateTime.from("2011-12-30T12:00"),
{ days: 1 },
TemporalHelpers.crossDateLineTimeZone(),
'Pacific/Apia',
);