diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 05dcfcc63b..25775b580b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -65,6 +65,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.add, add, 1, attr); define_native_function(vm.names.subtract, subtract, 1, attr); define_native_function(vm.names.until, until, 1, attr); + define_native_function(vm.names.since, since, 1, attr); define_native_function(vm.names.round, round, 1, attr); define_native_function(vm.names.equals, equals, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); @@ -561,6 +562,63 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until) return TRY(create_temporal_duration(global_object, round_result.years, round_result.months, round_result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)); } +// 5.3.29 Temporal.PlainDateTime.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.since +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = TRY(typed_this_object(global_object)); + + // 3. Set other to ? ToTemporalDateTime(other). + auto* other = TRY(to_temporal_date_time(global_object, vm.argument(0))); + + // 4. If ? CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception. + if (!TRY(calendar_equals(global_object, date_time->calendar(), other->calendar()))) + return vm.throw_completion(global_object, ErrorType::TemporalDifferentCalendars); + + // 5. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(1))); + + // 6. Let smallestUnit be ? ToSmallestTemporalUnit(options, « », "nanosecond"). + auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, {}, "nanosecond"sv)); + + // 7. Let defaultLargestUnit be ! LargerOfTwoTemporalUnits("day", smallestUnit). + auto default_largest_unit = larger_of_two_temporal_units("day"sv, *smallest_unit); + + // 8. Let largestUnit be ? ToLargestTemporalUnit(options, « », "auto", defaultLargestUnit). + auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, {}, "auto"sv, default_largest_unit)); + + // 9. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit). + TRY(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit)); + + // 10. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc"). + auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv)); + + // 11. Set roundingMode to ! NegateTemporalRoundingMode(roundingMode). + rounding_mode = negate_temporal_rounding_mode(rounding_mode); + + // 12. Let maximum be ! MaximumTemporalDurationRoundingIncrement(smallestUnit). + auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); + + // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional {}, false)); + + // 14. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options). + auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), largest_unit, options)); + + // 15. Let relativeTo be ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). + auto* relative_to = MUST(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar())); + + // 16. Let roundResult be ? RoundDuration(diff.[[Years]], diff.[[Months]], diff.[[Weeks]], diff.[[Days]], diff.[[Hours]], diff.[[Minutes]], diff.[[Seconds]], diff.[[Milliseconds]], diff.[[Microseconds]], diff.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, relativeTo). + auto round_result = TRY(round_duration(global_object, diff.years, diff.months, diff.weeks, diff.days, diff.hours, diff.minutes, diff.seconds, diff.milliseconds, diff.microseconds, diff.nanoseconds, rounding_increment, *smallest_unit, rounding_mode, relative_to)); + + // 17. Let result be ! BalanceDuration(roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], largestUnit). + auto result = MUST(balance_duration(global_object, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, *js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)round_result.nanoseconds)), largest_unit)); + + // 18. Return ? CreateTemporalDuration(−roundResult.[[Years]], −roundResult.[[Months]], −roundResult.[[Weeks]], −result.[[Days]], −result.[[Hours]], −result.[[Minutes]], −result.[[Seconds]], −result.[[Milliseconds]], −result.[[Microseconds]], −result.[[Nanoseconds]]). + return TRY(create_temporal_duration(global_object, -round_result.years, -round_result.months, -round_result.weeks, -result.days, -result.hours, -result.minutes, -result.seconds, -result.milliseconds, -result.microseconds, -result.nanoseconds)); +} + // 5.3.30 Temporal.PlainDateTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.round JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::round) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 74b60d2031..f46af1975e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -48,6 +48,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(add); 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); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.since.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.since.js new file mode 100644 index 0000000000..126925b178 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.since.js @@ -0,0 +1,99 @@ +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 calendarOne = { + toString() { + return "calendarOne"; + }, + }; + + const calendarTwo = { + toString() { + return "calendarTwo"; + }, + }; + + const args = [1970, 1, 1, 0, 0, 0, 0, 0, 0]; + const plainDateTimeOne = new Temporal.PlainDateTime(...args, calendarOne); + const plainDateTimeTwo = new Temporal.PlainDateTime(...args, calendarTwo); + + expect(() => { + plainDateTimeOne.since(plainDateTimeTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); +});