TestTimeZone.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/StringView.h>
  8. #include <LibCore/Environment.h>
  9. #include <LibUnicode/TimeZone.h>
  10. class TimeZoneGuard {
  11. public:
  12. explicit TimeZoneGuard(StringView time_zone)
  13. : m_time_zone(Core::Environment::get("TZ"sv))
  14. {
  15. MUST(Core::Environment::set("TZ"sv, time_zone, Core::Environment::Overwrite::Yes));
  16. }
  17. ~TimeZoneGuard()
  18. {
  19. if (m_time_zone.has_value())
  20. MUST(Core::Environment::set("TZ"sv, *m_time_zone, Core::Environment::Overwrite::Yes));
  21. else
  22. MUST(Core::Environment::unset("TZ"sv));
  23. }
  24. private:
  25. Optional<StringView> m_time_zone;
  26. };
  27. TEST_CASE(current_time_zone)
  28. {
  29. {
  30. TimeZoneGuard guard { "America/New_York"sv };
  31. EXPECT_EQ(Unicode::current_time_zone(), "America/New_York"sv);
  32. }
  33. {
  34. TimeZoneGuard guard { "ladybird"sv };
  35. EXPECT_EQ(Unicode::current_time_zone(), "UTC"sv);
  36. }
  37. }
  38. TEST_CASE(available_time_zones)
  39. {
  40. auto const& time_zones = Unicode::available_time_zones();
  41. EXPECT(time_zones.contains_slow("UTC"sv));
  42. EXPECT(!time_zones.contains_slow("EAT"sv));
  43. }
  44. TEST_CASE(available_time_zones_in_region)
  45. {
  46. {
  47. auto time_zones = Unicode::available_time_zones_in_region("AD"sv);
  48. EXPECT_EQ(time_zones, to_array({ "Europe/Andorra"_string }));
  49. }
  50. {
  51. auto time_zones = Unicode::available_time_zones_in_region("ES"sv);
  52. EXPECT_EQ(time_zones, to_array({ "Africa/Ceuta"_string, "Atlantic/Canary"_string, "Europe/Madrid"_string }));
  53. }
  54. }
  55. TEST_CASE(resolve_primary_time_zone)
  56. {
  57. EXPECT_EQ(Unicode::resolve_primary_time_zone("UTC"sv), "Etc/UTC"sv);
  58. EXPECT_EQ(Unicode::resolve_primary_time_zone("Asia/Katmandu"sv), "Asia/Kathmandu"sv);
  59. EXPECT_EQ(Unicode::resolve_primary_time_zone("Australia/Canberra"sv), "Australia/Sydney"sv);
  60. }
  61. using enum Unicode::TimeZoneOffset::InDST;
  62. static void test_offset(StringView time_zone, i64 time, AK::Duration expected_offset, Unicode::TimeZoneOffset::InDST expected_in_dst)
  63. {
  64. auto actual_offset = Unicode::time_zone_offset(time_zone, AK::UnixDateTime::from_seconds_since_epoch(time));
  65. VERIFY(actual_offset.has_value());
  66. EXPECT_EQ(actual_offset->offset, expected_offset);
  67. EXPECT_EQ(actual_offset->in_dst, expected_in_dst);
  68. }
  69. static constexpr AK::Duration offset(i64 sign, i64 hours, i64 minutes, i64 seconds)
  70. {
  71. return AK::Duration::from_seconds(sign * ((hours * 3600) + (minutes * 60) + seconds));
  72. }
  73. // Useful website to convert times in the TZDB (which sometimes are and aren't UTC) to UTC and the desired local time:
  74. // https://www.epochconverter.com/#tools
  75. //
  76. // In the tests below, if only UTC time is shown as a comment, then the corresponding Rule change in the TZDB was specified
  77. // as UTC. Otherwise, the TZDB time was local, and was converted to a UTC timestamp for that test.
  78. TEST_CASE(time_zone_offset)
  79. {
  80. EXPECT(!Unicode::time_zone_offset("I don't exist"sv, {}).has_value());
  81. test_offset("America/Chicago"sv, -2717647201, offset(-1, 5, 50, 36), No); // November 18, 1883 5:59:59 PM UTC
  82. test_offset("America/Chicago"sv, -2717647200, offset(-1, 6, 0, 0), No); // November 18, 1883 6:00:00 PM UTC
  83. test_offset("America/Chicago"sv, -1067788860, offset(-1, 6, 0, 0), No); // March 1, 1936 1:59:00 AM Chicago (March 1, 1936 7:59:00 AM UTC)
  84. test_offset("America/Chicago"sv, -1067788800, offset(-1, 5, 0, 0), No); // March 1, 1936 3:00:00 AM Chicago (March 1, 1936 8:00:00 AM UTC)
  85. test_offset("America/Chicago"sv, -1045414860, offset(-1, 5, 0, 0), No); // November 15, 1936 1:59:00 AM Chicago (November 15, 1936 6:59:00 AM UTC)
  86. test_offset("America/Chicago"sv, -1045411200, offset(-1, 6, 0, 0), No); // November 15, 1936 2:00:00 AM Chicago (November 15, 1936 8:00:00 AM UTC)
  87. test_offset("Europe/London"sv, -3852662326, offset(-1, 0, 1, 15), No); // November 30, 1847 11:59:59 PM London (December 1, 1847 12:01:14 AM UTC)
  88. test_offset("Europe/London"sv, -3852662325, offset(+1, 0, 0, 0), No); // December 1, 1847 12:01:15 AM London (December 1, 1847 12:01:15 AM UTC)
  89. test_offset("Europe/London"sv, -59004001, offset(+1, 0, 0, 0), No); // February 18, 1968 1:59:59 AM London (February 18, 1968 1:59:59 AM UTC)
  90. test_offset("Europe/London"sv, -59004000, offset(+1, 1, 0, 0), Yes); // February 18, 1968 3:00:00 AM London (February 18, 1968 2:00:00 AM UTC)
  91. test_offset("Europe/London"sv, 57722399, offset(+1, 1, 0, 0), No); // October 31, 1971 1:59:59 AM UTC
  92. test_offset("Europe/London"sv, 57722400, offset(+1, 0, 0, 0), No); // October 31, 1971 2:00:00 AM UTC
  93. test_offset("UTC"sv, -1641846268, offset(+1, 0, 00, 00), No);
  94. test_offset("UTC"sv, 0, offset(+1, 0, 00, 00), No);
  95. test_offset("UTC"sv, 1641846268, offset(+1, 0, 00, 00), No);
  96. test_offset("Etc/GMT+4"sv, -1641846268, offset(-1, 4, 00, 00), No);
  97. test_offset("Etc/GMT+5"sv, 0, offset(-1, 5, 00, 00), No);
  98. test_offset("Etc/GMT+6"sv, 1641846268, offset(-1, 6, 00, 00), No);
  99. test_offset("Etc/GMT-12"sv, -1641846268, offset(+1, 12, 00, 00), No);
  100. test_offset("Etc/GMT-13"sv, 0, offset(+1, 13, 00, 00), No);
  101. test_offset("Etc/GMT-14"sv, 1641846268, offset(+1, 14, 00, 00), No);
  102. }
  103. TEST_CASE(time_zone_offset_with_dst)
  104. {
  105. test_offset("America/New_York"sv, 1642576528, offset(-1, 5, 00, 00), No); // January 19, 2022 2:15:28 AM New York (January 19, 2022 7:15:28 AM UTC)
  106. test_offset("America/New_York"sv, 1663568128, offset(-1, 4, 00, 00), Yes); // September 19, 2022 2:15:28 AM New York (September 19, 2022 6:15:28 AM UTC)
  107. test_offset("America/New_York"sv, 1671471238, offset(-1, 5, 00, 00), No); // December 19, 2022 12:33:58 PM New York (December 19, 2022 5:33:58 PM UTC)
  108. // Phoenix does not observe DST.
  109. test_offset("America/Phoenix"sv, 1642583728, offset(-1, 7, 00, 00), No); // January 19, 2022 2:15:28 AM Phoenix (January 19, 2022 9:15:28 AM UTC)
  110. test_offset("America/Phoenix"sv, 1663578928, offset(-1, 7, 00, 00), No); // September 19, 2022 2:15:28 AM Phoenix (September 19, 2022 9:15:28 AM UTC)
  111. test_offset("America/Phoenix"sv, 1671478438, offset(-1, 7, 00, 00), No); // December 19, 2022 12:33:58 PM Phoenix (December 19, 2022 7:33:58 PM UTC)
  112. // Moscow's observed DST changed several times in 1919.
  113. test_offset("Europe/Moscow"sv, -1609459200, offset(+1, 3, 31, 19), Yes); // January 1, 1919 12:00:00 AM UTC
  114. test_offset("Europe/Moscow"sv, -1596429079, offset(+1, 4, 31, 19), Yes); // June 1, 1919 12:00:00 AM Moscow (May 31, 1919 7:28:41 PM UTC)
  115. test_offset("Europe/Moscow"sv, -1592625600, offset(+1, 4, 00, 00), Yes); // July 15, 1919 12:00:00 AM Moscow (July 14, 1919 8:00:00 PM UTC)
  116. test_offset("Europe/Moscow"sv, -1589079600, offset(+1, 3, 00, 00), No); // August 25, 1919 12:00:00 AM Moscow (August 24, 1919 9:00:00 PM UTC)
  117. // Paraguay begins the year in DST.
  118. test_offset("America/Asuncion"sv, 1642569328, offset(-1, 3, 00, 00), Yes); // January 19, 2022 2:15:28 AM Asuncion (January 19, 2022 5:15:28 AM UTC)
  119. test_offset("America/Asuncion"sv, 1663568128, offset(-1, 4, 00, 00), No); // September 19, 2022 2:15:28 AM Asuncion (September 19, 2022 6:15:28 AM UTC)
  120. test_offset("America/Asuncion"sv, 1671464038, offset(-1, 3, 00, 00), Yes); // December 19, 2022 12:33:58 PM Asuncion (December 19, 2022 3:33:58 PM UTC)
  121. }