From 0580f102e4e09364edbddfd6c0e03d46e6d158bc Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 4 Aug 2021 22:51:39 +0100 Subject: [PATCH] LibJS: Implement Temporal.ZonedDateTime.prototype.day --- .../Temporal/ZonedDateTimePrototype.cpp | 28 +++++++++++++++++++ .../Runtime/Temporal/ZonedDateTimePrototype.h | 1 + .../ZonedDateTime.prototype.day.js | 15 ++++++++++ 3 files changed, 44 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.day.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 49372020e8f..3b91dc6b1fe 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -35,6 +35,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.year, year_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.month, month_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable); } static ZonedDateTime* typed_this(GlobalObject& global_object) @@ -157,4 +158,31 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::month_code_getter) return js_string(vm, calendar_month_code(global_object, calendar, *temporal_date_time)); } +// 6.3.8 get Temporal.ZonedDateTime.prototype.day, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.day +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::day_getter) +{ + // 1. Let zonedDateTime be the this value. + // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]). + auto* zoned_date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let timeZone be zonedDateTime.[[TimeZone]]. + auto& time_zone = zoned_date_time->time_zone(); + + // 4. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]). + auto* instant = create_temporal_instant(global_object, zoned_date_time->nanoseconds()); + + // 5. Let calendar be zonedDateTime.[[Calendar]]. + auto& calendar = zoned_date_time->calendar(); + + // 6. Let temporalDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar). + auto* temporal_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar); + if (vm.exception()) + return {}; + + // 7. Return ? CalendarDay(calendar, temporalDateTime). + return Value(calendar_day(global_object, calendar, *temporal_date_time)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h index 190b0e5f337..3c82660d010 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -24,6 +24,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(year_getter); JS_DECLARE_NATIVE_FUNCTION(month_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); + JS_DECLARE_NATIVE_FUNCTION(day_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.day.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.day.js new file mode 100644 index 00000000000..baa0150d8d6 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.day.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const timeZone = new Temporal.TimeZone("UTC"); + const zonedDateTime = new Temporal.ZonedDateTime(1625614921000000000n, timeZone); + expect(zonedDateTime.day).toBe(6); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Reflect.get(Temporal.ZonedDateTime.prototype, "day", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime"); + }); +});