LibJS+LibUnicode: Port retrieving the system time zone to ICU

This commit is contained in:
Timothy Flynn
2024-06-24 10:17:32 -04:00
committed by Andreas Kling
parent 89aa9a3af0
commit d3e809bcd4
16 changed files with 115 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ set(TEST_SOURCES
TestIDNA.cpp
TestLocale.cpp
TestSegmenter.cpp
TestTimeZone.cpp
TestUnicodeCharacterTypes.cpp
TestUnicodeNormalization.cpp
)

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/StringView.h>
#include <LibCore/Environment.h>
#include <LibUnicode/TimeZone.h>
class TimeZoneGuard {
public:
explicit TimeZoneGuard(StringView time_zone)
: m_time_zone(Core::Environment::get("TZ"sv))
{
MUST(Core::Environment::set("TZ"sv, time_zone, Core::Environment::Overwrite::Yes));
}
~TimeZoneGuard()
{
if (m_time_zone.has_value())
MUST(Core::Environment::set("TZ"sv, *m_time_zone, Core::Environment::Overwrite::Yes));
else
MUST(Core::Environment::unset("TZ"sv));
}
private:
Optional<StringView> m_time_zone;
};
TEST_CASE(current_time_zone)
{
{
TimeZoneGuard guard { "America/New_York"sv };
EXPECT_EQ(Unicode::current_time_zone(), "America/New_York"sv);
}
{
TimeZoneGuard guard { "ladybird"sv };
EXPECT_EQ(Unicode::current_time_zone(), "UTC"sv);
}
}