LibCore: Convert explicit timezone to local in DateTime::parse

When we encounter an explicit timezone, we shift the time to UTC.
Because we rely on `mktime`, we need to shift it to the local time
before proceeding. If no explicit timezone is provided, local timezone
is assumed.

This fixes the "timezone-offset extension" LibJS test running on
machines with a non-UTC timezone offset.

Co-authored-by: Timothy Flynn <trflynn89@pm.me>
This commit is contained in:
Jelle Raaijmakers 2022-12-19 01:44:14 +01:00 committed by Tim Flynn
parent 1ea10bcb73
commit 9feac465dc
Notes: sideshowbarker 2024-07-17 03:00:26 +09:00

View file

@ -283,6 +283,7 @@ Optional<DateTime> 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> 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> 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));
}
}