TestLibCTime.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <LibTest/TestCase.h>
  8. #include <time.h>
  9. const auto expected_epoch = "Thu Jan 1 00:00:00 1970\n"sv;
  10. class TimeZoneGuard {
  11. public:
  12. TimeZoneGuard()
  13. : m_tz(getenv("TZ"))
  14. {
  15. }
  16. explicit TimeZoneGuard(char const* tz)
  17. : m_tz(getenv("TZ"))
  18. {
  19. setenv("TZ", tz, 1);
  20. }
  21. ~TimeZoneGuard()
  22. {
  23. if (m_tz)
  24. setenv("TZ", m_tz, 1);
  25. else
  26. unsetenv("TZ");
  27. }
  28. private:
  29. char const* m_tz { nullptr };
  30. };
  31. TEST_CASE(asctime)
  32. {
  33. TimeZoneGuard guard("UTC");
  34. time_t epoch = 0;
  35. auto result = asctime(localtime(&epoch));
  36. EXPECT_EQ(expected_epoch, StringView(result));
  37. }
  38. TEST_CASE(asctime_r)
  39. {
  40. TimeZoneGuard guard("UTC");
  41. char buffer[26] {};
  42. time_t epoch = 0;
  43. auto result = asctime_r(localtime(&epoch), buffer);
  44. EXPECT_EQ(expected_epoch, StringView(result));
  45. }
  46. TEST_CASE(ctime)
  47. {
  48. TimeZoneGuard guard("UTC");
  49. time_t epoch = 0;
  50. auto result = ctime(&epoch);
  51. EXPECT_EQ(expected_epoch, StringView(result));
  52. }
  53. TEST_CASE(ctime_r)
  54. {
  55. TimeZoneGuard guard("UTC");
  56. char buffer[26] {};
  57. time_t epoch = 0;
  58. auto result = ctime_r(&epoch, buffer);
  59. EXPECT_EQ(expected_epoch, StringView(result));
  60. }
  61. TEST_CASE(tzset)
  62. {
  63. TimeZoneGuard guard;
  64. auto set_tz = [](char const* tz) {
  65. setenv("TZ", tz, 1);
  66. tzset();
  67. };
  68. set_tz("UTC");
  69. EXPECT_EQ(timezone, 0);
  70. EXPECT_EQ(altzone, 0);
  71. EXPECT_EQ(daylight, 0);
  72. EXPECT_EQ(tzname[0], "UTC"sv);
  73. EXPECT_EQ(tzname[1], "UTC"sv);
  74. set_tz("America/New_York");
  75. EXPECT_EQ(timezone, 5 * 60 * 60);
  76. EXPECT_EQ(altzone, 4 * 60 * 60);
  77. EXPECT_EQ(daylight, 1);
  78. EXPECT_EQ(tzname[0], "EST"sv);
  79. EXPECT_EQ(tzname[1], "EDT"sv);
  80. set_tz("America/Phoenix");
  81. EXPECT_EQ(timezone, 7 * 60 * 60);
  82. EXPECT_EQ(altzone, 7 * 60 * 60);
  83. EXPECT_EQ(daylight, 0);
  84. EXPECT_EQ(tzname[0], "MST"sv);
  85. EXPECT_EQ(tzname[1], "MST"sv);
  86. set_tz("America/Asuncion");
  87. EXPECT_EQ(timezone, 4 * 60 * 60);
  88. EXPECT_EQ(altzone, 3 * 60 * 60);
  89. EXPECT_EQ(daylight, 1);
  90. EXPECT_EQ(tzname[0], "-04"sv);
  91. EXPECT_EQ(tzname[1], "-03"sv);
  92. set_tz("CET");
  93. EXPECT_EQ(timezone, -1 * 60 * 60);
  94. EXPECT_EQ(altzone, -2 * 60 * 60);
  95. EXPECT_EQ(daylight, 1);
  96. EXPECT_EQ(tzname[0], "CET"sv);
  97. EXPECT_EQ(tzname[1], "CEST"sv);
  98. }