Skip to content

Commit

Permalink
LibJS: Display example Date strings inline with their format strings
Browse files Browse the repository at this point in the history
The current comment style feels a bit awkward to read.
  • Loading branch information
trflynn89 authored and awesomekling committed Nov 8, 2023
1 parent 9923377 commit 9d3f942
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Userland/Libraries/LibJS/Runtime/DateConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,25 @@ static double parse_simplified_iso8601(DeprecatedString const& iso_8601)
return time_clip(time_ms);
}

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)
{
auto value = parse_simplified_iso8601(date_string);
if (isfinite(value))
return value;

// Date.parse() is allowed to accept an arbitrary number of implementation-defined formats.
// Parse formats of this type: "Wed Apr 17 23:08:53 +0000 2019"
// 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.
static constexpr auto extra_formats = AK::Array {
"%a %b %e %T %z %Y"sv, // "Wed Apr 17 23:08:53 +0000 2019"
"%m/%e/%Y"sv, // "4/17/2019"
"%m/%e/%Y %R %z"sv, // "12/05/2022 10:00 -0800"
"%Y/%m/%e %R"sv, // "2014/11/14 13:05"
"%Y-%m-%e %R"sv, // "2014-11-14 13:05"
"%B %e, %Y %T"sv, // "June 5, 2023 17:00:00"
};

for (auto const& format : extra_formats) {
auto maybe_datetime = Core::DateTime::parse(format, date_string);
if (maybe_datetime.has_value())
Expand Down

0 comments on commit 9d3f942

Please sign in to comment.