diff options
author | Linus Groh <mail@linusgroh.de> | 2021-10-30 09:18:01 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-30 10:15:49 +0200 |
commit | de2e95b2789d742df436b848412814b8cbcdf0fb (patch) | |
tree | 000f22fddd4658bb40fc2f14794feec12c3cc5c9 /Userland | |
parent | 28a9a248d637a3deded706f84d0f9eae8116c3e9 (diff) | |
download | serenity-de2e95b2789d742df436b848412814b8cbcdf0fb.zip |
LibJS: Ensure make_day()'s temporary Core::DateTime is treated as UTC
DateTime::create() and subsequently DateTime::set_time() uses mktime()
internally to ensure out-of-range input values still result in a valid
date (Jan 32 -> Feb 1 etc.).
This however also means that the input is treated as local time, and
then shifted to UTC accordingly for the returned time_t - it is however
already in UTC in this case! The temporary solution is simply to set the
"TZ" environment variable to "UTC" and back after create(). The proper
solution is probably to have better timezone support in Core::DateTime.
This should only affect Lagom, as serenity itself has no timezone
support yet and always assumes UTC.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Date.cpp | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index 6a54ca3885..cdb2568984 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -341,7 +341,14 @@ Value make_day(GlobalObject& global_object, Value year, Value month, Value date) // 8. Find a finite time value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn and DateFromTime(t) is 1𝔽; but if this is not possible (because some argument is out of range), return NaN. if (!AK::is_within_range<int>(y) || !AK::is_within_range<int>(m + 1)) return js_nan(); + // FIXME: Core::DateTime assumes the argument values are in local time, which is not the case here. + // Let mktime() think local time is UTC by temporarily overwriting the TZ environment variable, + // so that the values are not adjusted. Core::DateTime should probably learn to deal with both + // local time and UTC time itself. + auto* tz = getenv("TZ"); + VERIFY(setenv("TZ", "UTC", 1) == 0); auto t = Core::DateTime::create(static_cast<int>(y), static_cast<int>(m + 1), 1).timestamp() * 1000; + tz ? setenv("TZ", tz, 1) : unsetenv("TZ"); // 9. Return Day(t) + dt - 1𝔽. return Value(day(static_cast<double>(t)) + dt - 1); } |