LibJS: Begin implementing the relativeTo option of Duration.round

This commit is contained in:
Timothy Flynn 2024-11-22 18:16:57 -05:00 committed by Andreas Kling
commit 70ad66d3c0
Notes: github-actions[bot] 2024-11-23 13:47:01 +00:00
6 changed files with 219 additions and 9 deletions

View file

@ -8,6 +8,8 @@
#include <AK/Assertions.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/PlainTime.h>
#include <math.h>
@ -46,6 +48,37 @@ Time noon_time_record()
return { .days = 0, .hour = 12, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
}
// 4.5.5 DifferenceTime ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime
TimeDuration difference_time(Time const& time1, Time const& time2)
{
// 1. Let hours be time2.[[Hour]] - time1.[[Hour]].
auto hours = static_cast<double>(time2.hour) - static_cast<double>(time1.hour);
// 2. Let minutes be time2.[[Minute]] - time1.[[Minute]].
auto minutes = static_cast<double>(time2.minute) - static_cast<double>(time1.minute);
// 3. Let seconds be time2.[[Second]] - time1.[[Second]].
auto seconds = static_cast<double>(time2.second) - static_cast<double>(time1.second);
// 4. Let milliseconds be time2.[[Millisecond]] - time1.[[Millisecond]].
auto milliseconds = static_cast<double>(time2.millisecond) - static_cast<double>(time1.millisecond);
// 5. Let microseconds be time2.[[Microsecond]] - time1.[[Microsecond]].
auto microseconds = static_cast<double>(time2.microsecond) - static_cast<double>(time1.microsecond);
// 6. Let nanoseconds be time2.[[Nanosecond]] - time1.[[Nanosecond]].
auto nanoseconds = static_cast<double>(time2.nanosecond) - static_cast<double>(time1.nanosecond);
// 7. Let timeDuration be TimeDurationFromComponents(hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
auto time_duration = time_duration_from_components(hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
// 8. Assert: abs(timeDuration) < nsPerDay.
VERIFY(time_duration.unsigned_value() < NANOSECONDS_PER_DAY);
// 9. Return timeDuration.
return time_duration;
}
// 4.5.8 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
ThrowCompletionOr<Time> regulate_time(VM& vm, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, Overflow overflow)
{
@ -172,4 +205,62 @@ Time balance_time(double hour, double minute, double second, double millisecond,
return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond, delta_days);
}
// 4.5.14 CompareTimeRecord ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetimerecord
i8 compare_time_record(Time const& time1, Time const& time2)
{
// 1. If time1.[[Hour]] > time2.[[Hour]], return 1.
if (time1.hour > time2.hour)
return 1;
// 2. If time1.[[Hour]] < time2.[[Hour]], return -1.
if (time1.hour < time2.hour)
return -1;
// 3. If time1.[[Minute]] > time2.[[Minute]], return 1.
if (time1.minute > time2.minute)
return 1;
// 4. If time1.[[Minute]] < time2.[[Minute]], return -1.
if (time1.minute < time2.minute)
return -1;
// 5. If time1.[[Second]] > time2.[[Second]], return 1.
if (time1.second > time2.second)
return 1;
// 6. If time1.[[Second]] < time2.[[Second]], return -1.
if (time1.second < time2.second)
return -1;
// 7. If time1.[[Millisecond]] > time2.[[Millisecond]], return 1.
if (time1.millisecond > time2.millisecond)
return 1;
// 8. If time1.[[Millisecond]] < time2.[[Millisecond]], return -1.
if (time1.millisecond < time2.millisecond)
return -1;
// 9. If time1.[[Microsecond]] > time2.[[Microsecond]], return 1.
if (time1.microsecond > time2.microsecond)
return 1;
// 10. If time1.[[Microsecond]] < time2.[[Microsecond]], return -1.
if (time1.microsecond < time2.microsecond)
return -1;
// 11. If time1.[[Nanosecond]] > time2.[[Nanosecond]], return 1.
if (time1.nanosecond > time2.nanosecond)
return 1;
// 12. If time1.[[Nanosecond]] < time2.[[Nanosecond]], return -1.
if (time1.nanosecond < time2.nanosecond)
return -1;
// 13. Return 0.
return 0;
}
// 4.5.15 AddTime ( time, timeDuration ), https://tc39.es/proposal-temporal/#sec-temporal-addtime
Time add_time(Time const& time, TimeDuration const& time_duration)
{
auto nanoseconds = time_duration.plus(TimeDuration { static_cast<i64>(time.nanosecond) });
// 1. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], time.[[Millisecond]], time.[[Microsecond]], time.[[Nanosecond]] + timeDuration).
return balance_time(time.hour, time.minute, time.second, time.millisecond, time.microsecond, nanoseconds.to_double());
}
}