Ver Fonte

LibJS: Implement Temporal.ZonedDateTime.prototype.until()

Linus Groh há 3 anos atrás
pai
commit
7a2eeae8c6

+ 1 - 0
Userland/Libraries/LibJS/Runtime/ErrorTypes.h

@@ -197,6 +197,7 @@
     M(StringRepeatCountMustBe, "repeat count must be a {} number")                                                                      \
     M(TemporalAmbiguousMonthOfPlainMonthDay, "Accessing month of PlainMonthDay is ambiguous, use monthCode instead")                    \
     M(TemporalDifferentCalendars, "Cannot compare dates from two different calendars")                                                  \
+    M(TemporalDifferentTimeZones, "Cannot compare dates from two different time zones")                                                 \
     M(TemporalDisambiguatePossibleInstantsEarlierZero, "Cannot disambiguate zero possible instants with mode \"earlier\"")              \
     M(TemporalDisambiguatePossibleInstantsRejectMoreThanOne, "Cannot disambiguate two or more possible instants with mode \"reject\"")  \
     M(TemporalDisambiguatePossibleInstantsRejectZero, "Cannot disambiguate zero possible instants with mode \"reject\"")                \

+ 47 - 0
Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp

@@ -1608,6 +1608,53 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d
     return RoundedDuration { .years = years, .months = months, .weeks = weeks, .days = days, .hours = hours, .minutes = minutes, .seconds = seconds, .milliseconds = milliseconds, .microseconds = microseconds, .nanoseconds = nanoseconds, .remainder = remainder };
 }
 
