Skip to content

Commit

Permalink
Temporal: Use TemporalHelpers.springForwardFallBackTimeZone in staging
Browse files Browse the repository at this point in the history
Some tests in staging used various IANA time zones in order to test DST
behaviour. Since implementations are technically not required to
understand IANA time zones, we have a fake DST time zone in
TemporalHelpers. Use that instead.

For a few cases where it's not practical to use the fake DST time zone
(because it only has one spring-forward and one fall-back transition, for
example), move a few tests into staging/Intl402/Temporal/.

See: #3649
  • Loading branch information
ptomato authored and Ms2ger committed Oct 3, 2022
1 parent 42074e7 commit 886f091
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 333 deletions.
73 changes: 73 additions & 0 deletions test/staging/Intl402/Temporal/old/duration-arithmetic-dst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-duration-objects
description: >
Various DST arithmetic tests that it's impractical to do without a time zone
database in the implementation
features: [Temporal]
---*/

// Tests for arithmetic that start inside a repeated hour, and end in a skipped
// hour. We have TemporalHelpers.springForwardFallBackTimeZone which is
// sufficient to test this for Temporal.Duration.prototype.add, and
// Temporal.Duration.prototype.round, but it's impractical to replicate all the
// TZDB data for testing it with other methods such as subtract() where we need
// to calculate to the _next_ transition

var skippedHourDay = Temporal.ZonedDateTime.from("2019-03-10T00:00[America/Vancouver]");
var repeatedHourDay = Temporal.ZonedDateTime.from("2019-11-03T00:00[America/Vancouver]");
var inRepeatedHour = Temporal.ZonedDateTime.from("2019-11-03T01:00-07:00[America/Vancouver]");

// subtract()

var oneDay = new Temporal.Duration(0, 0, 0, 1);
assert.sameValue(`${ Temporal.Duration.from({
days: 127,
hours: 1
}).subtract(oneDay, { relativeTo: inRepeatedHour }) }`, "P126DT1H");
var hours24 = new Temporal.Duration(0, 0, 0, 0, 24);
assert.sameValue(`${ Temporal.Duration.from({
days: 127,
hours: 1
}).subtract(hours24, { relativeTo: inRepeatedHour }) }`, "P126D");

// total()
var totalDays = Temporal.Duration.from({
days: 126,
hours: 1
}).total({
unit: "days",
relativeTo: inRepeatedHour
});
assert(Math.abs(totalDays - (126 + 1 / 23)) < Number.EPSILON);
assert.sameValue(Temporal.Duration.from({
days: 126,
hours: 1
}).total({
unit: "hours",
relativeTo: inRepeatedHour
}), 3026);

// Tests for casting relativeTo to ZonedDateTime when possible:
// Without a TZDB, it's not possible to get a ZonedDateTime with DST from a
// string.

assert.sameValue(
`${ oneDay.add(hours24, { relativeTo: "2019-11-02T00:00[America/Vancouver]" }) }`,
"P1DT24H"
);
var hours25 = new Temporal.Duration(0, 0, 0, 0, 25);
assert.sameValue(`${ hours25.round({
largestUnit: "days",
relativeTo: "2019-11-03T00:00[America/Vancouver]"
}) }`, "P1D");
assert.sameValue(
`${ oneDay.subtract(hours24, { relativeTo: "2019-11-03T00:00[America/Vancouver]" }) }`,
"PT1H"
);
assert.sameValue(oneDay.total({
unit: "hours",
relativeTo: "2019-11-03T00:00[America/Vancouver]"
}), 25);
98 changes: 98 additions & 0 deletions test/staging/Intl402/Temporal/old/tzdb-string-parsing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-zoneddatetime-objects
description: String parsing cases that require a TZDB in the implementation
features: [Temporal]
---*/

// parses with an IANA zone but no offset (with disambiguation)
var zdt = Temporal.ZonedDateTime.from("2020-03-08T02:30[America/Los_Angeles]", { disambiguation: "earlier" });
assert.sameValue(zdt.toString(), "2020-03-08T01:30:00-08:00[America/Los_Angeles]");

// "Z" means preserve the exact time in the given IANA time zone
var zdt = Temporal.ZonedDateTime.from("2020-03-08T09:00:00Z[America/Los_Angeles]");
assert.sameValue(zdt.toString(), "2020-03-08T01:00:00-08:00[America/Los_Angeles]");

