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

LibJS: Account for differences in month representations (0-11 vs 1-12)

Since DateTime stores months as 1 to 12, while JS accepts months as
0 to 11, we have to account for the difference (by subtracting or
adding 1) where appropriate.
This commit is contained in:
Idan Horowitz 2021-06-06 18:30:28 +03:00 committed by Linus Groh
parent b893963651
commit 59034554a4
2 changed files with 6 additions and 6 deletions

View file

@ -169,14 +169,14 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
}
auto new_year = new_year_value.as_i32();
auto new_month_value = arg_or(1, datetime.month());
auto new_month_value = arg_or(1, datetime.month() - 1);
if (vm.exception())
return {};
if (!new_month_value.is_finite_number()) {
this_object->set_is_invalid(true);
return js_nan();
}
auto new_month = new_month_value.as_i32();
auto new_month = new_month_value.as_i32() + 1; // JS Months: 0 - 11, DateTime months: 1-12
auto new_day_value = arg_or(2, datetime.day());
if (vm.exception())

View file

@ -40,7 +40,7 @@ test("Year and month as arguments", () => {
date.setFullYear(2021, 3);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(2);
expect(date.getMonth()).toBe(3);
expect(date.getDate()).toBe(1);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
@ -53,7 +53,7 @@ test("Year, month, and day as arguments", () => {
date.setFullYear(2021, 3, 16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(2);
expect(date.getMonth()).toBe(3);
expect(date.getDate()).toBe(16);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
@ -88,7 +88,7 @@ test("NaN or undefined in any arguments", () => {
date.setFullYear(2021, 3, 16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(2);
expect(date.getMonth()).toBe(3);
expect(date.getDate()).toBe(16);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
@ -103,7 +103,7 @@ test("Make Invalid Date valid again", () => {
date.setFullYear(2021, 3, 16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(2);
expect(date.getMonth()).toBe(3);
expect(date.getDate()).toBe(16);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);