+// 7.5.19 AdjustRoundedDurationDays ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, increment, unit, roundingMode [ , relativeTo ] ), https://tc39.es/proposal-temporal/#sec-temporal-adjustroundeddurationdays
+ThrowCompletionOr<TemporalDuration> adjust_rounded_duration_days(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object)
+{
+    auto& vm = global_object.vm();
+
+    // 1. If relativeTo is not present; or Type(relativeTo) is not Object; or relativeTo does not have an [[InitializedTemporalZonedDateTime]] internal slot; or unit is one of "year", "month", "week", or "day"; or unit is "nanosecond" and increment is 1, then
+    if (relative_to_object == nullptr || !is<ZonedDateTime>(relative_to_object) || unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv) || (unit == "nanosecond"sv && increment == 1)) {
+        // a. Return the Record { [[Years]]: years, [[Months]]: months, [[Weeks]]: weeks, [[Days]]: days, [[Hours]]: hours, [[Minutes]]: minutes, [[Seconds]]: seconds, [[Milliseconds]]: milliseconds, [[Microseconds]]: microseconds, [[Nanoseconds]]: nanoseconds }.
+        return TemporalDuration { .years = years, .months = months, .weeks = weeks, .days = days, .hours = hours, .minutes = minutes, .seconds = seconds, .milliseconds = milliseconds, .microseconds = microseconds, .nanoseconds = nanoseconds };
+    }
+
+    auto& relative_to = static_cast<ZonedDateTime&>(*relative_to_object);
+
+    // 2. Let timeRemainderNs be ! TotalDurationNanoseconds(0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 0).
+    auto time_remainder_ns = total_duration_nanoseconds(global_object, 0, hours, minutes, seconds, milliseconds, microseconds, *js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)nanoseconds)), 0)->big_integer();
+
+    // 3. Let direction be ! ℝ(Sign(𝔽(timeRemainderNs))).
+    auto direction = Temporal::sign(time_remainder_ns);
+
+    // 4. Let dayStart be ? AddZonedDateTime(relativeTo.[[Nanoseconds]], relativeTo.[[TimeZone]], relativeTo.[[Calendar]], years, months, weeks, days, 0, 0, 0, 0, 0, 0).
+    auto* day_start = TRY(add_zoned_date_time(global_object, relative_to.nanoseconds(), &relative_to.time_zone(), relative_to.calendar(), years, months, weeks, days, 0, 0, 0, 0, 0, 0));
+
+    // 5. Let dayEnd be ? AddZonedDateTime(dayStart, relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, direction, 0, 0, 0, 0, 0, 0).
+    auto* day_end = TRY(add_zoned_date_time(global_object, *day_start, &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, direction, 0, 0, 0, 0, 0, 0));
+
+    // 6. Let dayLengthNs be ℝ(dayEnd − dayStart).
+    auto day_length_ns = day_end->big_integer().minus(day_start->big_integer());
+
+    // 7. If (timeRemainderNs − dayLengthNs) × direction < 0, then
+    if (time_remainder_ns.minus(day_length_ns).multiplied_by(Crypto::SignedBigInteger { (i32)direction }).is_negative()) {
+        // a. Return the Record { [[Years]]: years, [[Months]]: months, [[Weeks]]: weeks, [[Days]]: days, [[Hours]]: hours, [[Minutes]]: minutes, [[Seconds]]: seconds, [[Milliseconds]]: milliseconds, [[Microseconds]]: microseconds, [[Nanoseconds]]: nanoseconds }.
+        return TemporalDuration { .years = years, .months = months, .weeks = weeks, .days = days, .hours = hours, .minutes = minutes, .seconds = seconds, .milliseconds = milliseconds, .microseconds = microseconds, .nanoseconds = nanoseconds };
+    }
+
+    // 8. Set timeRemainderNs to ! RoundTemporalInstant(ℤ(timeRemainderNs − dayLengthNs), increment, unit, roundingMode).
+    time_remainder_ns = round_temporal_instant(global_object, *js_bigint(vm, time_remainder_ns.minus(day_length_ns)), increment, unit, rounding_mode)->big_integer();
+
+    // 9. Let adjustedDateDuration be ? AddDuration(years, months, weeks, days, 0, 0, 0, 0, 0, 0, 0, 0, 0, direction, 0, 0, 0, 0, 0, 0, relativeTo).
+    auto adjusted_date_duration = TRY(add_duration(global_object, years, months, weeks, days, 0, 0, 0, 0, 0, 0, 0, 0, 0, direction, 0, 0, 0, 0, 0, 0, &relative_to));
+
+    // 10. Let adjustedTimeDuration be ? BalanceDuration(0, 0, 0, 0, 0, 0, timeRemainderNs, "hour").
+    auto adjusted_time_duration = TRY(balance_duration(global_object, 0, 0, 0, 0, 0, 0, *js_bigint(vm, move(time_remainder_ns)), "hour"sv));
+
+    // 11. Return the Record { [[Years]]: adjustedDateDuration.[[Years]], [[Months]]: adjustedDateDuration.[[Months]], [[Weeks]]: adjustedDateDuration.[[Weeks]], [[Days]]: adjustedDateDuration.[[Days]], [[Hours]]: adjustedTimeDuration.[[Hours]], [[Minutes]]: adjustedTimeDuration.[[Minutes]], [[Seconds]]: adjustedTimeDuration.[[Seconds]], [[Milliseconds]]: adjustedTimeDuration.[[Milliseconds]], [[Microseconds]]: adjustedTimeDuration.[[Microseconds]], [[Nanoseconds]]: adjustedTimeDuration.[[Nanoseconds]] }.
+    return TemporalDuration { .years = adjusted_date_duration.years, .months = adjusted_date_duration.months, .weeks = adjusted_date_duration.weeks, .days = adjusted_date_duration.days, .hours = adjusted_time_duration.hours, .minutes = adjusted_time_duration.minutes, .seconds = adjusted_time_duration.seconds, .milliseconds = adjusted_time_duration.milliseconds, .microseconds = adjusted_time_duration.microseconds, .nanoseconds = adjusted_time_duration.nanoseconds };
+}
+
 // 7.5.20 ToLimitedTemporalDuration ( temporalDurationLike, disallowedFields ), https://tc39.es/proposal-temporal/#sec-temporal-tolimitedtemporalduration
 ThrowCompletionOr<TemporalDuration> to_limited_temporal_duration(GlobalObject& global_object, Value temporal_duration_like, Vector<StringView> const& disallowed_fields)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/Duration.h

@@ -167,6 +167,7 @@ ThrowCompletionOr<TemporalDuration> add_duration(GlobalObject&, double years1, d
 ThrowCompletionOr<MoveRelativeDateResult> move_relative_date(GlobalObject&, Object& calendar, PlainDate& relative_to, Duration& duration);
 ThrowCompletionOr<ZonedDateTime*> move_relative_zoned_date_time(GlobalObject&, ZonedDateTime&, double years, double months, double weeks, double days);
 ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object = nullptr);
+ThrowCompletionOr<TemporalDuration> adjust_rounded_duration_days(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object = nullptr);
 ThrowCompletionOr<TemporalDuration> to_limited_temporal_duration(GlobalObject&, Value temporal_duration_like, Vector<StringView> const& disallowed_fields);
 String temporal_duration_to_string(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision);
 

+ 75 - 0
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp

@@ -72,6 +72,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
     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.round, round, 1, attr);
     define_native_function(vm.names.equals, equals, 1, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
@@ -942,6 +943,80 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::subtract)
     return MUST(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, time_zone, calendar));
 }
 
