diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-06-06 18:30:28 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-06-06 19:14:11 +0100 |
commit | 59034554a4626aaa5fdaa525aeda7fc51fff8524 (patch) | |
tree | 3c13882d35c4b7fe8f084039976b163d24e8e6d6 | |
parent | b893963651fa7f1f4a96016e4218bc3405094292 (diff) | |
download | serenity-59034554a4626aaa5fdaa525aeda7fc51fff8524.zip |
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.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/DatePrototype.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setFullYear.js | 8 |
2 files changed, 6 insertions, 6 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 4f0c958b18..2b59e36945 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -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()) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setFullYear.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setFullYear.js index a9a957b0db..9c5dec7de9 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setFullYear.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setFullYear.js @@ -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); |