From d22fe25643d3263caee7c3c356922644a07556ce Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 23 Jul 2021 18:19:45 +0300 Subject: [PATCH] LibJS: Implement Temporal.Calendar.prototype.weekOfYear --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../LibJS/Runtime/Temporal/Calendar.cpp | 43 +++++++++++++++++++ .../LibJS/Runtime/Temporal/Calendar.h | 2 + .../Runtime/Temporal/CalendarPrototype.cpp | 23 ++++++++++ .../Runtime/Temporal/CalendarPrototype.h | 1 + .../Calendar/Calendar.prototype.weekOfYear.js | 11 +++++ 6 files changed, 81 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.weekOfYear.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 66f88ff03c..d72dab501a 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -382,6 +382,7 @@ namespace JS { P(valueOf) \ P(values) \ P(warn) \ + P(weekOfYear) \ P(weeks) \ P(with) \ P(withCalendar) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 33e6558fd5..fc25620a35 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -382,6 +382,21 @@ bool is_iso_leap_year(i32 year) return true; } +// 12.1.31 ISODaysInYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinyear +u16 iso_days_in_year(i32 year) +{ + // 1. Assert: year is an integer. + + // 2. If ! IsISOLeapYear(year) is true, then + if (is_iso_leap_year(year)) { + // a. Return 366. + return 366; + } + + // 3. Return 365. + return 365; +} + // 12.1.32 ISODaysInMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinmonth i32 iso_days_in_month(i32 year, i32 month) { @@ -441,6 +456,34 @@ u16 to_iso_day_of_year(i32 year, u8 month, u8 day) return days; } +// 12.1.35 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear +u8 to_iso_week_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 week number according to ISO-8601. + auto day_of_year = to_iso_day_of_year(year, month, day); + auto day_of_week = to_iso_day_of_week(year, month, day); + auto week = (day_of_year - day_of_week + 10) / 7; + + if (week < 1) { + auto day_of_jump = to_iso_day_of_week(year, 1, 1); + if (day_of_jump == 5 || (is_iso_leap_year(year) && day_of_jump == 6)) + return 53; + else + return 52; + } else if (week == 53) { + auto days_in_year = iso_days_in_year(year); + if (days_in_year - day_of_year < 4 - day_of_week) + return 1; + } + + return week; +} + // 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 cf08466952..6d13bf6d6e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -46,9 +46,11 @@ Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&); PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options); bool calendar_equals(GlobalObject&, Object& one, Object& two); bool is_iso_leap_year(i32 year); +u16 iso_days_in_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); +u8 to_iso_week_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 67d60a03c3..bf98086b8d 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -37,6 +37,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) 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.weekOfYear, week_of_year, 1, attr); define_native_function(vm.names.toString, to_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); } @@ -254,6 +255,28 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_year) return Value(to_iso_day_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); } +// 12.4.15 Temporal.Calendar.prototype.weekOfYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.weekofyear +// NOTE: This is the minimum weekOfYear implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::week_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_week_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 0d056478fa..491acf4fe3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -27,6 +27,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(day); JS_DECLARE_NATIVE_FUNCTION(day_of_week); JS_DECLARE_NATIVE_FUNCTION(day_of_year); + JS_DECLARE_NATIVE_FUNCTION(week_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.weekOfYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.weekOfYear.js new file mode 100644 index 0000000000..f0e8560746 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.weekOfYear.js @@ -0,0 +1,11 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.Calendar.prototype.weekOfYear).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const date = new Temporal.PlainDate(2021, 7, 23); + expect(calendar.weekOfYear(date)).toBe(29); + }); +});