+// 6.3.37 Temporal.ZonedDateTime.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.until
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::until)
+{
+    // 1. Let zonedDateTime be the this value.
+    // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+    auto* zoned_date_time = TRY(typed_this_object(global_object));
+
+    // 3. Set other to ? ToTemporalZonedDateTime(other).
+    auto* other = TRY(to_temporal_zoned_date_time(global_object, vm.argument(0)));
+
+    // 4. If ? CalendarEquals(zonedDateTime.[[Calendar]], other.[[Calendar]]) is false, then
+    if (!TRY(calendar_equals(global_object, zoned_date_time->calendar(), other->calendar()))) {
+        // a. Throw a RangeError exception.
+        return vm.throw_completion<RangeError>(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("hour", smallestUnit).
+    auto default_largest_unit = larger_of_two_temporal_units("hour"sv, *smallest_unit);
+
+    // 8. Let largestUnit be ? ToLargestTemporalUnit(options, « », "auto", defaultLargestUnit).
+    auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, {}, "auto"sv, move(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. Let maximum be ! MaximumTemporalDurationRoundingIncrement(smallestUnit).
+    auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
+
+    // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+
+    // 13. If largestUnit is not one of "year", "month", "week", or "day", then
+    if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
+        // a. Let differenceNs be ! DifferenceInstant(zonedDateTime.[[Nanoseconds]], other.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode).
+        auto* difference_ns = difference_instant(global_object, zoned_date_time->nanoseconds(), other->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode);
+
+        // b. Let balanceResult be ! BalanceDuration(0, 0, 0, 0, 0, 0, differenceNs, largestUnit).
+        auto balance_result = MUST(balance_duration(global_object, 0, 0, 0, 0, 0, 0, *difference_ns, *largest_unit));
+
+        // c. Return ? CreateTemporalDuration(0, 0, 0, 0, balanceResult.[[Hours]], balanceResult.[[Minutes]], balanceResult.[[Seconds]], balanceResult.[[Milliseconds]], balanceResult.[[Microseconds]], balanceResult.[[Nanoseconds]]).
+        return TRY(create_temporal_duration(global_object, 0, 0, 0, 0, balance_result.hours, balance_result.minutes, balance_result.seconds, balance_result.milliseconds, balance_result.microseconds, balance_result.nanoseconds));
+    }
+
+    // 14. If ? TimeZoneEquals(zonedDateTime.[[TimeZone]], other.[[TimeZone]]) is false, then
+    if (!TRY(time_zone_equals(global_object, zoned_date_time->time_zone(), other->time_zone()))) {
+        // a. Throw a RangeError exception.
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDifferentTimeZones);
+    }
+
+    // 15. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit).
+    auto* until_options = TRY(merge_largest_unit_option(global_object, *options, *largest_unit));
+
+    // 16. Let difference be ? DifferenceZonedDateTime(zonedDateTime.[[Nanoseconds]], other.[[Nanoseconds]], zonedDateTime.[[TimeZone]], zonedDateTime.[[Calendar]], largestUnit, untilOptions).
+    auto difference = TRY(difference_zoned_date_time(global_object, zoned_date_time->nanoseconds(), other->nanoseconds(), zoned_date_time->time_zone(), zoned_date_time->calendar(), *largest_unit, until_options));
+
+    // 17. Let roundResult be ? RoundDuration(difference.[[Years]], difference.[[Months]], difference.[[Weeks]], difference.[[Days]], difference.[[Hours]], difference.[[Minutes]], difference.[[Seconds]], difference.[[Milliseconds]], difference.[[Microseconds]], difference.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, zonedDateTime).
+    auto round_result = TRY(round_duration(global_object, difference.years, difference.months, difference.weeks, difference.days, difference.hours, difference.minutes, difference.seconds, difference.milliseconds, difference.microseconds, difference.nanoseconds, rounding_increment, *smallest_unit, rounding_mode, zoned_date_time));
+
+    // 18. Let result be ? AdjustRoundedDurationDays(roundResult.[[Years]], roundResult.[[Months]], roundResult.[[Weeks]], roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, zonedDateTime).
+    auto result = TRY(adjust_rounded_duration_days(global_object, round_result.years, round_result.months, round_result.weeks, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, round_result.nanoseconds, rounding_increment, *smallest_unit, rounding_mode, zoned_date_time));
+
+    // 19. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
+    return TRY(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
+}
+
 // 6.3.39 Temporal.ZonedDateTime.prototype.round ( roundTo ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.round
 JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::round)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h

@@ -56,6 +56,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(with_calendar);
     JS_DECLARE_NATIVE_FUNCTION(add);
     JS_DECLARE_NATIVE_FUNCTION(subtract);
+    JS_DECLARE_NATIVE_FUNCTION(until);
     JS_DECLARE_NATIVE_FUNCTION(round);
     JS_DECLARE_NATIVE_FUNCTION(equals);
     JS_DECLARE_NATIVE_FUNCTION(to_string);

+ 115 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.until.js

@@ -0,0 +1,115 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.ZonedDateTime.prototype.until).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const values = [
+            [0n, 0n, "PT0S"],
+            [123456789n, 2345679011n, "PT2.222222222S"],
+            [0n, 123456789n, "PT0.123456789S"],
+            [123456789n, 0n, "-PT0.123456789S"],
+            [0n, 123456789123456789n, "PT34293H33M9.123456789S"],
+            [123456789123456789n, 0n, "-PT34293H33M9.123456789S"],
+        ];
+        const utc = new Temporal.TimeZone("UTC");
+        for (const [arg, argOther, expected] of values) {
+            const zonedDateTime = new Temporal.ZonedDateTime(arg, utc);
+            const other = new Temporal.ZonedDateTime(argOther, utc);
+            expect(zonedDateTime.until(other).toString()).toBe(expected);
+        }
+    });
+
+    test("smallestUnit option", () => {
+        const utc = new Temporal.TimeZone("UTC");
+        const zonedDateTime = new Temporal.ZonedDateTime(0n, utc);
+        const other = new Temporal.ZonedDateTime(34401906007008009n, utc);
+        const values = [
+            ["year", "P1Y"],
+            ["month", "P13M"],
+            ["week", "P56W"],
+            ["day", "P398D"],
+            ["hour", "PT9556H"],
+            ["minute", "PT9556H5M"],
+            ["second", "PT9556H5M6S"],
+            ["millisecond", "PT9556H5M6.007S"],
+            ["microsecond", "PT9556H5M6.007008S"],
+            ["nanosecond", "PT9556H5M6.007008009S"],
+        ];
+        for (const [smallestUnit, expected] of values) {
+            expect(zonedDateTime.until(other, { smallestUnit }).toString()).toBe(expected);
+        }
+    });
+
+    test("largestUnit option", () => {
+        const utc = new Temporal.TimeZone("UTC");
+        const zonedDateTime = new Temporal.ZonedDateTime(0n, utc);
+        const other = new Temporal.ZonedDateTime(34401906007008009n, utc);
+        const values = [
+            ["year", "P1Y1M2DT4H5M6.007008009S"],
+            ["month", "P13M2DT4H5M6.007008009S"],
+            ["week", "P56W6DT4H5M6.007008009S"],
+            ["day", "P398DT4H5M6.007008009S"],
+            ["hour", "PT9556H5M6.007008009S"],
+            ["minute", "PT573365M6.007008009S"],
+            ["second", "PT34401906.007008009S"],
+            ["millisecond", "PT34401906.007008009S"],
+            ["microsecond", "PT34401906.007008009S"],
+            ["nanosecond", "PT34401906.007008008S"],
+        ];
+        for (const [largestUnit, expected] of values) {
+            expect(zonedDateTime.until(other, { largestUnit }).toString()).toBe(expected);
+        }
+    });
+});
+
+describe("errors", () => {
+    test("this value must be a Temporal.ZonedDateTime object", () => {
+        expect(() => {
+            Temporal.ZonedDateTime.prototype.until.call("foo", {});
+        }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
+    });
+
+    test("cannot compare dates from different calendars", () => {
+        const calendarOne = {
+            toString() {
+                return "calendarOne";
+            },
+        };
+
+        const calendarTwo = {
+            toString() {
+                return "calendarTwo";
+            },
+        };
+
+        const utc = new Temporal.TimeZone("UTC");
+        const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, utc, calendarOne);
+        const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, utc, calendarTwo);
+
+        expect(() => {
+            zonedDateTimeOne.until(zonedDateTimeTwo);
+        }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
+    });
+
+    test("cannot compare dates from different time zones", () => {
+        const timeZoneOne = {
+            toString() {
+                return "timeZoneOne";
+            },
+        };
+
+        const timeZoneTwo = {
+            toString() {
+                return "timeZoneTwo";
+            },
+        };
+
+        const zonedDateTimeOne = new Temporal.ZonedDateTime(0n, timeZoneOne);
+        const zonedDateTimeTwo = new Temporal.ZonedDateTime(0n, timeZoneTwo);
+
+        expect(() => {
+            zonedDateTimeOne.until(zonedDateTimeTwo, { largestUnit: "day" });
+        }).toThrowWithMessage(RangeError, "Cannot compare dates from two different time zones");
+    });
+});