diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-27 00:21:16 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-27 19:51:44 +0100 |
commit | c303bbde5430c8d889853ae112d50275b57f74c5 (patch) | |
tree | 384724cadb81da7e3c237404b3d073cd4e6e1ddd /Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp | |
parent | 5512ff79f0836912f19499bf5dc59c49cbb48482 (diff) | |
download | serenity-c303bbde5430c8d889853ae112d50275b57f74c5.zip |
LibJS: Implement Temporal.Now.plainDate()
...and ten required AOs we didn't have yet:
- BalanceISODate
- BalanceISODateTime
- BalanceISOYearMonth
- BalanceTime
- BuiltinTimeZoneGetPlainDateTimeFor
- GetISOPartsFromEpoch
- GetOffsetNanosecondsFor
- ParseTemporalTimeZone
- SystemDateTime
- ToTemporalTimeZone
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
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 <LibCrypto/BigInt/SignedBigInteger.h> #include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Temporal/Calendar.h> #include <LibJS/Runtime/Temporal/Instant.h> #include <LibJS/Runtime/Temporal/Now.h> +#include <LibJS/Runtime/Temporal/PlainDate.h> +#include <LibJS/Runtime/Temporal/PlainDateTime.h> #include <LibJS/Runtime/Temporal/TimeZone.h> #include <time.h> @@ -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); +} + } |