LibJS+LibUnicode: Change time zones in a way that works on Windows
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macOS, macos-15, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macOS, macOS-arm64, macos-15) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, Linux, Linux-x86_64, blacksmith-8vcpu-ubuntu-2404) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

On Windows, ICU does not look at the TZ environment variable at all. So
to support changing time zones in test-js, let's set ICU's default time
zone directly.

Note that we no longer deal with "null" time zone strings. We just cache
whatever ICU thinks is the current time zone before attempting to change
it, for which we never have a null result.

Co-authored-by: Andrew Kaster <andrew@ladybird.org>
This commit is contained in:
Timothy Flynn 2025-06-01 08:22:19 -04:00 committed by Tim Flynn
parent 8d33a97630
commit 8ded0c65dc
Notes: github-actions[bot] 2025-06-01 22:50:05 +00:00
4 changed files with 34 additions and 33 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -24,8 +24,8 @@ String current_time_zone()
UErrorCode status = U_ZERO_ERROR;
auto time_zone = adopt_own_if_nonnull(icu::TimeZone::detectHostTimeZone());
if (!time_zone)
auto time_zone = adopt_own_if_nonnull(icu::TimeZone::createDefault());
if (!time_zone || *time_zone == icu::TimeZone::getUnknown())
return "UTC"_string;
icu::UnicodeString time_zone_id;
@ -46,6 +46,18 @@ void clear_system_time_zone_cache()
cached_system_time_zone.clear();
}
ErrorOr<void> set_current_time_zone(StringView time_zone)
{
auto time_zone_data = TimeZoneData::for_time_zone(time_zone);
if (!time_zone_data.has_value())
return Error::from_string_literal("Unable to find the provided time zone");
icu::TimeZone::setDefault(time_zone_data->time_zone());
clear_system_time_zone_cache();
return {};
}
// https://github.com/unicode-org/icu/blob/main/icu4c/source/tools/tzcode/icuzones
static constexpr bool is_legacy_non_iana_time_zone(StringView time_zone)
{