Skip to content

Commit

Permalink
Validate required methods of Temporal TimeZone protocol
Browse files Browse the repository at this point in the history
Checking whether an object implements the TimeZone protocol is now done by
means of HasProperty operations for each of the required methods unless
the object already has the TimeZone brand.

Discussion:
tc39/proposal-temporal#2104 (comment)

Corresponding normative PR:
tc39/proposal-temporal#2485
  • Loading branch information
ptomato committed Apr 7, 2023
1 parent be84a7a commit 238483e
Show file tree
Hide file tree
Showing 68 changed files with 175 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ const expectedOpsForZonedRelativeTo = expected.concat([
"call options.relativeTo.year.valueOf",
"get options.relativeTo.calendar.dateFromFields",
"call options.relativeTo.calendar.dateFromFields",
"has options.relativeTo.timeZone.getOffsetNanosecondsFor",
"has options.relativeTo.timeZone.getPossibleInstantsFor",
"has options.relativeTo.timeZone.id",
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
"get options.relativeTo.timeZone.getOffsetNanosecondsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -25,6 +24,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ const expectedOpsForZonedRelativeTo = expected.concat([
// InterpretTemporalDateTimeFields
"get options.relativeTo.calendar.dateFromFields",
"call options.relativeTo.calendar.dateFromFields",
// ToRelativeTemporalObject again
"has options.relativeTo.timeZone.getOffsetNanosecondsFor",
"has options.relativeTo.timeZone.getPossibleInstantsFor",
"has options.relativeTo.timeZone.id",
// InterpretISODateTimeOffset
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ const expectedOpsForZonedRelativeTo = [
"call options.relativeTo.year.valueOf",
"get options.relativeTo.calendar.dateFromFields",
"call options.relativeTo.calendar.dateFromFields",
"has options.relativeTo.timeZone.getOffsetNanosecondsFor",
"has options.relativeTo.timeZone.getPossibleInstantsFor",
"has options.relativeTo.timeZone.id",
// InterpretISODateTimeOffset
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ const expectedOpsForZonedRelativeTo = expected.concat([
// InterpretTemporalDateTimeFields
"get options.relativeTo.calendar.dateFromFields",
"call options.relativeTo.calendar.dateFromFields",
// ToRelativeTemporalObject again
"has options.relativeTo.timeZone.getOffsetNanosecondsFor",
"has options.relativeTo.timeZone.getPossibleInstantsFor",
"has options.relativeTo.timeZone.id",
// InterpretISODateTimeOffset
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ const expectedOpsForZonedRelativeTo = [
"call options.relativeTo.year.valueOf",
"get options.relativeTo.calendar.dateFromFields",
"call options.relativeTo.calendar.dateFromFields",
"has options.relativeTo.timeZone.getOffsetNanosecondsFor",
"has options.relativeTo.timeZone.getPossibleInstantsFor",
"has options.relativeTo.timeZone.id",
// InterpretISODateTimeOffset
"get options.relativeTo.timeZone.getPossibleInstantsFor",
"call options.relativeTo.timeZone.getPossibleInstantsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const expected = [
"get options.smallestUnit.toString",
"call options.smallestUnit.toString",
"get options.timeZone",
"has options.timeZone.getOffsetNanosecondsFor",
"has options.timeZone.getPossibleInstantsFor",
"has options.timeZone.id",
"get options.timeZone.getOffsetNanosecondsFor",
"call options.timeZone.getOffsetNanosecondsFor",
"get options.timeZone.getOffsetNanosecondsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]

const actual = [];
const expected = [
"has timeZone.getOffsetNanosecondsFor",
"has timeZone.getPossibleInstantsFor",
"has timeZone.id",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
"get timeZone.getOffsetNanosecondsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ features: [Temporal]
---*/

const actual = [];
const expected = [
"has timeZone.getOffsetNanosecondsFor",
"has timeZone.getPossibleInstantsFor",
"has timeZone.id",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const calendar = Temporal.Calendar.from("iso8601");
Expand All @@ -19,4 +24,4 @@ const timeZone = TemporalHelpers.timeZoneObserver(actual, "timeZone", {
const result = instant.toZonedDateTime({ timeZone, calendar });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);

assert.compareArray(actual, []);
assert.compareArray(actual, expected);
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -27,6 +26,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
3 changes: 2 additions & 1 deletion test/built-ins/Temporal/Now/plainDate/timezone-wrong-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -25,6 +24,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
3 changes: 3 additions & 0 deletions test/built-ins/Temporal/Now/plainDate/toPlainDate-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]

const actual = [];
const expected = [
"has timeZone.getOffsetNanosecondsFor",
"has timeZone.getPossibleInstantsFor",
"has timeZone.id",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -25,6 +24,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ features: [BigInt, Proxy, Temporal]
const actual = [];

const expected = [
'has timeZone.getOffsetNanosecondsFor',
'has timeZone.getPossibleInstantsFor',
'has timeZone.id',
'get timeZone.getOffsetNanosecondsFor',
'call timeZone.getOffsetNanosecondsFor'
];
Expand Down
2 changes: 2 additions & 0 deletions test/built-ins/Temporal/Now/plainDateTime/return-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ features: [BigInt, Temporal]
const calendar = Temporal.Calendar.from('iso8601');

const timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor(instant) {
return -Number(instant.epochNanoseconds % 86400000000000n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ features: [Temporal]

var calls = [];
var timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor: function() {
calls.push({
args: arguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ for (const dateTime of invalidValues) {
let callCount = 0;

const timeZone = {
getOffsetNanosecondsFor(instant, calendar) {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor() {
callCount += 1;
return dateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ features: [Temporal]
---*/

var timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
get getOffsetNanosecondsFor() {
throw new Test262Error();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ features: [Temporal]
---*/

var timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor() {
throw new Test262Error();
}
Expand Down
3 changes: 3 additions & 0 deletions test/built-ins/Temporal/Now/plainDateTime/timezone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ features: [BigInt, Proxy, Temporal]
const actual = [];

const expected = [
'has timeZone.getOffsetNanosecondsFor',
'has timeZone.getPossibleInstantsFor',
'has timeZone.id',
'get timeZone.getOffsetNanosecondsFor',
'call timeZone.getOffsetNanosecondsFor'
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const rangeErrorTests = [
[1, "number that doesn't convert to a valid ISO string"],
[19761118, "number that would convert to a valid ISO string in other contexts"],
[1n, "bigint"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of rangeErrorTests) {
Expand All @@ -25,6 +24,8 @@ for (const [timeZone, description] of rangeErrorTests) {

const typeErrorTests = [
[Symbol(), "symbol"],
[{}, "object not implementing time zone protocol"],
[new Temporal.Calendar("iso8601"), "calendar instance"],
];

for (const [timeZone, description] of typeErrorTests) {
Expand Down
3 changes: 3 additions & 0 deletions test/built-ins/Temporal/Now/plainDateTime/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ features: [Temporal]

const actual = [];
const expected = [
"has timeZone.getOffsetNanosecondsFor",
"has timeZone.getPossibleInstantsFor",
"has timeZone.id",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
2 changes: 2 additions & 0 deletions test/built-ins/Temporal/Now/plainDateTimeISO/return-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ description: Return value describes the start of a day
features: [BigInt, Temporal]
---*/
const timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor(instant) {
return -Number(instant.epochNanoseconds % 86400000000000n);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ features: [Temporal]

var calls = [];
var timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor: function() {
calls.push({
args: arguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ for (const dateTime of invalidValues) {
let callCount = 0;

const timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
getOffsetNanosecondsFor(instant, calendar) {
callCount += 1;
return dateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ features: [Temporal]
---*/

var timeZone = {
id: 'Etc/Test',
getPossibleInstantsFor() { return []; },
get getOffsetNanosecondsFor() {
throw new Test262Error();
}
Expand Down
Loading

0 comments on commit 238483e

Please sign in to comment.