summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-08-27 16:33:15 +0300
committerLinus Groh <mail@linusgroh.de>2021-08-27 16:40:16 +0100
commit28fa4d15689a158a6b2915f43546ba56c5cac714 (patch)
treeaf4b050b748d0e9c147f5a5efcfc0ceae194e0c9 /Userland/Libraries
parent86b99fd9a6ccbf2dbb98aabd5f2ba97f6f843596 (diff)
downloadserenity-28fa4d15689a158a6b2915f43546ba56c5cac714.zip
LibJS: Add the CompareTemporalTime & CompareISODateTime AOs
These are required for implementing comparisons between `PlainTime`s and `PlainDateTime`s.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp18
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp57
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.h1
4 files changed, 77 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
index 2d81fb5acc..a07c7dc77f 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp
@@ -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);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h
index 0467be387b..4e66d72696 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.h
@@ -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);
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp
index e30e4056cd..d4344300de 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.cpp
@@ -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;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.h
index 03b7964e69..0728dd8bad 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTime.h
@@ -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);
}