Truly independent web browser
LibJS: Parse a negative year of four or five digits in Date.parse
Shannon Booth905b053d94da
diff --git a/Libraries/LibJS/Rust/src/runtime/date_parser.rs b/Libraries/LibJS/Rust/src/runtime/date_parser.rsindex 4b3072ec249..58b3d58e842 100644--- a/Libraries/LibJS/Rust/src/runtime/date_parser.rs+++ b/Libraries/LibJS/Rust/src/runtime/date_parser.rs@@ -572,8 +572,13 @@ impl DateParser {return sign == -1;}1..=5 => {- // Too small to be a signed year;if self.hours.is_none() {+ // A negative year of four or five digits is how toString() and toUTCString() write the years from+ // -99999 to -1, so it has to parse as that year.+ if sign == -1 && number.digits >= 4 && self.year.is_none() {+ self.year = Some(-(number.number as i64));+ return true;+ }// Otherwise a candidate for timezone offset.self.numbers.push(number.number); // Ignore the sign and treat it as a number.return true;diff --git a/Tests/LibJS/Runtime/builtins/Date/Date.parse.js b/Tests/LibJS/Runtime/builtins/Date/Date.parse.jsindex cd7009de924..da059ae7b3b 100644--- a/Tests/LibJS/Runtime/builtins/Date/Date.parse.js+++ b/Tests/LibJS/Runtime/builtins/Date/Date.parse.js@@ -245,6 +245,12 @@ test("Round trip Date.prototype.to*String", () => {expect(Date.parse(epoch.toString())).toBe(epoch.valueOf());expect(Date.parse(epoch.toISOString())).toBe(epoch.valueOf());expect(Date.parse(epoch.toUTCString())).toBe(epoch.valueOf());++ for (const year of [-1, -999, -12345]) {+ const date = new Date(Date.UTC(year, 5, 1));+ expect(Date.parse(date.toUTCString())).toBe(date.valueOf());+ expect(Date.parse(date.toISOString())).toBe(date.valueOf());+ }});test("UTC offset hour out of range", () => {