diff options
author | Linus Groh <mail@linusgroh.de> | 2022-03-31 00:54:27 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-31 17:09:10 +0100 |
commit | 8e175b4959995ceda6bed78293a00df918e747d7 (patch) | |
tree | 3ecd2c0f02fe939835e4b4adff0c4e223578ac33 | |
parent | cfb04765fa83683944f96b535c336e5d607e9b60 (diff) | |
download | serenity-8e175b4959995ceda6bed78293a00df918e747d7.zip |
LibJS: Adjust ISO8601 representation for years between 1 BCE and 999 CE
This is a normative change in the Temporal spec.
See: https://github.com/tc39/proposal-temporal/commit/39eeecd
3 files changed, 17 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index c567ce0783..85d62dc2cd 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -499,13 +499,14 @@ String pad_iso_year(i32 y) { // 1. Assert: y is an integer. - // 2. If y > 999 and y ≤ 9999, then - if (y > 999 && y <= 9999) { - // a. Return y formatted as a four-digit decimal number. - return String::number(y); + // 2. If y ≥ 0 and y ≤ 9999, then + if (y >= 0 && y <= 9999) { + // a. Return y formatted as a four-digit decimal number, padded to the left with zeroes as necessary. + return String::formatted("{:04}", y); } - // 3. If y ≥ 0, let yearSign be "+"; otherwise, let yearSign be "-". - auto year_sign = y >= 0 ? '+' : '-'; + + // 3. If y > 0, let yearSign be "+"; otherwise, let yearSign be "-". + auto year_sign = y > 0 ? '+' : '-'; // 4. Let year be abs(y), formatted as a six-digit decimal number, padded to the left with zeroes as necessary. // 5. Return the string-concatenation of yearSign and year. diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toString.js index 0722a55340..3faad7c0a8 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toString.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toString.js @@ -19,10 +19,13 @@ describe("correct behavior", () => { expect(plainDate.toString({ calendarName: "never" })).toBe("2021-07-06"); plainDate = new Temporal.PlainDate(0, 1, 1); - expect(plainDate.toString()).toBe("+000000-01-01"); + expect(plainDate.toString()).toBe("0000-01-01"); plainDate = new Temporal.PlainDate(999, 1, 1); - expect(plainDate.toString()).toBe("+000999-01-01"); + expect(plainDate.toString()).toBe("0999-01-01"); + + plainDate = new Temporal.PlainDate(9999, 1, 1); + expect(plainDate.toString()).toBe("9999-01-01"); plainDate = new Temporal.PlainDate(12345, 1, 1); expect(plainDate.toString()).toBe("+012345-01-01"); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toString.js index 2be37729d9..487cedda4d 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toString.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toString.js @@ -21,10 +21,13 @@ describe("correct behavior", () => { expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07-06"); plainYearMonth = new Temporal.PlainYearMonth(0, 1); - expect(plainYearMonth.toString()).toBe("+000000-01"); + expect(plainYearMonth.toString()).toBe("0000-01"); plainYearMonth = new Temporal.PlainYearMonth(999, 1); - expect(plainYearMonth.toString()).toBe("+000999-01"); + expect(plainYearMonth.toString()).toBe("0999-01"); + + plainYearMonth = new Temporal.PlainYearMonth(9999, 1); + expect(plainYearMonth.toString()).toBe("9999-01"); plainYearMonth = new Temporal.PlainYearMonth(12345, 1); expect(plainYearMonth.toString()).toBe("+012345-01"); |