diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp b/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp index d1ee1ff2389..f268cd52932 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp @@ -367,6 +367,42 @@ ThrowCompletionOr difference_plain_date_time_with_total(VM& return TRY(total_relative_duration(vm, diff, dest_epoch_ns, iso_date_time1, {}, calendar, unit)); } +// 5.5.15 DifferenceTemporalPlainDateTime ( operation, dateTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaindatetime +ThrowCompletionOr> difference_temporal_plain_date_time(VM& vm, DurationOperation operation, PlainDateTime const& date_time, Value other_value, Value options) +{ + // 1. Set other to ? ToTemporalDateTime(other). + auto other = TRY(to_temporal_date_time(vm, other_value)); + + // 2. If CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception. + if (!calendar_equals(date_time.calendar(), other->calendar())) + return vm.throw_completion(ErrorType::TemporalDifferentCalendars); + + // 3. Let resolvedOptions be ? GetOptionsObject(options). + auto resolved_options = TRY(get_options_object(vm, options)); + + // 4. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATETIME, « », NANOSECOND, DAY). + auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::DateTime, {}, Unit::Nanosecond, Unit::Day)); + + // 5. If CompareISODateTime(dateTime.[[ISODateTime]], other.[[ISODateTime]]) = 0, then + if (compare_iso_date_time(date_time.iso_date_time(), other->iso_date_time()) == 0) { + // a. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0). + return MUST(create_temporal_duration(vm, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + } + + // 6. Let internalDuration be ? DifferencePlainDateTimeWithRounding(dateTime.[[ISODateTime]], other.[[ISODateTime]], dateTime.[[Calendar]], settings.[[LargestUnit]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). + auto internal_duration = TRY(difference_plain_date_time_with_rounding(vm, date_time.iso_date_time(), other->iso_date_time(), date_time.calendar(), settings.largest_unit, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode)); + + // 7. Let result be ? TemporalDurationFromInternal(internalDuration, settings.[[LargestUnit]]). + auto result = TRY(temporal_duration_from_internal(vm, internal_duration, settings.largest_unit)); + + // 8. If operation is SINCE, set result to CreateNegatedTemporalDuration(result). + if (operation == DurationOperation::Since) + result = create_negated_temporal_duration(vm, result); + + // 9. Return result. + return result; +} + // 5.5.16 AddDurationToDateTime ( operation, dateTime, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtodatetime ThrowCompletionOr> add_duration_to_date_time(VM& vm, ArithmeticOperation operation, PlainDateTime const& date_time, Value temporal_duration_like, Value options) { diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h b/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h index 9c645b603d6..9d38caa2002 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h @@ -44,6 +44,7 @@ ISODateTime round_iso_date_time(ISODateTime const&, u64 increment, Unit, Roundin ThrowCompletionOr difference_iso_date_time(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit); ThrowCompletionOr difference_plain_date_time_with_rounding(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit, u64 rounding_increment, Unit smallest_unit, RoundingMode); ThrowCompletionOr difference_plain_date_time_with_total(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit); +ThrowCompletionOr> difference_temporal_plain_date_time(VM&, DurationOperation, PlainDateTime const&, Value other, Value options); ThrowCompletionOr> add_duration_to_date_time(VM&, ArithmeticOperation, PlainDateTime const&, Value temporal_duration_like, Value options); } diff --git a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 429680b5012..aecb1723302 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -8,6 +8,7 @@ #include #include +#include #include namespace JS::Temporal { @@ -55,6 +56,8 @@ void PlainDateTimePrototype::initialize(Realm& realm) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(realm, vm.names.add, add, 1, attr); 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.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); @@ -244,6 +247,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::subtract) return TRY(add_duration_to_date_time(vm, ArithmeticOperation::Subtract, date_time, temporal_duration_like, options)); } +// 5.3.30 Temporal.PlainDateTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.until +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto date_time = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalPlainDateTime(UNTIL, dateTime, other, options). + return TRY(difference_temporal_plain_date_time(vm, DurationOperation::Until, date_time, other, options)); +} + +// 5.3.31 Temporal.PlainDateTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.since +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since) +{ + auto other = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto date_time = TRY(typed_this_object(vm)); + + // 3. Return ? DifferenceTemporalPlainDateTime(SINCE, dateTime, other, options). + return TRY(difference_temporal_plain_date_time(vm, DurationOperation::Since, date_time, other, options)); +} + // 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 b3ebaca2b6a..a039efa8442 100644 --- a/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -47,6 +47,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(add); JS_DECLARE_NATIVE_FUNCTION(subtract); + JS_DECLARE_NATIVE_FUNCTION(until); + JS_DECLARE_NATIVE_FUNCTION(since); 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.since.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.since.js new file mode 100644 index 00000000000..07af370a1dc --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.since.js @@ -0,0 +1,87 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainDateTime.prototype.since).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [[0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], "PT0S"], + [[2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9], "P394DT1H1M1.001001001S"], + [[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 1, 0, 0, 0, 0, 0, 0], "P399DT4H5M6.007008009S"], + [[0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9], "-P399DT4H5M6.007008009S"], + [ + [0, 12, 31, 23, 59, 59, 999, 999, 999], + [0, 1, 1, 0, 0, 0, 0, 0, 0], + "P365DT23H59M59.999999999S", + ], + [ + [0, 1, 1, 0, 0, 0, 0, 0, 0], + [0, 12, 31, 23, 59, 59, 999, 999, 999], + "-P365DT23H59M59.999999999S", + ], + ]; + for (const [args, argsOther, expected] of values) { + const plainDateTime = new Temporal.PlainDateTime(...args); + const other = new Temporal.PlainDateTime(...argsOther); + expect(plainDateTime.since(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const plainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9); + const other = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0); + const values = [ + ["year", "P1Y"], + ["month", "P13M"], + ["week", "P57W"], + ["day", "P399D"], + ["hour", "P399DT4H"], + ["minute", "P399DT4H5M"], + ["second", "P399DT4H5M6S"], + ["millisecond", "P399DT4H5M6.007S"], + ["microsecond", "P399DT4H5M6.007008S"], + ["nanosecond", "P399DT4H5M6.007008009S"], + ]; + for (const [smallestUnit, expected] of values) { + expect(plainDateTime.since(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const plainDateTime = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9); + const other = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0); + const values = [ + ["year", "P1Y1M2DT4H5M6.007008009S"], + ["month", "P13M2DT4H5M6.007008009S"], + ["week", "P57WT4H5M6.007008009S"], + ["day", "P399DT4H5M6.007008009S"], + ["hour", "PT9580H5M6.007008009S"], + ["minute", "PT574805M6.007008009S"], + ["second", "PT34488306.007008009S"], + ["millisecond", "PT34488306.007008009S"], + ["microsecond", "PT34488306.007008009S"], + ["nanosecond", "PT34488306.007008008S"], + ]; + for (const [largestUnit, expected] of values) { + expect(plainDateTime.since(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Temporal.PlainDateTime.prototype.since.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime"); + }); + + test("cannot compare dates from different calendars", () => { + const args = [1970, 1, 1, 0, 0, 0, 0, 0, 0]; + const plainDateTimeOne = new Temporal.PlainDateTime(...args, "iso8601"); + const plainDateTimeTwo = new Temporal.PlainDateTime(...args, "gregory"); + + expect(() => { + plainDateTimeOne.since(plainDateTimeTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.until.js b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.until.js new file mode 100644 index 00000000000..d1a9e6913f3 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.until.js @@ -0,0 +1,87 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainDateTime.prototype.until).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [[0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], "PT0S"], + [[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 10], "P394DT1H1M1.001001001S"], + [[0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9], "P399DT4H5M6.007008009S"], + [[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 1, 0, 0, 0, 0, 0, 0], "-P399DT4H5M6.007008009S"], + [ + [0, 1, 1, 0, 0, 0, 0, 0, 0], + [0, 12, 31, 23, 59, 59, 999, 999, 999], + "P365DT23H59M59.999999999S", + ], + [ + [0, 12, 31, 23, 59, 59, 999, 999, 999], + [0, 1, 1, 0, 0, 0, 0, 0, 0], + "-P365DT23H59M59.999999999S", + ], + ]; + for (const [args, argsOther, expected] of values) { + const plainDateTime = new Temporal.PlainDateTime(...args); + const other = new Temporal.PlainDateTime(...argsOther); + expect(plainDateTime.until(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0); + const other = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9); + const values = [ + ["year", "P1Y"], + ["month", "P13M"], + ["week", "P57W"], + ["day", "P399D"], + ["hour", "P399DT4H"], + ["minute", "P399DT4H5M"], + ["second", "P399DT4H5M6S"], + ["millisecond", "P399DT4H5M6.007S"], + ["microsecond", "P399DT4H5M6.007008S"], + ["nanosecond", "P399DT4H5M6.007008009S"], + ]; + for (const [smallestUnit, expected] of values) { + expect(plainDateTime.until(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0); + const other = new Temporal.PlainDateTime(1, 2, 3, 4, 5, 6, 7, 8, 9); + const values = [ + ["year", "P1Y1M2DT4H5M6.007008009S"], + ["month", "P13M2DT4H5M6.007008009S"], + ["week", "P57WT4H5M6.007008009S"], + ["day", "P399DT4H5M6.007008009S"], + ["hour", "PT9580H5M6.007008009S"], + ["minute", "PT574805M6.007008009S"], + ["second", "PT34488306.007008009S"], + ["millisecond", "PT34488306.007008009S"], + ["microsecond", "PT34488306.007008009S"], + ["nanosecond", "PT34488306.007008008S"], + ]; + for (const [largestUnit, expected] of values) { + expect(plainDateTime.until(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Temporal.PlainDateTime.prototype.until.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime"); + }); + + test("cannot compare dates from different calendars", () => { + const args = [1970, 1, 1, 0, 0, 0, 0, 0, 0]; + const plainDateTimeOne = new Temporal.PlainDateTime(...args, "iso8601"); + const plainDateTimeTwo = new Temporal.PlainDateTime(...args, "gregory"); + + expect(() => { + plainDateTimeOne.until(plainDateTimeTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); +});