mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Use round_number_to_increment(double) in round_time()
Much nicer :^)
This commit is contained in:
parent
8b07453bce
commit
ef004c6b98
Notes:
sideshowbarker
2024-07-18 02:57:20 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/ef004c6b984 Pull-request: https://github.com/SerenityOS/serenity/pull/10385 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/alimpfard
3 changed files with 11 additions and 16 deletions
|
@ -457,7 +457,7 @@ i8 compare_temporal_time(u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16
|
|||
}
|
||||
|
||||
// 4.5.13 RoundTime ( hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode [ , dayLengthNs ] ), https://tc39.es/proposal-temporal/#sec-temporal-roundtime
|
||||
DaysAndTime round_time(GlobalObject& global_object, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns)
|
||||
DaysAndTime round_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns)
|
||||
{
|
||||
// 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, and increment are integers.
|
||||
|
||||
|
@ -508,55 +508,50 @@ DaysAndTime round_time(GlobalObject& global_object, u8 hour, u8 minute, u8 secon
|
|||
quantity = nanosecond;
|
||||
}
|
||||
|
||||
// FIXME: This doesn't seem right...
|
||||
auto* quantity_bigint = js_bigint(global_object.vm(), Crypto::SignedBigInteger::create_from((u64)quantity));
|
||||
|
||||
// 10. Let result be ! RoundNumberToIncrement(quantity, increment, roundingMode).
|
||||
auto* result = round_number_to_increment(global_object, *quantity_bigint, increment, rounding_mode);
|
||||
|
||||
auto result_i64 = (i64)result->big_integer().to_double();
|
||||
auto result = round_number_to_increment(quantity, increment, rounding_mode);
|
||||
|
||||
// If unit is "day", then
|
||||
if (unit == "day"sv) {
|
||||
// a. Return the Record { [[Days]]: result, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
|
||||
return DaysAndTime { .days = (i32)result_i64, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
|
||||
return DaysAndTime { .days = (i32)result, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
|
||||
}
|
||||
|
||||
// 12. If unit is "hour", then
|
||||
if (unit == "hour"sv) {
|
||||
// a. Return ! BalanceTime(result, 0, 0, 0, 0, 0).
|
||||
return balance_time(result_i64, 0, 0, 0, 0, 0);
|
||||
return balance_time(result, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
// 13. If unit is "minute", then
|
||||
if (unit == "minute"sv) {
|
||||
// a. Return ! BalanceTime(hour, result, 0, 0, 0, 0).
|
||||
return balance_time(hour, result_i64, 0, 0, 0, 0);
|
||||
return balance_time(hour, result, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
// 14. If unit is "second", then
|
||||
if (unit == "second"sv) {
|
||||
// a. Return ! BalanceTime(hour, minute, result, 0, 0, 0).
|
||||
return balance_time(hour, minute, result_i64, 0, 0, 0);
|
||||
return balance_time(hour, minute, result, 0, 0, 0);
|
||||
}
|
||||
|
||||
// 15. If unit is "millisecond", then
|
||||
if (unit == "millisecond"sv) {
|
||||
// a. Return ! BalanceTime(hour, minute, second, result, 0, 0).
|
||||
return balance_time(hour, minute, second, result_i64, 0, 0);
|
||||
return balance_time(hour, minute, second, result, 0, 0);
|
||||
}
|
||||
|
||||
// 16. If unit is "microsecond", then
|
||||
if (unit == "microsecond"sv) {
|
||||
// a. Return ! BalanceTime(hour, minute, second, millisecond, result, 0).
|
||||
return balance_time(hour, minute, second, millisecond, result_i64, 0);
|
||||
return balance_time(hour, minute, second, millisecond, result, 0);
|
||||
}
|
||||
|
||||
// 17. Assert: unit is "nanosecond".
|
||||
VERIFY(unit == "nanosecond"sv);
|
||||
|
||||
// 18. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, result).
|
||||
return balance_time(hour, minute, second, millisecond, microsecond, result_i64);
|
||||
return balance_time(hour, minute, second, millisecond, microsecond, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -102,6 +102,6 @@ ThrowCompletionOr<PlainTime*> create_temporal_time(GlobalObject&, u8 hour, u8 mi
|
|||
ThrowCompletionOr<UnregulatedTemporalTime> to_temporal_time_record(GlobalObject&, Object const& temporal_time_like);
|
||||
String temporal_time_to_string(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
|
||||
i8 compare_temporal_time(u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2);
|
||||
DaysAndTime round_time(GlobalObject&, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns = {});
|
||||
DaysAndTime round_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns = {});
|
||||
|
||||
}
|
||||
|
|
|
@ -346,7 +346,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
|
|||
auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
||||
// 6. Let roundResult be ! RoundTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], precision.[[Increment]], precision.[[Unit]], roundingMode).
|
||||
auto round_result = round_time(global_object, temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision.increment, precision.unit, rounding_mode);
|
||||
auto round_result = round_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision.increment, precision.unit, rounding_mode);
|
||||
|
||||
// 7. Return ! TemporalTimeToString(roundResult.[[Hour]], roundResult.[[Minute]], roundResult.[[Second]], roundResult.[[Millisecond]], roundResult.[[Microsecond]], roundResult.[[Nanosecond]], precision.[[Precision]]).
|
||||
auto string = temporal_time_to_string(round_result.hour, round_result.minute, round_result.second, round_result.millisecond, round_result.microsecond, round_result.nanosecond, precision.precision);
|
||||
|
|
Loading…
Add table
Reference in a new issue