1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

LibJS: Disallow one day long time zone offsets

This is a normative change in the Temporal spec.
See: 9cc8b29b
This commit is contained in:
davidot 2022-10-13 23:30:51 +02:00 committed by Linus Groh
parent 1167fdb17a
commit 1b0ca52c54
7 changed files with 83 additions and 10 deletions

View file

@ -246,7 +246,8 @@
M(TemporalInvalidMonthCode, "Invalid month code") \ M(TemporalInvalidMonthCode, "Invalid month code") \
M(TemporalInvalidMonthDayString, "Invalid month day string '{}'") \ M(TemporalInvalidMonthDayString, "Invalid month day string '{}'") \
M(TemporalInvalidMonthDayStringUTCDesignator, "Invalid month day string '{}': must not contain a UTC designator") \ M(TemporalInvalidMonthDayStringUTCDesignator, "Invalid month day string '{}': must not contain a UTC designator") \
M(TemporalInvalidOffsetNanosecondsValue, "Invalid offset nanoseconds value, must be in range -86400 * 10^9 to 86400 * 10^9") \ M(TemporalInvalidOffsetNanosecondsValue, "Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to " \
"86400 * 10^9 - 1") \
M(TemporalInvalidPlainDate, "Invalid plain date") \ M(TemporalInvalidPlainDate, "Invalid plain date") \
M(TemporalInvalidPlainDateTime, "Invalid plain date time") \ M(TemporalInvalidPlainDateTime, "Invalid plain date time") \
M(TemporalInvalidPlainMonthDay, "Invalid plain month day") \ M(TemporalInvalidPlainMonthDay, "Invalid plain month day") \

View file

@ -492,8 +492,8 @@ ThrowCompletionOr<double> get_offset_nanoseconds_for(VM& vm, Value time_zone, In
// 5. Set offsetNanoseconds to (offsetNanoseconds). // 5. Set offsetNanoseconds to (offsetNanoseconds).
auto offset_nanoseconds = offset_nanoseconds_value.as_double(); auto offset_nanoseconds = offset_nanoseconds_value.as_double();
// 6. If abs(offsetNanoseconds) > nsPerDay, throw a RangeError exception. // 6. If abs(offsetNanoseconds) nsPerDay, throw a RangeError exception.
if (fabs(offset_nanoseconds) > ns_per_day) if (fabs(offset_nanoseconds) >= ns_per_day)
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidOffsetNanosecondsValue); return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidOffsetNanosecondsValue);
// 7. Return offsetNanoseconds. // 7. Return offsetNanoseconds.

View file

@ -14,7 +14,7 @@ describe("correct behavior", () => {
const calendar = new Temporal.Calendar("iso8601"); const calendar = new Temporal.Calendar("iso8601");
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return 86400000000000; return 86399999999999;
}, },
}; };
const plainDate = Temporal.Now.plainDate(calendar, "UTC"); const plainDate = Temporal.Now.plainDate(calendar, "UTC");
@ -34,6 +34,21 @@ describe("correct behavior", () => {
} }
} }
}); });
test("cannot have a time zone with more than a day", () => {
[86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
const calendar = new Temporal.Calendar("iso8601");
const timeZone = {
getOffsetNanosecondsFor() {
return offset;
},
};
expect(() => Temporal.Now.plainDate(calendar, timeZone)).toThrowWithMessage(
RangeError,
"Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
);
});
});
}); });
describe("errors", () => { describe("errors", () => {

View file

@ -12,7 +12,7 @@ describe("correct behavior", () => {
test("custom time zone", () => { test("custom time zone", () => {
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return 86400000000000; return 86399999999999;
}, },
}; };
const plainDate = Temporal.Now.plainDateISO("UTC"); const plainDate = Temporal.Now.plainDateISO("UTC");
@ -32,6 +32,20 @@ describe("correct behavior", () => {
} }
} }
}); });
test("cannot have a time zone with more than a day", () => {
[86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
const timeZone = {
getOffsetNanosecondsFor() {
return offset;
},
};
expect(() => Temporal.Now.plainDateISO(timeZone)).toThrowWithMessage(
RangeError,
"Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
);
});
});
}); });
describe("errors", () => { describe("errors", () => {

View file

@ -33,7 +33,7 @@ describe("correct behavior", () => {
const calendar = new Temporal.Calendar("iso8601"); const calendar = new Temporal.Calendar("iso8601");
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return 86400000000000; return 86399999999999;
}, },
}; };
@ -54,7 +54,7 @@ describe("correct behavior", () => {
const calendar = new Temporal.Calendar("iso8601"); const calendar = new Temporal.Calendar("iso8601");
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return -86400000000000; return -86399999999999;
}, },
}; };
@ -72,6 +72,21 @@ describe("correct behavior", () => {
}); });
expect(timeZoneTested).toBeTrue(); expect(timeZoneTested).toBeTrue();
test("cannot have a time zone with more than a day", () => {
[86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
const calendar = new Temporal.Calendar("iso8601");
const timeZone = {
getOffsetNanosecondsFor() {
return offset;
},
};
expect(() => Temporal.Now.plainDateTime(calendar, timeZone)).toThrowWithMessage(
RangeError,
"Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
);
});
});
}); });
describe("errors", () => { describe("errors", () => {

View file

@ -31,7 +31,7 @@ describe("correct behavior", () => {
test("custom time zone positive", () => { test("custom time zone positive", () => {
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return 86400000000000; return 86399999999999;
}, },
}; };
@ -51,7 +51,7 @@ describe("correct behavior", () => {
test("custom time zone negative", () => { test("custom time zone negative", () => {
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return -86400000000000; return -86399999999999;
}, },
}; };
@ -69,6 +69,20 @@ describe("correct behavior", () => {
}); });
expect(timeZoneTested).toBeTrue(); expect(timeZoneTested).toBeTrue();
test("cannot have a time zone with more than a day", () => {
[86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
const timeZone = {
getOffsetNanosecondsFor() {
return offset;
},
};
expect(() => Temporal.Now.plainDateTimeISO(timeZone)).toThrowWithMessage(
RangeError,
"Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
);
});
});
}); });
describe("errors", () => { describe("errors", () => {

View file

@ -12,13 +12,27 @@ describe("correct behavior", () => {
test("custom time zone", () => { test("custom time zone", () => {
const timeZone = { const timeZone = {
getOffsetNanosecondsFor() { getOffsetNanosecondsFor() {
return 86400000000000; return 86399999999999;
}, },
}; };
const plainTime = Temporal.Now.plainTimeISO("UTC"); const plainTime = Temporal.Now.plainTimeISO("UTC");
const plainTimeWithOffset = Temporal.Now.plainTimeISO(timeZone); const plainTimeWithOffset = Temporal.Now.plainTimeISO(timeZone);
// FIXME: Compare these in a sensible way // FIXME: Compare these in a sensible way
}); });
test("cannot have a time zone with more than a day", () => {
[86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
const timeZone = {
getOffsetNanosecondsFor() {
return offset;
},
};
expect(() => Temporal.Now.plainTimeISO(timeZone)).toThrowWithMessage(
RangeError,
"Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
);
});
});
}); });
describe("errors", () => { describe("errors", () => {