From 99233776e61ec03e0ddded469e49559508ca0107 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 7 Nov 2023 11:14:55 -0500 Subject: [PATCH] test-js: Add a test helper to override the current time zone --- Tests/LibJS/test-js.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tests/LibJS/test-js.cpp b/Tests/LibJS/test-js.cpp index a921778a792..86174d778bf 100644 --- a/Tests/LibJS/test-js.cpp +++ b/Tests/LibJS/test-js.cpp @@ -5,8 +5,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include +#include +#include TEST_ROOT("Userland/Libraries/LibJS/Tests"); @@ -88,6 +91,26 @@ TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer) return JS::js_null(); } +TESTJS_GLOBAL_FUNCTION(set_time_zone, setTimeZone) +{ + auto current_time_zone = JS::js_null(); + + if (auto const* time_zone = getenv("TZ")) + current_time_zone = JS::PrimitiveString::create(vm, StringView { time_zone, strlen(time_zone) }); + + if (auto time_zone = vm.argument(0); time_zone.is_null()) { + if (auto result = Core::System::unsetenv("TZ"sv); result.is_error()) + return vm.throw_completion(MUST(String::formatted("Could not unset time zone: {}", result.error()))); + } else { + if (auto result = Core::System::setenv("TZ"sv, TRY(time_zone.to_string(vm)), true); result.is_error()) + return vm.throw_completion(MUST(String::formatted("Could not set time zone: {}", result.error()))); + } + + tzset(); + + return current_time_zone; +} + TESTJS_RUN_FILE_FUNCTION(DeprecatedString const& test_file, JS::Realm& realm, JS::ExecutionContext&) { if (!test262_parser_tests)