// Offset options

// { offset: 'prefer' } if offset matches time zone (first 1:30 when DST ends)
var zdt = Temporal.ZonedDateTime.from("2020-11-01T01:30-07:00[America/Los_Angeles]", { offset: "prefer" });
assert.sameValue(zdt.toString(), "2020-11-01T01:30:00-07:00[America/Los_Angeles]");

// { offset: 'prefer' } if offset matches time zone (second 1:30 when DST ends)
var zdt = Temporal.ZonedDateTime.from("2020-11-01T01:30-08:00[America/Los_Angeles]", { offset: "prefer" });
assert.sameValue(zdt.toString(), "2020-11-01T01:30:00-08:00[America/Los_Angeles]");

// { offset: 'prefer' } if offset does not match time zone
var zdt = Temporal.ZonedDateTime.from("2020-11-01T04:00-07:00[America/Los_Angeles]", { offset: "prefer" });
assert.sameValue(zdt.toString(), "2020-11-01T04:00:00-08:00[America/Los_Angeles]");

// { offset: 'ignore' } uses time zone only
var zdt = Temporal.ZonedDateTime.from("2020-11-01T04:00-12:00[America/Los_Angeles]", { offset: "ignore" });
assert.sameValue(zdt.toString(), "2020-11-01T04:00:00-08:00[America/Los_Angeles]");

// { offset: 'use' } uses offset only
var zdt = Temporal.ZonedDateTime.from("2020-11-01T04:00-07:00[America/Los_Angeles]", { offset: "use" });
assert.sameValue(zdt.toString(), "2020-11-01T03:00:00-08:00[America/Los_Angeles]");

// Disambiguation options

// plain datetime with multiple instants - Fall DST in Brazil
var str = "2019-02-16T23:45[America/Sao_Paulo]";
assert.sameValue(`${ Temporal.ZonedDateTime.from(str) }`, "2019-02-16T23:45:00-02:00[America/Sao_Paulo]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "compatible" }) }`, "2019-02-16T23:45:00-02:00[America/Sao_Paulo]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "earlier" }) }`, "2019-02-16T23:45:00-02:00[America/Sao_Paulo]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "later" }) }`, "2019-02-16T23:45:00-03:00[America/Sao_Paulo]");
assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str, { disambiguation: "reject" }));

