From c303bbde5430c8d889853ae112d50275b57f74c5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 27 Jul 2021 00:21:16 +0100 Subject: LibJS: Implement Temporal.Now.plainDate() ...and ten required AOs we didn't have yet: - BalanceISODate - BalanceISODateTime - BalanceISOYearMonth - BalanceTime - BuiltinTimeZoneGetPlainDateTimeFor - GetISOPartsFromEpoch - GetOffsetNanosecondsFor - ParseTemporalTimeZone - SystemDateTime - ToTemporalTimeZone --- Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp') diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp index aa2073edaf..f33e1c3529 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -6,8 +6,11 @@ #include #include +#include #include #include +#include +#include #include #include @@ -31,6 +34,7 @@ void Now::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.timeZone, time_zone, 0, attr); define_native_function(vm.names.instant, instant, 0, attr); + define_native_function(vm.names.plainDate, plain_date, 1, attr); } // 2.1.1 Temporal.Now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone @@ -47,6 +51,21 @@ JS_DEFINE_NATIVE_FUNCTION(Now::instant) return system_instant(global_object); } +// 2.1.7 Temporal.Now.plainDate ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindate +JS_DEFINE_NATIVE_FUNCTION(Now::plain_date) +{ + auto calendar = vm.argument(0); + auto temporal_time_zone_like = vm.argument(1); + + // 1. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar). + auto* date_time = system_date_time(global_object, temporal_time_zone_like, calendar); + if (vm.exception()) + return {}; + + // 2. Return ? CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]). + return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); +} + // 2.2.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone TimeZone* system_time_zone(GlobalObject& global_object) { @@ -90,4 +109,33 @@ Instant* system_instant(GlobalObject& global_object) return create_temporal_instant(global_object, *ns); } +// 2.2.4 SystemDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemdatetime +PlainDateTime* system_date_time(GlobalObject& global_object, Value temporal_time_zone_like, Value calendar_like) +{ + auto& vm = global_object.vm(); + Object* time_zone; + + // 1. If temporalTimeZoneLike is undefined, then + if (temporal_time_zone_like.is_undefined()) { + // a. Let timeZone be ! SystemTimeZone(). + time_zone = system_time_zone(global_object); + } else { + // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike). + time_zone = to_temporal_time_zone(global_object, temporal_time_zone_like); + if (vm.exception()) + return {}; + } + + // 3. Let calendar be ? ToTemporalCalendar(calendarLike). + auto* calendar = to_temporal_calendar(global_object, calendar_like); + if (vm.exception()) + return {}; + + // 4. Let instant be ! SystemInstant(). + auto* instant = system_instant(global_object); + + // 5. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar). + return builtin_time_zone_get_plain_date_time_for(global_object, *time_zone, *instant, *calendar); +} + } -- cgit v1.2.3