diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index f5b6e6109a..66f88ff03c 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -107,6 +107,7 @@ namespace JS { P(dateFromFields) \ P(day) \ P(dayOfWeek) \ + P(dayOfYear) \ P(days) \ P(debug) \ P(decodeURI) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index d005510162..4c22c72f62 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -416,6 +416,21 @@ u8 to_iso_day_of_week(i32 year, u8 month, u8 day) return result; } +// 12.1.34 ToISODayOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofyear +u16 to_iso_day_of_year(i32 year, u8 month, u8 day) +{ + // 1. Assert: year is an integer. + // 2. Assert: month is an integer. + // 3. Assert: day is an integer. + + // 4. Let date be the date given by year, month, and day. + // 5. Return date's ordinal date in the year according to ISO-8601. + u16 days = day; + for (u8 i = month - 1; i > 0; --i) + days += iso_days_in_month(year, i); + return days; +} + // 12.1.36 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode String build_iso_month_code(i32 month) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index 00cdd78ebd..446737299a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -47,6 +47,7 @@ bool calendar_equals(GlobalObject&, Object& one, Object& two); bool is_iso_leap_year(i32 year); i32 iso_days_in_month(i32 year, i32 month); u8 to_iso_day_of_week(i32 year, u8 month, u8 day); +u16 to_iso_day_of_year(i32 year, u8 month, u8 day); String build_iso_month_code(i32 month); double resolve_iso_month(GlobalObject&, Object& fields); Optional iso_date_from_fields(GlobalObject&, Object& fields, Object& options); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index a898426dc7..67d60a03c3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -36,6 +36,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.monthCode, month_code, 1, attr); define_native_function(vm.names.day, day, 1, attr); define_native_function(vm.names.dayOfWeek, day_of_week, 1, attr); + define_native_function(vm.names.dayOfYear, day_of_year, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -231,6 +232,28 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_week) return Value(to_iso_day_of_week(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); } +// 12.4.14 Temporal.Calendar.prototype.dayOfYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.dayofyear +// NOTE: This is the minimum dayOfYear implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_year) +{ + // 1. Let calendar be the this value. + // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + auto* calendar = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Assert: calendar.[[Identifier]] is "iso8601". + VERIFY(calendar->identifier() == "iso8601"sv); + + // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). + auto* temporal_date = to_temporal_date(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 5. Return 𝔽(! ToISODayOfYear(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])). + return Value(to_iso_day_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); +} + // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index 28a019dde9..0d056478fa 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -26,6 +26,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(month_code); JS_DECLARE_NATIVE_FUNCTION(day); JS_DECLARE_NATIVE_FUNCTION(day_of_week); + JS_DECLARE_NATIVE_FUNCTION(day_of_year); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_json); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dayOfYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dayOfYear.js new file mode 100644 index 0000000000..2ecb761fd5 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dayOfYear.js @@ -0,0 +1,11 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.dayOfYear).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.dayOfYear(date)).toBe(204); + }); +});