mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
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:
parent
1ea10bcb73
commit
9feac465dc
Notes:
sideshowbarker
2024-07-17 03:00:26 +09:00
Author: https://github.com/gmta Commit: https://github.com/SerenityOS/serenity/commit/9feac465dc Pull-request: https://github.com/SerenityOS/serenity/pull/16582
1 changed files with 9 additions and 0 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue