diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-04 23:14:30 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-05 19:19:40 +0200 |
commit | 5a7db1b5f4e252c90ad181beea44426a3a0cc5f7 (patch) | |
tree | eb238033c3e0e417a20eea7c8af6d24a72b6430e /Userland/Libraries | |
parent | 204ba01416d0925c322c393d14abf61278fcb6e7 (diff) | |
download | serenity-5a7db1b5f4e252c90ad181beea44426a3a0cc5f7.zip |
LibJS: Implement Temporal.ZonedDateTime.prototype.monthsInYear
Diffstat (limited to 'Userland/Libraries')
3 files changed, 44 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 0a31900b45..b0f8df0505 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -52,6 +52,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.daysInWeek, days_in_week_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInMonth, days_in_month_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.daysInYear, days_in_year_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.monthsInYear, months_in_year_getter, {}, Attribute::Configurable); } static ZonedDateTime* typed_this(GlobalObject& global_object) @@ -595,4 +596,31 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::days_in_year_getter) return calendar_days_in_year(global_object, calendar, *temporal_date_time); } +// 6.3.26 get Temporal.ZonedDateTime.prototype.monthsInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.monthsinyear +JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::months_in_year_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 ? CalendarMonthsInYear(calendar, temporalDateTime). + return calendar_months_in_year(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 a646238c8e..f6663695eb 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h @@ -41,6 +41,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(days_in_week_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter); JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter); + JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.monthsInYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.monthsInYear.js new file mode 100644 index 0000000000..e40dad1ad4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.monthsInYear.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.monthsInYear).toBe(12); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.ZonedDateTime object", () => { + expect(() => { + Reflect.get(Temporal.ZonedDateTime.prototype, "monthsInYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.ZonedDateTime"); + }); +}); |