diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 4eeced82dab..7f9146161e5 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -43,6 +43,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable); 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); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); @@ -298,6 +299,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::days_in_month_getter) return calendar_days_in_month(global_object, calendar, *date_time); } +// 5.3.19 get Temporal.PlainDateTime.prototype.daysInYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.daysinyear +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::days_in_year_getter) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be dateTime.[[Calendar]]. + auto& calendar = date_time->calendar(); + + // 4. Return ? CalendarDaysInYear(calendar, dateTime). + return calendar_days_in_year(global_object, calendar, *date_time); +} + // 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 84f980e9b43..ae37f7db2e6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -35,6 +35,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(week_of_year_getter); 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(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.daysInYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.daysInYear.js new file mode 100644 index 00000000000..f9bc9371e75 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.daysInYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 30); + expect(plainDateTime.daysInYear).toBe(365); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainDateTime.prototype, "daysInYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime"); + }); +});