diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp index 25df6ca1cf..09e36addf2 100644 --- a/Userland/Libraries/LibCore/DateTime.cpp +++ b/Userland/Libraries/LibCore/DateTime.cpp @@ -283,6 +283,7 @@ Optional DateTime::parse(StringView format, DeprecatedString const& st struct tm tm = {}; auto parsing_failed = false; + auto tm_represents_utc_time = false; auto parse_number = [&] { if (string_pos >= string.length()) { @@ -487,6 +488,7 @@ Optional DateTime::parse(StringView format, DeprecatedString const& st break; } case 'z': { + tm_represents_utc_time = true; if (string[string_pos] == 'Z') { // UTC time string_pos++; @@ -538,6 +540,13 @@ Optional DateTime::parse(StringView format, DeprecatedString const& st return {}; } + // If an explicit timezone was present, the time in tm was shifted to UTC. + // Convert it to local time, since that is what `mktime` expects. + if (tm_represents_utc_time) { + auto utc_time = timegm(&tm); + localtime_r(&utc_time, &tm); + } + return DateTime::from_timestamp(mktime(&tm)); } }