diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-27 00:49:52 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-27 19:51:44 +0100 |
commit | 0bb19fc51cc27e69befae3988e7792c728788a3e (patch) | |
tree | 1359623cb39e835719c701ad3311514cc0432ddf /Userland/Libraries/LibJS | |
parent | f2a2e8e13cd7f88f66df4d4c420fc449911b027d (diff) | |
download | serenity-0bb19fc51cc27e69befae3988e7792c728788a3e.zip |
LibJS: Implement Temporal.Now.plainDateTime()
Diffstat (limited to 'Userland/Libraries/LibJS')
4 files changed, 46 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index cea59b8771..1c5bcfb38f 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -284,6 +284,7 @@ namespace JS { P(parseInt) \ P(plainDate) \ P(plainDateISO) \ + P(plainDateTime) \ 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 5f702ad883..bcad2df62c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -34,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.plainDateTime, plain_date_time, 1, attr); define_native_function(vm.names.plainDate, plain_date, 1, attr); define_native_function(vm.names.plainDateISO, plain_date_iso, 0, attr); } @@ -52,6 +53,16 @@ JS_DEFINE_NATIVE_FUNCTION(Now::instant) return system_instant(global_object); } +// 2.1.3 Temporal.Now.plainDateTime ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetime +JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time) +{ + auto calendar = vm.argument(0); + auto temporal_time_zone_like = vm.argument(1); + + // 1. Return ? SystemDateTime(temporalTimeZoneLike, calendar). + return system_date_time(global_object, temporal_time_zone_like, calendar); +} + // 2.1.7 Temporal.Now.plainDate ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindate JS_DEFINE_NATIVE_FUNCTION(Now::plain_date) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h index 7d85c612bb..587561dc47 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.h @@ -21,6 +21,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(time_zone); JS_DECLARE_NATIVE_FUNCTION(instant); + JS_DECLARE_NATIVE_FUNCTION(plain_date_time); JS_DECLARE_NATIVE_FUNCTION(plain_date); JS_DECLARE_NATIVE_FUNCTION(plain_date_iso); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js new file mode 100644 index 0000000000..cab863834c --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Now/Now.plainDateTime.js @@ -0,0 +1,33 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Now.plainDateTime).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainDateTime = Temporal.Now.plainDateTime(calendar); + expect(plainDateTime).toBeInstanceOf(Temporal.PlainDateTime); + expect(plainDateTime.calendar).toBe(calendar); + }); + + test("custom time zone", () => { + const calendar = new Temporal.Calendar("iso8601"); + const timeZone = { + getOffsetNanosecondsFor() { + return 86400000000000; + }, + }; + const plainDateTime = Temporal.Now.plainDateTime(calendar); + const plainDateTimeWithOffset = Temporal.Now.plainDateTime(calendar, timeZone); + // Yes, this will fail if a day, month, or year change happens between the above two lines :^) + // FIXME: enable these once the getters are implemented + // expect(plainDateTimeWithOffset.year).toBe(plainDateTime.year); + // expect(plainDateTimeWithOffset.month).toBe(plainDateTime.month); + // expect(plainDateTimeWithOffset.day).toBe(plainDateTime.day + 1); + // expect(plainDateTimeWithOffset.hour).not.toBe(plainDateTime.hour); + // expect(plainDateTimeWithOffset.minute).not.toBe(plainDateTime.minute); + // expect(plainDateTimeWithOffset.second).not.toBe(plainDateTime.second); + // expect(plainDateTimeWithOffset.millisecond).not.toBe(plainDateTime.millisecond); + // microsecond, and nanosecond not checked here as they could easily be the same for both + }); +}); |