mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +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
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 = {};
|
struct tm tm = {};
|
||||||
|
|
||||||
auto parsing_failed = false;
|
auto parsing_failed = false;
|
||||||
|
auto tm_represents_utc_time = false;
|
||||||
|
|
||||||
auto parse_number = [&] {
|
auto parse_number = [&] {
|
||||||
if (string_pos >= string.length()) {
|
if (string_pos >= string.length()) {
|
||||||
|
@ -487,6 +488,7 @@ Optional<DateTime> DateTime::parse(StringView format, DeprecatedString const& st
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'z': {
|
case 'z': {
|
||||||
|
tm_represents_utc_time = true;
|
||||||
if (string[string_pos] == 'Z') {
|
if (string[string_pos] == 'Z') {
|
||||||
// UTC time
|
// UTC time
|
||||||
string_pos++;
|
string_pos++;
|
||||||
|
@ -538,6 +540,13 @@ Optional<DateTime> DateTime::parse(StringView format, DeprecatedString const& st
|
||||||
return {};
|
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));
|
return DateTime::from_timestamp(mktime(&tm));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue