From 37146e305a168ba505e200f0d199dd76ec05b18d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 5 Nov 2021 01:09:18 +0100 Subject: [PATCH] LibJS: Account for leap days in year_from_time() A year has 365.2425 days, not 365. This lead to off-by-one results for time values (milliseconds) near the end of a year, which would lead to calculation issues and crashes in other AOs. Fixes #10796. --- Userland/Libraries/LibJS/Runtime/Date.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index cdb2568984..328d0f51f2 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -205,7 +205,7 @@ double day_from_year(i32 y) i32 year_from_time(double t) { // the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t - return static_cast(t / (365.0 * MS_PER_DAY) + 1970); + return static_cast(t / (365.2425 * MS_PER_DAY) + 1970); } // InLeapYear(t), https://tc39.es/ecma262/#eqn-InLeapYear