1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:47:37 +00:00

LibJS: Add the CompareTemporalTime & CompareISODateTime AOs

These are required for implementing comparisons between `PlainTime`s and
`PlainDateTime`s.
This commit is contained in:
Idan Horowitz 2021-08-27 16:33:15 +03:00 committed by Linus Groh
parent 86b99fd9a6
commit 28fa4d1568
4 changed files with 77 additions and 0 deletions

View file

@ -304,4 +304,22 @@ PlainDateTime* create_temporal_date_time(GlobalObject& global_object, i32 iso_ye
return object;
}
// 5.5.8 CompareISODateTime ( y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1, y2, mon2, d2, h2, min2, s2, ms2, mus2, ns2 ),https://tc39.es/proposal-temporal/#sec-temporal-compareisodatetime
i8 compare_iso_date_time(i32 year1, u8 month1, u8 day1, u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, i32 year2, u8 month2, u8 day2, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2)
{
// 1. Assert: y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1, y2, mon2, d2, h2, min2, s2, ms2, mus2, and ns2 are integers.
// 2. Let dateResult be ! CompareISODate(y1, mon1, d1, y2, mon2, d2).
auto date_result = compare_iso_date(year1, month1, day1, year2, month2, day2);
// 3. If dateResult is not 0, then
if (date_result != 0) {
// a. Return dateResult.
return date_result;
}
// 4. Return ! CompareTemporalTime(h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2).
return compare_temporal_time(hour1, minute1, second1, millisecond1, microsecond1, nanosecond1, hour2, minute2, second2, millisecond2, microsecond2, nanosecond2);
}
}

View file

@ -54,5 +54,6 @@ Optional<ISODateTime> interpret_temporal_date_time_fields(GlobalObject&, Object&
PlainDateTime* to_temporal_date_time(GlobalObject&, Value item, Object* options = nullptr);
ISODateTime balance_iso_date_time(i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, i64 nanosecond);
PlainDateTime* create_temporal_date_time(GlobalObject&, i32 iso_year, u8 iso_month, u8 iso_day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Object& calendar, FunctionObject* new_target = nullptr);
i8 compare_iso_date_time(i32 year1, u8 month1, u8 day1, u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, i32 year2, u8 month2, u8 day2, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2);
}

View file

@ -264,4 +264,61 @@ Optional<TemporalTime> to_temporal_time_record(GlobalObject& global_object, Obje
return result;
}
// 4.5.11 CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetemporaltime
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)
{
// 1. Assert: h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, and ns2 are integers.
// 2. If h1 > h2, return 1.
if (hour1 > hour2)
return 1;
// 3. If h1 < h2, return -1.
if (hour1 < hour2)
return -1;
// 4. If min1 > min2, return 1.
if (minute1 > minute2)
return 1;
// 5. If min1 < min2, return -1.
if (minute1 < minute2)
return -1;
// 6. If s1 > s2, return 1.
if (second1 > second2)
return 1;
// 7. If s1 < s2, return -1.
if (second1 < second2)
return -1;
// 8. If ms1 > ms2, return 1.
if (millisecond1 > millisecond2)
return 1;
// 9. If ms1 < ms2, return -1.
if (millisecond1 < millisecond2)
return -1;
// 10. If mus1 > mus2, return 1.
if (microsecond1 > microsecond2)
return 1;
// 11. If mus1 < mus2, return -1.
if (microsecond1 < microsecond2)
return -1;
// 12. If ns1 > ns2, return 1.
if (nanosecond1 > nanosecond2)
return 1;
// 13. If ns1 < ns2, return -1.
if (nanosecond1 < nanosecond2)
return -1;
// 14. Return 0.
return 0;
}
}

View file

@ -87,5 +87,6 @@ DaysAndTime balance_time(i64 hour, i64 minute, i64 second, i64 millisecond, i64
TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond);
PlainTime* create_temporal_time(GlobalObject&, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject* new_target = nullptr);
Optional<TemporalTime> to_temporal_time_record(GlobalObject&, Object& temporal_time_like);
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);
}