diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-27 00:30:10 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-27 19:51:44 +0100 |
commit | f2a2e8e13cd7f88f66df4d4c420fc449911b027d (patch) | |
tree | ac3029f86cc1fc2a72d9d6536a8196a1be341717 /Userland/Libraries | |
parent | c303bbde5430c8d889853ae112d50275b57f74c5 (diff) | |
download | serenity-f2a2e8e13cd7f88f66df4d4c420fc449911b027d.zip |
LibJS: Implement Temporal.Now.plainDateISO()
Diffstat (limited to 'Userland/Libraries')
4 files changed, 47 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index a6431cbf78..cea59b8771 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -283,6 +283,7 @@ namespace JS { P(parseFloat) \ P(parseInt) \ P(plainDate) \ + P(plainDateISO) \ P(pop) \ P(pow) \ P(preventExtensions) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp index f33e1c3529..5f702ad883 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -35,6 +35,7 @@ void Now::initialize(GlobalObject& global_object) 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); + define_native_function(vm.names.plainDateISO, plain_date_iso, 0, attr); } // 2.1.1 Temporal.Now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone @@ -66,6 +67,25 @@ JS_DEFINE_NATIVE_FUNCTION(Now::plain_date) return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); } +// 2.1.8 Temporal.Now.plainDateISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindateiso +JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_iso) +{ + auto temporal_time_zone_like = vm.argument(0); + + // 1. Let calendar be ? GetISO8601Calendar(). + auto* calendar = get_iso8601_calendar(global_object); + if (vm.exception()) + return {}; + + // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar). + auto* date_time = system_date_time(global_object, temporal_time_zone_like, calendar); + if (vm.exception()) + return {}; + + // 3. 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) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h index e22a44ee16..7d85c612bb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h @@ -22,6 +22,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(time_zone); JS_DECLARE_NATIVE_FUNCTION(instant); JS_DECLARE_NATIVE_FUNCTION(plain_date); + JS_DECLARE_NATIVE_FUNCTION(plain_date_iso); }; TimeZone* system_time_zone(GlobalObject&); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js new file mode 100644 index 0000000000..1463866f2d --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateISO.js @@ -0,0 +1,25 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.Now.plainDateISO).toHaveLength(0); + }); + + test("basic functionality", () => { + const plainDate = Temporal.Now.plainDateISO(); + expect(plainDate).toBeInstanceOf(Temporal.PlainDate); + expect(plainDate.calendar.id).toBe("iso8601"); + }); + + test("custom time zone", () => { + const timeZone = { + getOffsetNanosecondsFor() { + return 86400000000000; + }, + }; + const plainDate = Temporal.Now.plainDateISO(); + const plainDateWithOffset = Temporal.Now.plainDateISO(timeZone); + // Yes, this will fail if a day, month, or year change happens between the above two lines :^) + expect(plainDateWithOffset.year).toBe(plainDate.year); + expect(plainDateWithOffset.month).toBe(plainDate.month); + expect(plainDateWithOffset.day).toBe(plainDate.day + 1); + }); +}); |