ladybird/Tests/LibUnicode/TestTimeZone.cpp
Timothy Flynn 4fc0fba646 LibCore+LibJS+LibUnicode: Port retrieving available time zones to ICU
This required updating some LibJS spec steps to their latest versions,
as the data expected by the old steps does not quite match the APIs that
are available with the ICU. The new spec steps are much more aligned.
2024-06-26 10:14:02 +02:00

58 lines
1.5 KiB
C++

/*
* 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);
}
}
TEST_CASE(available_time_zones)
{
auto const& time_zones = Unicode::available_time_zones();
EXPECT(time_zones.contains_slow("UTC"sv));
EXPECT(!time_zones.contains_slow("EAT"sv));
}
TEST_CASE(resolve_primary_time_zone)
{
EXPECT_EQ(Unicode::resolve_primary_time_zone("UTC"sv), "Etc/UTC"sv);
EXPECT_EQ(Unicode::resolve_primary_time_zone("Asia/Katmandu"sv), "Asia/Kathmandu"sv);
EXPECT_EQ(Unicode::resolve_primary_time_zone("Australia/Canberra"sv), "Australia/Sydney"sv);
}