diff options
author | Luke Wilde <lukew@serenityos.org> | 2023-05-21 20:35:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-22 06:07:05 +0200 |
commit | 78db4e683f9149b36e4e4f3c90f0cb22c1faaf3d (patch) | |
tree | eb48f2472a08532d401a34267007f0514b677ef5 | |
parent | e038901555ea901bc73fd4ed37bf6cec66a8fc20 (diff) | |
download | serenity-78db4e683f9149b36e4e4f3c90f0cb22c1faaf3d.zip |
LibJS: Add "Month dd, yy hh:mm:ss" Date format
Required by Discord's Birthday page.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/DateConstructor.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Date/Date.parse.js | 19 |
2 files changed, 22 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp index 96380dea5e..e9ceca6de2 100644 --- a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -147,12 +147,13 @@ static double parse_simplified_iso8601(DeprecatedString const& iso_8601) return time_clip(time_ms); } -static constexpr AK::Array<StringView, 5> extra_formats = { +static constexpr AK::Array<StringView, 6> extra_formats = { "%a %b %e %T %z %Y"sv, "%m/%e/%Y"sv, "%m/%e/%Y %R %z"sv, "%Y/%m/%e %R"sv, "%Y-%m-%e %R"sv, + "%B %e, %Y %T"sv, }; static double parse_date_string(DeprecatedString const& date_string) @@ -166,6 +167,7 @@ static double parse_date_string(DeprecatedString const& date_string) // And: "4/17/2019" // And: "12/05/2022 10:00 -0800" // And: "2014/11/14 13:05" or "2014-11-14 13:05" + // And: "June 5, 2023 17:00:00" // FIXME: Exactly what timezone and which additional formats we should support is unclear. // Both Chrome and Firefox seem to support "4/17/2019 11:08 PM +0000" with most parts // being optional, however this is not clearly documented anywhere. diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.parse.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.parse.js index efefb27dba..8c464a5c0d 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.parse.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.parse.js @@ -100,3 +100,22 @@ test("yy{/,-}mm{/,-}dd hh:mm extension", () => { expectStringToGiveDate("2014/11/14 13:05", 2014, 11, 14, 13, 5); expectStringToGiveDate("2014-11-14 13:05", 2014, 11, 14, 13, 5); }); + +test("Month dd, yy hh:mm:ss extension", () => { + function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours, minutes, seconds) { + // Since the timezone is not specified we just say it has to equal the date parts. + const date = new Date(Date.parse(input)); + expect(date.getFullYear()).toBe(fullYear); + expect(date.getMonth() + 1).toBe(month); + expect(date.getDate()).toBe(dayInMonth); + expect(date.getHours()).toBe(hours); + expect(date.getMinutes()).toBe(minutes); + expect(date.getSeconds()).toBe(seconds); + } + + // Examples from Discord's Birthday JavaScript for May 2023. + expectStringToGiveDate("May 15, 2023 17:00:00", 2023, 5, 15, 17, 0, 0); + expectStringToGiveDate("May 22, 2023 17:00:00", 2023, 5, 22, 17, 0, 0); + expectStringToGiveDate("May 30, 2023 17:00:00", 2023, 5, 30, 17, 0, 0); + expectStringToGiveDate("June 5, 2023 17:00:00", 2023, 6, 5, 17, 0, 0); +}); |