mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibJS: Protect YearFromTime against non-finite times
This commit is contained in:
parent
8ad043fe5e
commit
9be0a0fd28
1 changed files with 3 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/NumericLimits.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Time.h>
|
#include <AK/Time.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
|
@ -147,6 +148,8 @@ double time_from_year(i32 y)
|
||||||
i32 year_from_time(double t)
|
i32 year_from_time(double t)
|
||||||
{
|
{
|
||||||
// the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
|
// the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
|
||||||
|
if (!Value(t).is_finite_number())
|
||||||
|
return NumericLimits<i32>::max();
|
||||||
|
|
||||||
// Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
|
// Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
|
||||||
auto year = static_cast<i32>(t / (365.2425 * Date::ms_per_day) + 1970);
|
auto year = static_cast<i32>(t / (365.2425 * Date::ms_per_day) + 1970);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue