Skip to content

Commit

Permalink
Temporal: Port Demitasse tests for PlainDateTime toZonedDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama authored and Ms2ger committed Apr 25, 2022
1 parent 45fa006 commit cba0235
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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.plaindatetime.prototype.tozoneddatetime
description: Straightforward case of using UTC
features: [Temporal]
---*/

const dt = new Temporal.PlainDateTime(2020, 1, 1, 0, 0);
const zdt = dt.toZonedDateTime("UTC");

assert.sameValue(zdt.epochNanoseconds, 1577836800000000000n, "nanoseconds");
assert.sameValue(zdt.calendar.toString(), "iso8601", "calendar");
assert.sameValue(zdt.timeZone, "UTC", "timezone");
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ features: [Temporal]

const datetime = new Temporal.PlainDateTime(2001, 9, 9, 1, 46, 40, 987, 654, 321);
const timeZone = new Temporal.TimeZone("UTC");
assert.throws(RangeError, () => datetime.toZonedDateTime(timeZone, { disambiguation: "other string" }));
const invalidStrings = ["obviously bad", "", "EARLIER", "earlıer", "late\u0131r", "reject\0"];
invalidStrings.forEach((s) => {
assert.throws(
RangeError,
() => datetime.toZonedDateTime(timeZone, { disambiguation: s }),
`invalid disambiguation string (${s})`);
});

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.plaindatetime.prototype.tozoneddatetime
description: Convert to zoned datetime outside valid range
features: [Temporal]
---*/

const max = new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999);
const min = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1);

assert.throws(
RangeError,
() => max.toZonedDateTime("UTC"),
"outside of Instant range (too big)"
);

assert.throws(
RangeError,
() => min.toZonedDateTime("UTC"),
"outside of Instant range (too small)"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.plaindatetime.prototype.tozoneddatetime
description: Checking disambiguation options for daylight savings time changes
features: [Temporal]
includes: [temporalHelpers.js]
---*/

const tz = TemporalHelpers.springForwardFallBackTimeZone();

const dt1 = new Temporal.PlainDateTime(2000, 4, 2, 2);

const zdt1 = dt1.toZonedDateTime(tz);
const zdt1_compatible = dt1.toZonedDateTime(tz, { disambiguation: "compatible" });
const zdt1_earlier = dt1.toZonedDateTime(tz, { disambiguation: "earlier" });
const zdt1_later = dt1.toZonedDateTime(tz, { disambiguation: "later" });

assert.sameValue(zdt1.epochNanoseconds, 954669600000000000n, "Fall DST (no disambiguation)");
assert.sameValue(zdt1_compatible.epochNanoseconds, 954669600000000000n, "Fall DST (disambiguation = compatible)");
assert.sameValue(zdt1_earlier.epochNanoseconds, 954666000000000000n, "Fall DST (disambiguation = earlier)");
assert.sameValue(zdt1_later.epochNanoseconds, 954669600000000000n, "Fall DST (disambiguation = later)");

assert.throws(
RangeError,
() => dt1.toZonedDateTime(tz, { disambiguation: "reject" }),
"Fall DST (disambiguation = reject)"
);

const dt2 = new Temporal.PlainDateTime(2000, 10, 29, 1);

const zdt2 = dt2.toZonedDateTime(tz);
const zdt2_compatible = dt2.toZonedDateTime(tz, { disambiguation: "compatible" });
const zdt2_earlier = dt2.toZonedDateTime(tz, { disambiguation: "earlier" });
const zdt2_later = dt2.toZonedDateTime(tz, { disambiguation: "later" });

assert.sameValue(zdt2.epochNanoseconds, 972806400000000000n, "Spring DST (no disambiguation)");
assert.sameValue(zdt2_compatible.epochNanoseconds, 972806400000000000n, "Spring DST (disambiguation = compatible)");
assert.sameValue(zdt2_earlier.epochNanoseconds, 972806400000000000n, "Spring DST (disambiguation = earlier)");
assert.sameValue(zdt2_later.epochNanoseconds, 972810000000000000n, "Spring DST (disambiguation = later)");

assert.throws(
RangeError,
() => dt2.toZonedDateTime(tz, { disambiguation: "reject" }),
"Spring DST (disambiguation = reject)"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.plaindate.prototype.tozoneddatetime
description: Empty object may be used as options
features: [Temporal]
---*/

const dt = new Temporal.PlainDateTime(2019, 10, 29, 10, 46, 38, 271, 986, 102);

assert.sameValue(
dt.toZonedDateTime("UTC", {}).epochNanoseconds,
1572345998271986102n,
"options may be an empty plain object"
);

assert.sameValue(
dt.toZonedDateTime("UTC", () => {}).epochNanoseconds,
1572345998271986102n,
"options may be a function object"
);

0 comments on commit cba0235

Please sign in to comment.