Skip to content

Commit

Permalink
Test rejecting ISO strings with subminute offsets
Browse files Browse the repository at this point in the history
This commit verifies that ISO strings with sub-minute offsets cannot
be parsed into time zone identifiers. This was a change introduced in
the recently-merged tc39/proposal-temporal#2607, but tests for this case
were missing from tc39#3862 (the tests for that PR).

I noticed in codecov results on an unrelated PR that this case wasn't
being tested, so fixing that mistake now.
  • Loading branch information
justingrant committed Jul 21, 2023
1 parent 6f146e6 commit 3b3bce5
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Duration.compare(new Temporal.Duration(), new Temporal.Duration(), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Duration.compare(new Temporal.Duration(), new Temporal.Duration(), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Duration(1);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.add(new Temporal.Duration(1), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.add(new Temporal.Duration(1), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Duration(1);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.round({ largestUnit: "months", relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.round({ largestUnit: "months", relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Duration(1);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.subtract(new Temporal.Duration(1), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.subtract(new Temporal.Duration(1), { relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Duration(1);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.total({ unit: "months", relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.total({ unit: "months", relativeTo: { year: 2000, month: 5, day: 2, timeZone } }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Instant(0n);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toString({ timeZone }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toString({ timeZone }), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toString({ timeZone });
assert.sameValue(result1.substr(-6), "+00:00", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Instant(0n);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toZonedDateTime({ timeZone, calendar: "iso8601" }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toZonedDateTime({ timeZone, calendar: "iso8601" }), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toZonedDateTime({ timeZone, calendar: "iso8601" });
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.Instant(0n);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toZonedDateTimeISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toZonedDateTimeISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toZonedDateTimeISO(timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainDate("iso8601", timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainDate("iso8601", timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainDateISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainDateISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainDateTime("iso8601", timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainDateTime("iso8601", timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainDateTimeISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainDateTimeISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainTimeISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainTimeISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.zonedDateTime("iso8601", timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.zonedDateTime("iso8601", timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = Temporal.Now.zonedDateTime("iso8601", timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.zonedDateTimeISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.zonedDateTimeISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = Temporal.Now.zonedDateTimeISO(timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.PlainDate(2000, 5, 2);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toZonedDateTime(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toZonedDateTime(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toZonedDateTime(timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.PlainDateTime(2000, 5, 2);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toZonedDateTime(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toZonedDateTime(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toZonedDateTime(timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.PlainTime();
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.toZonedDateTime({ plainDate: new Temporal.PlainDate(2000, 5, 2), timeZone }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.toZonedDateTime({ plainDate: new Temporal.PlainDate(2000, 5, 2), timeZone }), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.toZonedDateTime({ plainDate: new Temporal.PlainDate(2000, 5, 2), timeZone });
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.TimeZone.from(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.TimeZone.from(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = Temporal.TimeZone.from(timeZone);
assert.sameValue(result1.id, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2023 Justin Grant. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.timezone.equals
description: Conversion of ISO date-time strings to the argument of Temporal.TimeZone.prototype.equals
features: [Temporal]
---*/

let tzUTC = Temporal.TimeZone.from("UTC");
let arg = "2021-08-19T17:30";
assert.throws(RangeError, () => tzUTC.equals(arg), "bare date-time string is not a time zone");

arg = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => tzUTC.equals(arg), "ISO string sub-minute offset is not OK as time zone");

arg = "2021-08-19T17:30Z";
tzUTC = Temporal.TimeZone.from(arg);
assert.sameValue(tzUTC.equals(arg), true, "date-time + Z is UTC time zone");

arg = "2021-08-19T17:30-07:00";
tzUTC = Temporal.TimeZone.from(arg);
assert.sameValue(tzUTC.equals(arg), true, "date-time + offset is the offset time zone");

arg = "2021-08-19T17:30[UTC]";
tzUTC = Temporal.TimeZone.from(arg);
assert.sameValue(tzUTC.equals(arg), true, "date-time + IANA annotation is the IANA time zone");

arg = "2021-08-19T17:30Z[UTC]";
tzUTC = Temporal.TimeZone.from(arg);
assert.sameValue(tzUTC.equals(arg), true, "date-time + Z + IANA annotation is the IANA time zone");

arg = "2021-08-19T17:30-07:00[UTC]";
tzUTC = Temporal.TimeZone.from(arg);
assert.sameValue(tzUTC.equals(arg), true, "date-time + offset + IANA annotation is the IANA time zone");
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.ZonedDateTime.compare({ year: 2000, month: 5, day: 2, timeZone }, instance), "bare date-time string is not a time zone (arg 1)");
assert.throws(RangeError, () => Temporal.ZonedDateTime.compare(instance, { year: 2000, month: 5, day: 2, timeZone }), "bare date-time string is not a time zone (arg 2)");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.ZonedDateTime.compare({ year: 2000, month: 5, day: 2, timeZone }, instance), "ISO string sub-minute offset is not OK as time zone (arg 1)");
assert.throws(RangeError, () => Temporal.ZonedDateTime.compare(instance, { year: 2000, month: 5, day: 2, timeZone }), "ISO string sub-minute offset is not OK as time zone (arg 2)");

// The following are all valid strings so should not throw:

[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.ZonedDateTime.from({ year: 2000, month: 5, day: 2, timeZone }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.ZonedDateTime.from({ year: 2000, month: 5, day: 2, timeZone }), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = Temporal.ZonedDateTime.from({ year: 2000, month: 5, day: 2, timeZone });
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const instance1 = new Temporal.ZonedDateTime(0n, expectedTimeZone);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance1.since({ year: 2020, month: 5, day: 2, timeZone }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance1.since({ year: 2020, month: 5, day: 2, timeZone }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw. They should produce
// expectedTimeZone, so additionally the operation will not throw due to the
// time zones being different on the receiver and the argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const instance1 = new Temporal.ZonedDateTime(0n, expectedTimeZone);
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance1.until({ year: 2020, month: 5, day: 2, timeZone }), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance1.until({ year: 2020, month: 5, day: 2, timeZone }), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw. They should produce
// expectedTimeZone, so additionally the operation will not throw due to the
// time zones being different on the receiver and the argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const instance = new Temporal.ZonedDateTime(0n, "UTC");
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => instance.withTimeZone(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => instance.withTimeZone(timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = instance.withTimeZone(timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => new Temporal.ZonedDateTime(0n, timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => new Temporal.ZonedDateTime(0n, timeZone), "ISO string sub-minute offset is not OK as time zone");

timeZone = "2021-08-19T17:30Z";
const result1 = new Temporal.ZonedDateTime(0n, timeZone);
assert.sameValue(result1.timeZoneId, "UTC", "date-time + Z is UTC time zone");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ features: [Temporal]
let timeZone = "2021-08-19T17:30";
assert.throws(RangeError, () => Temporal.Now.plainDateTimeISO(timeZone), "bare date-time string is not a time zone");

timeZone = "2021-08-19T17:30-07:00:01";
assert.throws(RangeError, () => Temporal.Now.plainDateTimeISO(timeZone), "ISO string sub-minute offset is not OK as time zone");

// The following are all valid strings so should not throw:

[
Expand Down

0 comments on commit 3b3bce5

Please sign in to comment.