LibJS: Implement MakeDay without using AK::years_to_days_since_epoch

Implementing years_to_days_since_epoch without a loop will be tricky.
The TimeFromYear AO gives a good enough approximation for MakeDay until
we figure that out.
This commit is contained in:
Timothy Flynn 2022-01-15 08:37:17 -05:00 committed by Linus Groh
parent 11d7c7ebbd
commit 032664332b
Notes: sideshowbarker 2024-07-17 20:50:37 +09:00

View file

@ -341,7 +341,10 @@ Value make_day(GlobalObject& global_object, Value year, Value month, Value date)
// 8. Find a finite time value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn and DateFromTime(t) is 1𝔽; but if this is not possible (because some argument is out of range), return NaN.
if (!AK::is_within_range<int>(ym) || !AK::is_within_range<int>(mn + 1))
return js_nan();
auto t = days_since_epoch(static_cast<int>(ym), static_cast<int>(mn) + 1, 1) * Date::ms_per_day;
// FIXME: We are avoiding AK::years_to_days_since_epoch here because it is implemented by looping over
// the range [1970, ym), which will spin for any time value with an extremely large year.
auto t = time_from_year(ym) + (day_of_year(static_cast<int>(ym), static_cast<int>(mn) + 1, 1) * Date::ms_per_day);
// 9. Return Day(t) + dt - 1𝔽.
return Value(day(static_cast<double>(t)) + dt - 1);