diff options
author | Linus Groh <mail@linusgroh.de> | 2022-03-10 17:56:58 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-10 23:20:39 +0100 |
commit | 2bae040bc9237b6c81ce63289bfdc8463a1f2463 (patch) | |
tree | 1c1d34e0711f6d6ea6c28615d9de38b528130213 | |
parent | 0d06f3655fbb23a31e7d1da1ebe9596a5526f5f5 (diff) | |
download | serenity-2bae040bc9237b6c81ce63289bfdc8463a1f2463.zip |
LibJS: Remove Sign abstract operation
This is an editorial change in the Temporal spec.
See: https://github.com/tc39/proposal-temporal/commit/bbc1ebc
4 files changed, 77 insertions, 109 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index f3bff4b04e..a36598cdea 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -964,36 +964,7 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u return String::formatted("{}.{}", seconds_string, fraction_string); } -// 13.27 Sign ( n ), https://tc39.es/proposal-temporal/#sec-temporal-sign -double sign(double n) -{ - // 1. If n is NaN, n is +0𝔽, or n is −0𝔽, return n. - if (isnan(n) || n == 0) - return n; - - // 2. If n < +0𝔽, return −1𝔽. - if (n < 0) - return -1; - - // 3. Return 1𝔽. - return 1; -} - -double sign(Crypto::SignedBigInteger const& n) -{ - // 1. If n is NaN, n is +0𝔽, or n is −0𝔽, return n. - if (n == Crypto::SignedBigInteger { 0 }) - return n.is_negative() ? -0 : 0; - - // 2. If n < +0𝔽, return −1𝔽. - if (n.is_negative()) - return -1; - - // 3. Return 1𝔽. - return 1; -} - -// 13.28 ConstrainToRange ( x, minimum, maximum ), https://tc39.es/proposal-temporal/#sec-temporal-constraintorange +// 13.27 ConstrainToRange ( x, minimum, maximum ), https://tc39.es/proposal-temporal/#sec-temporal-constraintorange double constrain_to_range(double x, double minimum, double maximum) { // 1. Assert: x, minimum and maximum are mathematical values. @@ -1007,7 +978,7 @@ double constrain_to_range(double x, double minimum, double maximum) // it uses mathematical values which can be arbitrarily (but not infinitely) large. // Incidentally V8's Temporal implementation does the same :^) -// 13.31 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement +// 13.30 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement i64 round_number_to_increment(double x, u64 increment, StringView rounding_mode) { // 1. Assert: x and increment are mathematical values. @@ -1044,7 +1015,7 @@ i64 round_number_to_increment(double x, u64 increment, StringView rounding_mode) return (i64)rounded * (i64)increment; } -// 13.31 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement +// 13.30 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, u64 increment, StringView rounding_mode) { auto& heap = global_object.heap(); @@ -1098,7 +1069,7 @@ BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, return js_bigint(heap, rounded.multiplied_by(increment_big_int)); } -// 13.33 ParseISODateTime ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parseisodatetime +// 13.32 ParseISODateTime ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parseisodatetime ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object, ParseResult const& parse_result) { auto& vm = global_object.vm(); @@ -1227,7 +1198,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object, return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = Optional<String>(move(calendar_part)) }; } -// 13.34 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring +// 13.33 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1263,7 +1234,7 @@ ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject& g return TemporalInstant { .year = result.year, .month = result.month, .day = result.day, .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .time_zone_offset = move(offset_string) }; } -// 13.35 ParseTemporalZonedDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalzoneddatetimestring +// 13.34 ParseTemporalZonedDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalzoneddatetimestring ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1289,7 +1260,7 @@ ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_string(G return TemporalZonedDateTime { .date_time = move(result), .time_zone = move(time_zone_result) }; } -// 13.36 ParseTemporalCalendarString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalcalendarstring +// 13.35 ParseTemporalCalendarString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalcalendarstring ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1316,7 +1287,7 @@ ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject& global_ob return id_part.value(); } -// 13.37 ParseTemporalDateString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatestring +// 13.36 ParseTemporalDateString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatestring ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1343,7 +1314,7 @@ ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject& global_ return TemporalDate { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) }; } -// 13.38 ParseTemporalDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatetimestring +// 13.37 ParseTemporalDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatetimestring ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1367,7 +1338,7 @@ ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject& glo return parse_iso_date_time(global_object, *parse_result); } -// 13.39 ParseTemporalDurationString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldurationstring +// 13.38 ParseTemporalDurationString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldurationstring ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1509,7 +1480,7 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject& g return create_duration_record(global_object, years * factor, months * factor, weeks * factor, days * factor, hours * factor, floor(minutes) * factor, floor(seconds) * factor, floor(milliseconds) * factor, floor(microseconds) * factor, floor(nanoseconds) * factor); } -// 13.40 ParseTemporalMonthDayString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalmonthdaystring +// 13.39 ParseTemporalMonthDayString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalmonthdaystring ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1545,7 +1516,7 @@ ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject return TemporalMonthDay { .year = year, .month = result.month, .day = result.day, .calendar = move(result.calendar) }; } -// 13.41 ParseTemporalRelativeToString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalrelativetostring +// 13.40 ParseTemporalRelativeToString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalrelativetostring ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1594,7 +1565,7 @@ ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_string(Globa return TemporalZonedDateTime { .date_time = move(result), .time_zone = { .z = z, .offset_string = move(offset_string), .name = move(time_zone) } }; } -// 13.42 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring +// 13.41 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1625,7 +1596,7 @@ ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_ return TemporalTime { .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .calendar = move(result.calendar) }; } -// 13.43 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring +// 13.42 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1668,7 +1639,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject return TemporalTimeZone { .z = false, .offset_string = Optional<String>(move(offset_string)), .name = Optional<String>(move(name)) }; } -// 13.44 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring +// 13.43 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject& global_object, String const& iso_string) { auto& vm = global_object.vm(); @@ -1695,7 +1666,7 @@ ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObje return TemporalYearMonth { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) }; } -// 13.45 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger +// 13.44 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value argument) { auto& vm = global_object.vm(); @@ -1713,7 +1684,7 @@ ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value return integer; } -// 13.48 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields +// 13.47 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields) { auto& vm = global_object.vm(); @@ -1764,7 +1735,7 @@ ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object, return result; } -// 13.49 PreparePartialTemporalFields ( fields, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-preparepartialtemporalfields +// 13.48 PreparePartialTemporalFields ( fields, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-preparepartialtemporalfields ThrowCompletionOr<Object*> prepare_partial_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names) { auto& vm = global_object.vm(); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index f54c17c41c..0da3a47421 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -144,7 +144,7 @@ ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument); ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields); ThrowCompletionOr<Object*> prepare_partial_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names); -// 13.46 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity +// 13.45 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity template<typename... Args> ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args) { @@ -163,7 +163,7 @@ ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_obje return integer; } -// 13.47 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding +// 13.46 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding template<typename... Args> ThrowCompletionOr<double> to_integer_without_rounding(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp index 01c798e57d..bb672a2117 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp @@ -1276,32 +1276,28 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d // x. Set days to days - daysPassed. days -= days_passed; - // y. Let sign be ! Sign(days). - auto sign = JS::Temporal::sign(days); + // y. If days < 0, let sign be −1; else, let sign be 1. + auto sign = days < 0 ? -1 : 1; - // z. If sign is 0, set sign to 1. - if (sign == 0) - sign = 1; - - // aa. Let oneYear be ? CreateTemporalDuration(sign, 0, 0, 0, 0, 0, 0, 0, 0, 0). + // z. Let oneYear be ? CreateTemporalDuration(sign, 0, 0, 0, 0, 0, 0, 0, 0, 0). auto* one_year = TRY(create_temporal_duration(global_object, sign, 0, 0, 0, 0, 0, 0, 0, 0, 0)); - // ab. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneYear). + // aa. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneYear). auto move_result = TRY(move_relative_date(global_object, *calendar, *relative_to, *one_year)); - // ac. Let oneYearDays be moveResult.[[Days]]. + // ab. Let oneYearDays be moveResult.[[Days]]. auto one_year_days = move_result.days; - // ad. Let fractionalYears be years + days / abs(oneYearDays). + // ac. Let fractionalYears be years + days / abs(oneYearDays). auto fractional_years = years + days / fabs(one_year_days); - // ae. Set years to ! RoundNumberToIncrement(fractionalYears, increment, roundingMode). + // ad. Set years to ! RoundNumberToIncrement(fractionalYears, increment, roundingMode). years = (double)round_number_to_increment(fractional_years, increment, rounding_mode); - // af. Set remainder to fractionalYears - years. + // ae. Set remainder to fractionalYears - years. remainder = fractional_years - years; - // ag. Set months, weeks, and days to 0. + // af. Set months, weeks, and days to 0. months = 0; weeks = 0; days = 0; @@ -1340,26 +1336,22 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d // j. Let days be days + weeksInDays. days += weeks_in_days; - // k. Let sign be ! Sign(days). - auto sign = JS::Temporal::sign(days); - - // l. If sign is 0, set sign to 1. - if (sign == 0) - sign = 1; + // k. If days < 0, let sign be −1; else, let sign be 1. + auto sign = days < 0 ? -1 : 1; - // m. Let oneMonth be ? CreateTemporalDuration(0, sign, 0, 0, 0, 0, 0, 0, 0, 0). + // l. Let oneMonth be ? CreateTemporalDuration(0, sign, 0, 0, 0, 0, 0, 0, 0, 0). auto* one_month = TRY(create_temporal_duration(global_object, 0, sign, 0, 0, 0, 0, 0, 0, 0, 0)); - // n. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneMonth). + // m. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneMonth). auto move_result = TRY(move_relative_date(global_object, *calendar, *relative_to, *one_month)); - // o. Set relativeTo to moveResult.[[RelativeTo]]. + // n. Set relativeTo to moveResult.[[RelativeTo]]. relative_to = move_result.relative_to.cell(); - // p. Let oneMonthDays be moveResult.[[Days]]. + // o. Let oneMonthDays be moveResult.[[Days]]. auto one_month_days = move_result.days; - // q. Repeat, while abs(days) ≥ abs(oneMonthDays), + // p. Repeat, while abs(days) ≥ abs(oneMonthDays), while (fabs(days) >= fabs(one_month_days)) { // i. Set months to months + sign. months += sign; @@ -1377,16 +1369,16 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d one_month_days = move_result.days; } - // r. Let fractionalMonths be months + days / abs(oneMonthDays). + // q. Let fractionalMonths be months + days / abs(oneMonthDays). auto fractional_months = months + days / fabs(one_month_days); - // s. Set months to ! RoundNumberToIncrement(fractionalMonths, increment, roundingMode). + // r. Set months to ! RoundNumberToIncrement(fractionalMonths, increment, roundingMode). months = (double)round_number_to_increment(fractional_months, increment, rounding_mode); - // t. Set remainder to fractionalMonths - months. + // s. Set remainder to fractionalMonths - months. remainder = fractional_months - months; - // u. Set weeks and days to 0. + // t. Set weeks and days to 0. weeks = 0; days = 0; } @@ -1394,26 +1386,22 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d else if (unit == "week"sv) { VERIFY(relative_to); - // a. Let sign be ! Sign(days). - auto sign = JS::Temporal::sign(days); + // a. If days < 0, let sign be −1; else, let sign be 1. + auto sign = days < 0 ? -1 : 1; - // b. If sign is 0, set sign to 1. - if (sign == 0) - sign = 1; - - // c. Let oneWeek be ? CreateTemporalDuration(0, 0, sign, 0, 0, 0, 0, 0, 0, 0). + // b. Let oneWeek be ? CreateTemporalDuration(0, 0, sign, 0, 0, 0, 0, 0, 0, 0). auto* one_week = TRY(create_temporal_duration(global_object, 0, 0, sign, 0, 0, 0, 0, 0, 0, 0)); - // d. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneWeek). + // c. Let moveResult be ? MoveRelativeDate(calendar, relativeTo, oneWeek). auto move_result = TRY(move_relative_date(global_object, *calendar, *relative_to, *one_week)); - // e. Set relativeTo to moveResult.[[RelativeTo]]. + // d. Set relativeTo to moveResult.[[RelativeTo]]. relative_to = move_result.relative_to.cell(); - // f. Let oneWeekDays be moveResult.[[Days]]. + // e. Let oneWeekDays be moveResult.[[Days]]. auto one_week_days = move_result.days; - // g. Repeat, while abs(days) ≥ abs(oneWeekDays), + // f. Repeat, while abs(days) ≥ abs(oneWeekDays), while (fabs(days) >= fabs(one_week_days)) { // i. Set weeks to weeks + sign. weeks += sign; @@ -1431,16 +1419,16 @@ ThrowCompletionOr<RoundedDuration> round_duration(GlobalObject& global_object, d one_week_days = move_result.days; } - // h. Let fractionalWeeks be weeks + days / abs(oneWeekDays). + // g. Let fractionalWeeks be weeks + days / abs(oneWeekDays). auto fractional_weeks = weeks + days / fabs(one_week_days); - // i. Set weeks to ! RoundNumberToIncrement(fractionalWeeks, increment, roundingMode). + // h. Set weeks to ! RoundNumberToIncrement(fractionalWeeks, increment, roundingMode). weeks = (double)round_number_to_increment(fractional_weeks, increment, rounding_mode); - // j. Set remainder to fractionalWeeks - weeks. + // i. Set remainder to fractionalWeeks - weeks. remainder = fractional_weeks - weeks; - // k. Set days to 0. + // j. Set days to 0. days = 0; } // 13. Else if unit is "day", then @@ -1569,34 +1557,43 @@ ThrowCompletionOr<DurationRecord> adjust_rounded_duration_days(GlobalObject& glo // 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); + i32 direction; + + // 3. If timeRemainderNs = 0, let direction be 0. + if (time_remainder_ns == "0"_bigint) + direction = 0; + // 4. Else if timeRemainderNs < 0, let direction be −1. + else if (time_remainder_ns.is_negative()) + direction = -1; + // 5. Else, let direction be 1. + else + direction = 1; - // 4. Let dayStart be ? AddZonedDateTime(relativeTo.[[Nanoseconds]], relativeTo.[[TimeZone]], relativeTo.[[Calendar]], years, months, weeks, days, 0, 0, 0, 0, 0, 0). + // 6. 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). + // 7. 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). + // 8. 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()) { + // 9. If (timeRemainderNs − dayLengthNs) × direction < 0, then + if (time_remainder_ns.minus(day_length_ns).multiplied_by(Crypto::SignedBigInteger { direction }).is_negative()) { // a. Return ! CreateDurationRecord(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). return create_duration_record(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } - // 8. Set timeRemainderNs to ! RoundTemporalInstant(ℤ(timeRemainderNs − dayLengthNs), increment, unit, roundingMode). + // 10. 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). + // 11. 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"). + // 12. 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 ! CreateDurationRecord(adjustedDateDuration.[[Years]], adjustedDateDuration.[[Months]], adjustedDateDuration.[[Weeks]], adjustedDateDuration.[[Days]], adjustedTimeDuration.[[Hours]], adjustedTimeDuration.[[Minutes]], adjustedTimeDuration.[[Seconds]], adjustedTimeDuration.[[Milliseconds]], adjustedTimeDuration.[[Microseconds]], adjustedTimeDuration.[[Nanoseconds]]). + // 13. Return ! CreateDurationRecord(adjustedDateDuration.[[Years]], adjustedDateDuration.[[Months]], adjustedDateDuration.[[Weeks]], adjustedDateDuration.[[Days]], adjustedTimeDuration.[[Hours]], adjustedTimeDuration.[[Minutes]], adjustedTimeDuration.[[Seconds]], adjustedTimeDuration.[[Milliseconds]], adjustedTimeDuration.[[Microseconds]], adjustedTimeDuration.[[Nanoseconds]]). return create_duration_record(adjusted_date_duration.years, adjusted_date_duration.months, adjusted_date_duration.weeks, adjusted_date_duration.days, adjusted_time_duration.hours, adjusted_time_duration.minutes, adjusted_time_duration.seconds, adjusted_time_duration.milliseconds, adjusted_time_duration.microseconds, adjusted_time_duration.nanoseconds); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index 77e3a8ce3f..a9cb2fa7d9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -460,18 +460,18 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(GlobalObject& glo // 2. Set nanoseconds to ℝ(nanoseconds). auto nanoseconds = nanoseconds_bigint.big_integer(); - // 3. Let sign be ! ℝ(Sign(𝔽(nanoseconds))). - auto sign = Temporal::sign(nanoseconds); - - // 4. Let dayLengthNs be 8.64 × 10^13. + // 3. Let dayLengthNs be 8.64 × 10^13. auto day_length_ns = "86400000000000"_sbigint; - // 5. If sign is 0, then - if (sign == 0) { + // 4. If nanoseconds = 0, then + if (nanoseconds == "0"_bigint) { // a. Return the Record { [[Days]]: 0, [[Nanoseconds]]: 0, [[DayLength]]: dayLengthNs }. return NanosecondsToDaysResult { .days = 0, .nanoseconds = make_handle(js_bigint(vm, { 0 })), .day_length = day_length_ns.to_double() }; } + // 5. If nanoseconds < 0, let sign be −1; else, let sign be 1. + auto sign = nanoseconds.is_negative() ? -1 : 1; + // 6. If Type(relativeTo) is not Object or relativeTo does not have an [[InitializedTemporalZonedDateTime]] internal slot, then if (!relative_to_value.is_object() || !is<ZonedDateTime>(relative_to_value.as_object())) { // a. Return the Record { [[Days]]: the integral part of nanoseconds / dayLengthNs, [[Nanoseconds]]: (abs(nanoseconds) modulo dayLengthNs) × sign, [[DayLength]]: dayLengthNs }. |