mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:17:35 +00:00
LibJS: Implement Temporal.Calendar.prototype.weekOfYear
This commit is contained in:
parent
d561535ac9
commit
d22fe25643
6 changed files with 81 additions and 0 deletions
|
@ -382,6 +382,7 @@ namespace JS {
|
|||
P(valueOf) \
|
||||
P(values) \
|
||||
P(warn) \
|
||||
P(weekOfYear) \
|
||||
P(weeks) \
|
||||
P(with) \
|
||||
P(withCalendar) \
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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<TemporalDate> iso_date_from_fields(GlobalObject&, Object& fields, Object& options);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue