1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

LibJS: Remove ToISODayOfYear

This is an editorial change in the Temporal spec.

See: 6117d90
This commit is contained in:
Linus Groh 2022-05-07 00:13:14 +02:00
parent a216c0b6df
commit cabcdd838b
3 changed files with 20 additions and 30 deletions

View file

@ -637,22 +637,7 @@ u8 to_iso_day_of_week(i32 year, u8 month, u8 day)
return day_of_week == 0 ? 7 : day_of_week; return day_of_week == 0 ? 7 : day_of_week;
} }
// 12.2.31 ToISODayOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofyear // 12.2.31 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear
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 as an integer.
u16 days = day;
for (u8 i = month - 1; i > 0; --i)
days += iso_days_in_month(year, i);
return days;
}
// 12.2.32 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear
u8 to_iso_week_of_year(i32 year, u8 month, u8 day) u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
{ {
// 1. Assert: year is an integer. // 1. Assert: year is an integer.
@ -661,7 +646,7 @@ u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
// 4. Let date be the date given by year, month, and day. // 4. Let date be the date given by year, month, and day.
// 5. Return date's week number according to ISO-8601 as an integer. // 5. Return date's week number according to ISO-8601 as an integer.
auto day_of_year = to_iso_day_of_year(year, month, day); auto day_of_year = day_within_year(make_date(make_day(year, month - 1, day), 0)) + 1;
auto day_of_week = to_iso_day_of_week(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; auto week = (day_of_year - day_of_week + 10) / 7;
@ -684,7 +669,7 @@ u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
return week; return week;
} }
// 12.2.33 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode // 12.2.32 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
String build_iso_month_code(u8 month) String build_iso_month_code(u8 month)
{ {
// 1. Let numberPart be ToZeroPaddedDecimalString(month, 2). // 1. Let numberPart be ToZeroPaddedDecimalString(month, 2).
@ -692,7 +677,7 @@ String build_iso_month_code(u8 month)
return String::formatted("M{:02}", month); return String::formatted("M{:02}", month);
} }
// 12.2.34 ResolveISOMonth ( fields ), https://tc39.es/proposal-temporal/#sec-temporal-resolveisomonth // 12.2.33 ResolveISOMonth ( fields ), https://tc39.es/proposal-temporal/#sec-temporal-resolveisomonth
ThrowCompletionOr<double> resolve_iso_month(GlobalObject& global_object, Object const& fields) ThrowCompletionOr<double> resolve_iso_month(GlobalObject& global_object, Object const& fields)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
@ -752,7 +737,7 @@ ThrowCompletionOr<double> resolve_iso_month(GlobalObject& global_object, Object
return number_part_integer; return number_part_integer;
} }
// 12.2.35 ISODateFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isodatefromfields // 12.2.34 ISODateFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isodatefromfields
ThrowCompletionOr<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const& fields, Object const& options) ThrowCompletionOr<ISODate> iso_date_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
@ -786,7 +771,7 @@ ThrowCompletionOr<ISODate> iso_date_from_fields(GlobalObject& global_object, Obj
return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow); return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), overflow);
} }
// 12.2.36 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields // 12.2.35 ISOYearMonthFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isoyearmonthfromfields
ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, Object const& fields, Object const& options) ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
@ -816,7 +801,7 @@ ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(GlobalObject& global_
return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 }; return ISOYearMonth { .year = result.year, .month = result.month, .reference_iso_day = 1 };
} }
// 12.2.37 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields // 12.2.36 ISOMonthDayFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthdayfromfields
ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Object const& fields, Object const& options) ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_object, Object const& fields, Object const& options)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
@ -874,7 +859,7 @@ ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(GlobalObject& global_ob
return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year }; return ISOMonthDay { .month = result->month, .day = result->day, .reference_iso_year = reference_iso_year };
} }
// 12.2.38 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear // 12.2.37 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
i32 iso_year(Object& temporal_object) i32 iso_year(Object& temporal_object)
{ {
// 1. Assert: temporalObject has an [[ISOYear]] internal slot. // 1. Assert: temporalObject has an [[ISOYear]] internal slot.
@ -892,7 +877,7 @@ i32 iso_year(Object& temporal_object)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// 12.2.39 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth // 12.2.38 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth
u8 iso_month(Object& temporal_object) u8 iso_month(Object& temporal_object)
{ {
// 1. Assert: temporalObject has an [[ISOMonth]] internal slot. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
@ -910,7 +895,7 @@ u8 iso_month(Object& temporal_object)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// 12.2.40 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode // 12.2.39 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
String iso_month_code(Object& temporal_object) String iso_month_code(Object& temporal_object)
{ {
// 1. Assert: temporalObject has an [[ISOMonth]] internal slot. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
@ -928,7 +913,7 @@ String iso_month_code(Object& temporal_object)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// 12.2.41 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode // 12.2.40 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
u8 iso_day(Object& temporal_object) u8 iso_day(Object& temporal_object)
{ {
// 1. Assert: temporalObject has an [[ISODay]] internal slot. // 1. Assert: temporalObject has an [[ISODay]] internal slot.
@ -946,7 +931,7 @@ u8 iso_day(Object& temporal_object)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// 12.2.42 DefaultMergeFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-defaultmergefields // 12.2.41 DefaultMergeFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-defaultmergefields
ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Object const& fields, Object const& additional_fields) ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Object const& fields, Object const& additional_fields)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();

View file

@ -64,7 +64,6 @@ ThrowCompletionOr<bool> calendar_equals(GlobalObject&, Object& one, Object& two)
ThrowCompletionOr<Object*> consolidate_calendars(GlobalObject&, Object& one, Object& two); ThrowCompletionOr<Object*> consolidate_calendars(GlobalObject&, Object& one, Object& two);
u8 iso_days_in_month(i32 year, u8 month); u8 iso_days_in_month(i32 year, u8 month);
u8 to_iso_day_of_week(i32 year, u8 month, u8 day); 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); u8 to_iso_week_of_year(i32 year, u8 month, u8 day);
String build_iso_month_code(u8 month); String build_iso_month_code(u8 month);
ThrowCompletionOr<double> resolve_iso_month(GlobalObject&, Object const& fields); ThrowCompletionOr<double> resolve_iso_month(GlobalObject&, Object const& fields);

View file

@ -342,8 +342,14 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_year)
// 4. Let temporalDate be ? ToTemporalDate(temporalDateLike). // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike).
auto* temporal_date = TRY(to_temporal_date(global_object, vm.argument(0))); auto* temporal_date = TRY(to_temporal_date(global_object, vm.argument(0)));
// 5. Return 𝔽(! ToISODayOfYear(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])). // 5. Let epochDays be MakeDay(𝔽(temporalDate.[[ISOYear]]), 𝔽(temporalDate.[[ISOMonth]] - 1), 𝔽(temporalDate.[[ISODay]])).
return Value(to_iso_day_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day())); auto epoch_days = make_day(temporal_date->iso_year(), temporal_date->iso_month() - 1, temporal_date->iso_day());
// 6. Assert: epochDays is finite.
VERIFY(isfinite(epoch_days));
// 7. Return DayWithinYear(MakeDate(epochDays, +0𝔽)) + 1𝔽.
return Value(day_within_year(make_date(epoch_days, 0)) + 1);
} }
// 12.4.15 Temporal.Calendar.prototype.weekOfYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.weekofyear // 12.4.15 Temporal.Calendar.prototype.weekOfYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.weekofyear