// plain datetime with multiple instants - Spring DST in Los Angeles
var str = "2020-03-08T02:30[America/Los_Angeles]";
assert.sameValue(`${ Temporal.ZonedDateTime.from(str) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "compatible" }) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "earlier" }) }`, "2020-03-08T01:30:00-08:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { disambiguation: "later" }) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str, { disambiguation: "reject" }));

// uses disambiguation if offset is ignored
var str = "2020-03-08T02:30[America/Los_Angeles]";
var offset = "ignore";
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { offset }) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "compatible"
}) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "earlier"
}) }`, "2020-03-08T01:30:00-08:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "later"
}) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "reject"
}));

// uses disambiguation if offset is wrong and option is prefer
var str = "2020-03-08T02:30-23:59[America/Los_Angeles]";
var offset = "prefer";
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, { offset }) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "compatible"
}) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "earlier"
}) }`, "2020-03-08T01:30:00-08:00[America/Los_Angeles]");
assert.sameValue(`${ Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "later"
}) }`, "2020-03-08T03:30:00-07:00[America/Los_Angeles]");
assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str, {
offset,
disambiguation: "reject"
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2018 Bloomberg LP. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-zoneddatetime-objects
description: Corner cases of time zone offset shifts
features: [Temporal]
---*/

// hoursInDay works with non-hour DST change
var zdt1 = Temporal.ZonedDateTime.from("2020-10-04T12:00[Australia/Lord_Howe]");
assert.sameValue(zdt1.hoursInDay, 23.5);
var zdt2 = Temporal.ZonedDateTime.from("2020-04-05T12:00[Australia/Lord_Howe]");
assert.sameValue(zdt2.hoursInDay, 24.5);

// hoursInDay works with non-half-hour DST change
var zdt = Temporal.ZonedDateTime.from("1933-01-01T12:00[Asia/Singapore]");
assert(Math.abs(zdt.hoursInDay - 23.666666666666668) < Number.EPSILON);

// hoursInDay works when day starts at 1:00 due to DST start at midnight
var zdt = Temporal.ZonedDateTime.from("2015-10-18T12:00:00-02:00[America/Sao_Paulo]");
assert.sameValue(zdt.hoursInDay, 23);

// startOfDay works when day starts at 1:00 due to DST start at midnight
var zdt = Temporal.ZonedDateTime.from("2015-10-18T12:00:00-02:00[America/Sao_Paulo]");
assert.sameValue(`${ zdt.startOfDay().toPlainTime() }`, "01:00:00");
57 changes: 29 additions & 28 deletions test/staging/Temporal/Duration/old/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/*---
esid: sec-temporal-duration-objects
description: Temporal.Duration.prototype.add() works as expected
includes: [temporalHelpers.js]
features: [Temporal]
---*/

Expand All @@ -17,24 +18,25 @@ assert.sameValue(`${ oneDay.add(hours24, { relativeTo }) }`, "P2D");
// relativeTo does not affect days if ZonedDateTime, and duration encompasses no DST change
var relativeTo = Temporal.ZonedDateTime.from("2017-01-01T00:00[+04:30]");
assert.sameValue(`${ oneDay.add(hours24, { relativeTo }) }`, "P2D");
var skippedHourDay = Temporal.ZonedDateTime.from("2019-03-10T00:00[America/Vancouver]");
var repeatedHourDay = Temporal.ZonedDateTime.from("2019-11-03T00:00[America/Vancouver]");
var inRepeatedHour = Temporal.ZonedDateTime.from("2019-11-03T01:00-07:00[America/Vancouver]");

// relativeTo affects days if ZonedDateTime, and duration encompasses DST change
var timeZone = TemporalHelpers.springForwardFallBackTimeZone();
var skippedHourDay = Temporal.PlainDateTime.from("2000-04-02").toZonedDateTime(timeZone);
var repeatedHourDay = Temporal.PlainDateTime.from("2000-10-29").toZonedDateTime(timeZone);
var inRepeatedHour = new Temporal.ZonedDateTime(972806400_000_000_000n, timeZone);
var hours12 = new Temporal.Duration(0, 0, 0, 0, 12);
var hours25 = new Temporal.Duration(0, 0, 0, 0, 25);

// relativeTo affects days if ZonedDateTime, and duration encompasses DST change",

// start inside repeated hour, end after",
// start inside repeated hour, end after
assert.sameValue(`${ hours25.add(oneDay, { relativeTo: inRepeatedHour }) }`, "P2D");
assert.sameValue(`${ oneDay.add(hours25, { relativeTo: inRepeatedHour }) }`, "P2DT1H");

// start after repeated hour, end inside (negative)"
var relativeTo = Temporal.ZonedDateTime.from("2019-11-05T01:00[America/Vancouver]");
// start after repeated hour, end inside (negative)
var relativeTo = Temporal.PlainDateTime.from("2000-10-31T01:00").toZonedDateTime(timeZone);
assert.sameValue(`${ hours25.negated().add(oneDay.negated(), { relativeTo }) }`, "-P2DT1H");
assert.sameValue(`${ oneDay.negated().add(hours25.negated(), { relativeTo }) }`, "-P2D");

// start inside repeated hour, end in skipped hour",
// start inside repeated hour, end in skipped hour
assert.sameValue(`${ hours25.add(Temporal.Duration.from({
days: 125,
hours: 1
Expand All @@ -44,44 +46,44 @@ assert.sameValue(`${ oneDay.add(Temporal.Duration.from({
hours: 1
}), { relativeTo: inRepeatedHour }) }`, "P126DT1H");

// start in normal hour, end in skipped hour",
var relativeTo = Temporal.ZonedDateTime.from("2019-03-08T02:30[America/Vancouver]");
// start in normal hour, end in skipped hour
var relativeTo = Temporal.PlainDateTime.from("2000-03-31T02:30").toZonedDateTime(timeZone);
assert.sameValue(`${ oneDay.add(hours25, { relativeTo }) }`, "P2DT1H");
assert.sameValue(`${ hours25.add(oneDay, { relativeTo }) }`, "P2D");

// start before skipped hour, end >1 day after",
// start before skipped hour, end >1 day after
assert.sameValue(`${ hours25.add(oneDay, { relativeTo: skippedHourDay }) }`, "P2DT2H");
assert.sameValue(`${ oneDay.add(hours25, { relativeTo: skippedHourDay }) }`, "P2DT1H");

// start after skipped hour, end >1 day before (negative)",
var relativeTo = Temporal.ZonedDateTime.from("2019-03-11T00:00[America/Vancouver]");
// start after skipped hour, end >1 day before (negative)
var relativeTo = Temporal.PlainDateTime.from("2000-04-03T00:00").toZonedDateTime(timeZone);
assert.sameValue(`${ hours25.negated().add(oneDay.negated(), { relativeTo }) }`, "-P2DT2H");
assert.sameValue(`${ oneDay.negated().add(hours25.negated(), { relativeTo }) }`, "-P2DT1H");

// start before skipped hour, end <1 day after",
// start before skipped hour, end <1 day after
assert.sameValue(`${ hours12.add(oneDay, { relativeTo: skippedHourDay }) }`, "P1DT13H");
assert.sameValue(`${ oneDay.add(hours12, { relativeTo: skippedHourDay }) }`, "P1DT12H");

// start after skipped hour, end <1 day before (negative)",
var relativeTo = Temporal.ZonedDateTime.from("2019-03-10T12:00[America/Vancouver]");
// start after skipped hour, end <1 day before (negative)
var relativeTo = Temporal.PlainDateTime.from("2000-04-02T12:00").toZonedDateTime(timeZone);
assert.sameValue(`${ hours12.negated().add(oneDay.negated(), { relativeTo }) }`, "-P1DT13H");
assert.sameValue(`${ oneDay.negated().add(hours12.negated(), { relativeTo }) }`, "-P1DT12H");

// start before repeated hour, end >1 day after",
// start before repeated hour, end >1 day after
assert.sameValue(`${ hours25.add(oneDay, { relativeTo: repeatedHourDay }) }`, "P2D");
assert.sameValue(`${ oneDay.add(hours25, { relativeTo: repeatedHourDay }) }`, "P2DT1H");

// start after repeated hour, end >1 day before (negative)",
var relativeTo = Temporal.ZonedDateTime.from("2019-11-04T00:00[America/Vancouver]");
// start after repeated hour, end >1 day before (negative)
var relativeTo = Temporal.PlainDateTime.from("2000-10-30T00:00").toZonedDateTime(timeZone);
assert.sameValue(`${ hours25.negated().add(oneDay.negated(), { relativeTo }) }`, "-P2D");
assert.sameValue(`${ oneDay.negated().add(hours25.negated(), { relativeTo }) }`, "-P2DT1H");

// start before repeated hour, end <1 day after",
// start before repeated hour, end <1 day after
assert.sameValue(`${ hours12.add(oneDay, { relativeTo: repeatedHourDay }) }`, "P1DT11H");
assert.sameValue(`${ oneDay.add(hours12, { relativeTo: repeatedHourDay }) }`, "P1DT12H");

// start after repeated hour, end <1 day before (negative)",
var relativeTo = Temporal.ZonedDateTime.from("2019-11-03T12:00[America/Vancouver]");
// start after repeated hour, end <1 day before (negative)
var relativeTo = Temporal.PlainDateTime.from("2000-10-29T12:00").toZonedDateTime(timeZone);
assert.sameValue(`${ hours12.negated().add(oneDay.negated(), { relativeTo }) }`, "-P1DT11H");
assert.sameValue(`${ oneDay.negated().add(hours12.negated(), { relativeTo }) }`, "-P1DT12H");

Expand All @@ -91,13 +93,12 @@ assert.sameValue(`${ hours25.add(oneDay, { relativeTo }) }`, "P3DT1H");
assert.sameValue(`${ oneDay.add(hours25, { relativeTo }) }`, "P3DT1H");

// casts relativeTo to ZonedDateTime if possible
assert.sameValue(`${ oneDay.add(hours24, { relativeTo: "2019-11-02T00:00[America/Vancouver]" }) }`, "P1DT24H");
assert.sameValue(`${ oneDay.add(hours24, {
relativeTo: {
year: 2019,
month: 11,
day: 2,
timeZone: "America/Vancouver"
year: 2000,
month: 10,
day: 28,
timeZone
}
}) }`, "P1DT24H");

Expand Down
Loading

0 comments on commit 886f091

Please sign in to comment.