diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index aecb1723302..c81824b58e2 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -58,6 +58,7 @@ void PlainDateTimePrototype::initialize(Realm& realm) define_native_function(realm, vm.names.subtract, subtract, 1, attr); define_native_function(realm, vm.names.until, until, 1, attr); define_native_function(realm, vm.names.since, since, 1, attr); + define_native_function(realm, vm.names.round, round, 1, attr); define_native_function(realm, vm.names.equals, equals, 1, attr); define_native_function(realm, vm.names.toString, to_string, 0, attr); define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr); @@ -275,6 +276,94 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since) return TRY(difference_temporal_plain_date_time(vm, DurationOperation::Since, date_time, other, options)); } +// 5.3.32 Temporal.PlainDateTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.round +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::round) +{ + auto& realm = *vm.current_realm(); + + auto round_to_value = vm.argument(0); + + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto date_time = TRY(typed_this_object(vm)); + + // 3. If roundTo is undefined, then + if (round_to_value.is_undefined()) { + // a. Throw a TypeError exception. + return vm.throw_completion(ErrorType::TemporalMissingOptionsObject); + } + + GC::Ptr round_to; + + // 4. If roundTo is a String, then + if (round_to_value.is_string()) { + // a. Let paramString be roundTo. + auto param_string = round_to_value; + + // b. Set roundTo to OrdinaryObjectCreate(null). + round_to = Object::create(realm, nullptr); + + // c. Perform ! CreateDataPropertyOrThrow(roundTo, "smallestUnit", paramString). + MUST(round_to->create_data_property_or_throw(vm.names.smallestUnit, param_string)); + } + // 5. Else, + else { + // a. Set roundTo to ? GetOptionsObject(roundTo). + round_to = TRY(get_options_object(vm, round_to_value)); + } + + // 6. NOTE: The following steps read options and perform independent validation in alphabetical order + // (GetRoundingIncrementOption reads "roundingIncrement" and GetRoundingModeOption reads "roundingMode"). + + // 7. Let roundingIncrement be ? GetRoundingIncrementOption(roundTo). + auto rounding_increment = TRY(get_rounding_increment_option(vm, *round_to)); + + // 8. Let roundingMode be ? GetRoundingModeOption(roundTo, HALF-EXPAND). + auto rounding_mode = TRY(get_rounding_mode_option(vm, *round_to, RoundingMode::HalfExpand)); + + // 9. Let smallestUnit be ? GetTemporalUnitValuedOption(roundTo, "smallestUnit", TIME, REQUIRED, « DAY »). + auto smallest_unit = TRY(get_temporal_unit_valued_option(vm, *round_to, vm.names.smallestUnit, UnitGroup::Time, Required {}, { { Unit::Day } })); + auto smallest_unit_value = smallest_unit.get(); + + RoundingIncrement maximum { 0 }; + auto inclusive = false; + + // 10. If smallestUnit is DAY, then + if (smallest_unit_value == Unit::Day) { + // a. Let maximum be 1. + maximum = 1; + + // b. Let inclusive be true. + inclusive = true; + } + // 11. Else, + else { + // a. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit). + maximum = maximum_temporal_duration_rounding_increment(smallest_unit_value); + + // b. Assert: maximum is not UNSET. + VERIFY(!maximum.has()); + + // c. Let inclusive be false. + inclusive = false; + } + + // 12. Perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, inclusive). + TRY(validate_temporal_rounding_increment(vm, rounding_increment, maximum.get(), inclusive)); + + // 13. If smallestUnit is NANOSECOND and roundingIncrement = 1, then + if (smallest_unit_value == Unit::Nanosecond && rounding_increment == 1) { + // a. Return ! CreateTemporalDateTime(dateTime.[[ISODateTime]], dateTime.[[Calendar]]). + return MUST(create_temporal_date_time(vm, date_time->iso_date_time(), date_time->calendar())); + } + + // 14. Let result be RoundISODateTime(dateTime.[[ISODateTime]], roundingIncrement, smallestUnit, roundingMode). + auto result = round_iso_date_time(date_time->iso_date_time(), rounding_increment, smallest_unit_value, rounding_mode); + + // 15. Return ? CreateTemporalDateTime(result, dateTime.[[Calendar]]). + return TRY(create_temporal_date_time(vm, result, date_time->calendar())); +} + // 5.3.33 Temporal.PlainDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.equals JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::equals) { diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index a039efa8442..dc0fe6e1b64 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -49,6 +49,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(subtract); JS_DECLARE_NATIVE_FUNCTION(until); JS_DECLARE_NATIVE_FUNCTION(since); + JS_DECLARE_NATIVE_FUNCTION(round); JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.round.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.round.js new file mode 100644 index 00000000000..6c58bfc164d --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.round.js @@ -0,0 +1,171 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainDateTime.prototype.round).toHaveLength(1); + }); + + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + + const firstRoundedPlainDateTime = plainDateTime.round({ smallestUnit: "minute" }); + expect(firstRoundedPlainDateTime.year).toBe(2021); + expect(firstRoundedPlainDateTime.month).toBe(11); + expect(firstRoundedPlainDateTime.monthCode).toBe("M11"); + expect(firstRoundedPlainDateTime.day).toBe(3); + expect(firstRoundedPlainDateTime.hour).toBe(18); + expect(firstRoundedPlainDateTime.minute).toBe(8); + expect(firstRoundedPlainDateTime.second).toBe(0); + expect(firstRoundedPlainDateTime.millisecond).toBe(0); + expect(firstRoundedPlainDateTime.microsecond).toBe(0); + expect(firstRoundedPlainDateTime.nanosecond).toBe(0); + + const secondRoundedPlainDateTime = plainDateTime.round({ + smallestUnit: "minute", + roundingMode: "ceil", + }); + expect(secondRoundedPlainDateTime.year).toBe(2021); + expect(secondRoundedPlainDateTime.month).toBe(11); + expect(secondRoundedPlainDateTime.monthCode).toBe("M11"); + expect(secondRoundedPlainDateTime.day).toBe(3); + expect(secondRoundedPlainDateTime.hour).toBe(18); + expect(secondRoundedPlainDateTime.minute).toBe(9); + expect(secondRoundedPlainDateTime.second).toBe(0); + expect(secondRoundedPlainDateTime.millisecond).toBe(0); + expect(secondRoundedPlainDateTime.microsecond).toBe(0); + expect(secondRoundedPlainDateTime.nanosecond).toBe(0); + + const thirdRoundedPlainDateTime = plainDateTime.round({ + smallestUnit: "minute", + roundingMode: "ceil", + roundingIncrement: 30, + }); + expect(thirdRoundedPlainDateTime.year).toBe(2021); + expect(thirdRoundedPlainDateTime.month).toBe(11); + expect(thirdRoundedPlainDateTime.monthCode).toBe("M11"); + expect(thirdRoundedPlainDateTime.day).toBe(3); + expect(thirdRoundedPlainDateTime.hour).toBe(18); + expect(thirdRoundedPlainDateTime.minute).toBe(30); + expect(thirdRoundedPlainDateTime.second).toBe(0); + expect(thirdRoundedPlainDateTime.millisecond).toBe(0); + expect(thirdRoundedPlainDateTime.microsecond).toBe(0); + expect(thirdRoundedPlainDateTime.nanosecond).toBe(0); + + const fourthRoundedPlainDateTime = plainDateTime.round({ + smallestUnit: "minute", + roundingMode: "floor", + roundingIncrement: 30, + }); + expect(fourthRoundedPlainDateTime.year).toBe(2021); + expect(fourthRoundedPlainDateTime.month).toBe(11); + expect(fourthRoundedPlainDateTime.monthCode).toBe("M11"); + expect(fourthRoundedPlainDateTime.day).toBe(3); + expect(fourthRoundedPlainDateTime.hour).toBe(18); + expect(fourthRoundedPlainDateTime.minute).toBe(0); + expect(fourthRoundedPlainDateTime.second).toBe(0); + expect(fourthRoundedPlainDateTime.millisecond).toBe(0); + expect(fourthRoundedPlainDateTime.microsecond).toBe(0); + expect(fourthRoundedPlainDateTime.nanosecond).toBe(0); + + const fifthRoundedPlainDateTime = plainDateTime.round({ + smallestUnit: "hour", + roundingMode: "halfExpand", + roundingIncrement: 4, + }); + expect(fifthRoundedPlainDateTime.year).toBe(2021); + expect(fifthRoundedPlainDateTime.month).toBe(11); + expect(fifthRoundedPlainDateTime.monthCode).toBe("M11"); + expect(fifthRoundedPlainDateTime.day).toBe(3); + expect(fifthRoundedPlainDateTime.hour).toBe(20); + expect(fifthRoundedPlainDateTime.minute).toBe(0); + expect(fifthRoundedPlainDateTime.second).toBe(0); + expect(fifthRoundedPlainDateTime.millisecond).toBe(0); + expect(fifthRoundedPlainDateTime.microsecond).toBe(0); + expect(fifthRoundedPlainDateTime.nanosecond).toBe(0); + }); + + test("string argument is implicitly converted to options object", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + expect( + plainDateTime.round("minute").equals(plainDateTime.round({ smallestUnit: "minute" })) + ).toBeTrue(); + }); + + test("range boundary conditions", () => { + // PlainDateTime can represent a point of time ±10**8 days from the epoch. + const min = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1); + const max = new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999); + + ["day", "hour", "minute", "second", "millisecond", "microsecond"].forEach(smallestUnit => { + expect(() => { + min.round({ smallestUnit, roundingMode: "floor" }); + }).toThrow(RangeError); + expect(() => { + min.round({ smallestUnit, roundingMode: "ceil" }); + }).not.toThrow(); + + expect(() => { + max.round({ smallestUnit, roundingMode: "floor" }); + }).not.toThrow(); + expect(() => { + max.round({ smallestUnit, roundingMode: "ceil" }); + }).toThrow(RangeError); + }); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Temporal.PlainDateTime.prototype.round.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime"); + }); + + test("missing options object", () => { + expect(() => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + plainDateTime.round(); + }).toThrowWithMessage(TypeError, "Required options object is missing or undefined"); + }); + + test("invalid rounding mode", () => { + expect(() => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + plainDateTime.round({ smallestUnit: "second", roundingMode: "serenityOS" }); + }).toThrowWithMessage( + RangeError, + "serenityOS is not a valid value for option roundingMode" + ); + }); + + test("invalid smallest unit", () => { + expect(() => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + plainDateTime.round({ smallestUnit: "serenityOS" }); + }).toThrowWithMessage( + RangeError, + "serenityOS is not a valid value for option smallestUnit" + ); + }); + + test("increment may not be NaN", () => { + expect(() => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + plainDateTime.round({ smallestUnit: "second", roundingIncrement: NaN }); + }).toThrowWithMessage(RangeError, "NaN is not a valid value for option roundingIncrement"); + }); + + test("increment may not be smaller than 1 or larger than maximum", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 18, 8, 10, 100, 200, 300); + expect(() => { + plainDateTime.round({ smallestUnit: "second", roundingIncrement: -1 }); + }).toThrowWithMessage(RangeError, "-1 is not a valid value for option roundingIncrement"); + expect(() => { + plainDateTime.round({ smallestUnit: "second", roundingIncrement: 0 }); + }).toThrowWithMessage(RangeError, "0 is not a valid value for option roundingIncrement"); + expect(() => { + plainDateTime.round({ smallestUnit: "second", roundingIncrement: Infinity }); + }).toThrowWithMessage( + RangeError, + "Infinity is not a valid value for option roundingIncrement" + ); + }); +});