diff options
author | Luke Wilde <lukew@serenityos.org> | 2022-07-22 16:08:32 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-22 17:10:03 +0100 |
commit | 61847b3cef76b99603467b7b540675cda0b8c9a4 (patch) | |
tree | 721d8e84379ac6b34dbd246e006b524b46a56d89 /Userland | |
parent | 114120852de801fb83fc83c7357eff36a0d34ad2 (diff) | |
download | serenity-61847b3cef76b99603467b7b540675cda0b8c9a4.zip |
LibJS: Disallow negative day lengths in ZonedDateTime.protoype.round
This is a normative change in the Temporal spec.
See: https://github.com/tc39/proposal-temporal/commit/6f04074
Diffstat (limited to 'Userland')
3 files changed, 34 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index 2d3c25a86f..4d69be9572 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -273,7 +273,8 @@ M(TemporalPropertyMustBeFinite, "Property must not be Infinity") \ M(TemporalPropertyMustBePositiveInteger, "Property must be a positive integer") \ M(TemporalTimeZoneOffsetStringMismatch, "Time zone offset string mismatch: '{}' is not equal to '{}'") \ - M(TemporalZonedDateTimeRoundZeroLengthDay, "Cannot round a ZonedDateTime in a calendar that has zero-length days") \ + M(TemporalZonedDateTimeRoundZeroOrNegativeLengthDay, "Cannot round a ZonedDateTime in a calendar or time zone that has zero or " \ + "negative length days") \ M(ThisHasNotBeenInitialized, "|this| has not been initialized") \ M(ThisIsAlreadyInitialized, "|this| is already initialized") \ M(ToObjectNullOrUndefined, "ToObject on null or undefined") \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 6efe119651..b4745dc092 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -1018,10 +1018,10 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::round) // 18. Let dayLengthNs be ℝ(endNs - startNs). auto day_length_ns = end_ns->big_integer().minus(start_ns.big_integer()).to_double(); - // 19. If dayLengthNs is 0, then - if (day_length_ns == 0) { + // 19. If dayLengthNs ≤ 0, then + if (day_length_ns <= 0) { // a. Throw a RangeError exception. - return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalZonedDateTimeRoundZeroLengthDay); + return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalZonedDateTimeRoundZeroOrNegativeLengthDay); } // 20. Let roundResult be ! RoundISODateTime(temporalDateTime.[[ISOYear]], temporalDateTime.[[ISOMonth]], temporalDateTime.[[ISODay]], temporalDateTime.[[ISOHour]], temporalDateTime.[[ISOMinute]], temporalDateTime.[[ISOSecond]], temporalDateTime.[[ISOMillisecond]], temporalDateTime.[[ISOMicrosecond]], temporalDateTime.[[ISONanosecond]], roundingIncrement, smallestUnit, roundingMode, dayLengthNs). diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.round.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.round.js index 80d64e6704..66397b4120 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.round.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.round.js @@ -112,7 +112,35 @@ describe("errors", () => { zonedDateTime.round({ smallestUnit: "second" }); }).toThrowWithMessage( RangeError, - "Cannot round a ZonedDateTime in a calendar that has zero-length days" + "Cannot round a ZonedDateTime in a calendar or time zone that has zero or negative length days" + ); + }); + + test("time zone with negative length days", () => { + class CustomTimeZone extends Temporal.TimeZone { + constructor() { + super("UTC"); + this.getPossibleInstantsForCallCount = 0; + } + + getPossibleInstantsFor(temporalDateTime) { + this.getPossibleInstantsForCallCount++; + + if (this.getPossibleInstantsForCallCount === 2) { + return [new Temporal.Instant(-1n)]; + } + + return super.getPossibleInstantsFor(temporalDateTime); + } + } + + const zonedDateTime = new Temporal.ZonedDateTime(1n, new CustomTimeZone()); + + expect(() => { + zonedDateTime.round({ smallestUnit: "second" }); + }).toThrowWithMessage( + RangeError, + "Cannot round a ZonedDateTime in a calendar or time zone that has zero or negative length days" ); }); }); |