1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:28:11 +00:00

LibJS/Date: Ensure YearFromTime(t) holds invariant after approximation

As of https://tc39.es/ecma262/#sec-yearfromtime, YearFromTime(t) should
return `y` such that `TimeFromYear(YearFromTime(t)) <= t`. This wasn't
held, since the approximation contained decimal digits that would nudge
the final value in the wrong direction.

Adapted from Kiesel:
6548a85743

Co-authored-by: Linus Groh <mail@linusgroh.de>
This commit is contained in:
Jesús (gsus) Lapastora 2023-10-22 22:01:38 +02:00 committed by Tim Flynn
parent 5870a1a9a1
commit 2086b8df9c
2 changed files with 10 additions and 1 deletions

View file

@ -70,3 +70,12 @@ test("time clip", () => {
expect(Date.UTC(275760, 8, 13, 0, 0, 0, 0)).toBe(8.64e15);
expect(Date.UTC(275760, 8, 13, 0, 0, 0, 1)).toBeNaN();
});
test("YearFromTime invariant holds with negative times", () => {
// https://tc39.es/ecma262/#sec-yearfromtime: YearFromTime(t) should return
// a value such that TimeFromYear(YearFromTime(t)) <= t.
//
// If this doesn't hold, then the following Date constructor will result in
// a crash from an assertion (#21548).
new Date(Date.UTC(-1112, 11, 31));
});