TestLibCoreDateTime.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <AK/String.h>
  8. #include <AK/StringView.h>
  9. #include <LibCore/DateTime.h>
  10. #include <LibCore/System.h>
  11. #include <LibTest/TestCase.h>
  12. #include <time.h>
  13. class TimeZoneGuard {
  14. public:
  15. explicit TimeZoneGuard(StringView time_zone)
  16. {
  17. if (auto const* current_time_zone = getenv("TZ"))
  18. m_time_zone = MUST(String::from_utf8({ current_time_zone, strlen(current_time_zone) }));
  19. update(time_zone);
  20. }
  21. ~TimeZoneGuard()
  22. {
  23. if (m_time_zone.has_value())
  24. TRY_OR_FAIL(Core::System::setenv("TZ"sv, *m_time_zone, true));
  25. else
  26. TRY_OR_FAIL(Core::System::unsetenv("TZ"sv));
  27. tzset();
  28. }
  29. void update(StringView time_zone)
  30. {
  31. TRY_OR_FAIL(Core::System::setenv("TZ"sv, time_zone, true));
  32. tzset();
  33. }
  34. private:
  35. Optional<String> m_time_zone;
  36. };
  37. TEST_CASE(parse_time_zone_name)
  38. {
  39. EXPECT(!Core::DateTime::parse("%Z"sv, ""sv).has_value());
  40. EXPECT(!Core::DateTime::parse("%Z"sv, "123"sv).has_value());
  41. EXPECT(!Core::DateTime::parse("%Z"sv, "notatimezone"sv).has_value());
  42. auto test = [](auto format, auto time, u32 year, u32 month, u32 day, u32 hour, u32 minute) {
  43. auto result = Core::DateTime::parse(format, time);
  44. VERIFY(result.has_value());
  45. EXPECT_EQ(year, result->year());
  46. EXPECT_EQ(month, result->month());
  47. EXPECT_EQ(day, result->day());
  48. EXPECT_EQ(hour, result->hour());
  49. EXPECT_EQ(minute, result->minute());
  50. };
  51. TimeZoneGuard guard { "UTC"sv };
  52. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 UTC"sv, 2023, 01, 23, 10, 50);
  53. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 America/New_York"sv, 2023, 01, 23, 15, 50);
  54. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Europe/Paris"sv, 2023, 01, 23, 9, 50);
  55. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Australia/Perth"sv, 2023, 01, 23, 2, 50);
  56. guard.update("America/New_York"sv);
  57. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 UTC"sv, 2023, 01, 23, 5, 50);
  58. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 America/New_York"sv, 2023, 01, 23, 10, 50);
  59. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Europe/Paris"sv, 2023, 01, 23, 4, 50);
  60. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Australia/Perth"sv, 2023, 01, 22, 21, 50);
  61. guard.update("Europe/Paris"sv);
  62. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 UTC"sv, 2023, 01, 23, 11, 50);
  63. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 America/New_York"sv, 2023, 01, 23, 16, 50);
  64. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Europe/Paris"sv, 2023, 01, 23, 10, 50);
  65. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Australia/Perth"sv, 2023, 01, 23, 3, 50);
  66. guard.update("Australia/Perth"sv);
  67. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 UTC"sv, 2023, 01, 23, 18, 50);
  68. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 America/New_York"sv, 2023, 01, 23, 23, 50);
  69. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Europe/Paris"sv, 2023, 01, 23, 17, 50);
  70. test("%Y/%m/%d %R %Z"sv, "2023/01/23 10:50 Australia/Perth"sv, 2023, 01, 23, 10, 50);
  71. }