From 010ec36d20e6c2d97fd6fb4e6bd7f8582f123c57 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 24 Jan 2022 15:38:33 -0500 Subject: [PATCH] LibC: Ensure most time tests run under UTC This ensures these tests pass even if the user has changed the system time zone away from UTC. --- Tests/LibC/TestLibCTime.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/LibC/TestLibCTime.cpp b/Tests/LibC/TestLibCTime.cpp index 4fa829e2a2b..72e0777b2c4 100644 --- a/Tests/LibC/TestLibCTime.cpp +++ b/Tests/LibC/TestLibCTime.cpp @@ -17,6 +17,12 @@ public: { } + explicit TimeZoneGuard(char const* tz) + : m_tz(getenv("TZ")) + { + setenv("TZ", tz, 1); + } + ~TimeZoneGuard() { if (m_tz) @@ -31,6 +37,8 @@ private: TEST_CASE(asctime) { + TimeZoneGuard guard("UTC"); + time_t epoch = 0; auto result = asctime(localtime(&epoch)); EXPECT_EQ(expected_epoch, StringView(result)); @@ -38,6 +46,8 @@ TEST_CASE(asctime) TEST_CASE(asctime_r) { + TimeZoneGuard guard("UTC"); + char buffer[26] {}; time_t epoch = 0; auto result = asctime_r(localtime(&epoch), buffer); @@ -46,6 +56,8 @@ TEST_CASE(asctime_r) TEST_CASE(ctime) { + TimeZoneGuard guard("UTC"); + time_t epoch = 0; auto result = ctime(&epoch); @@ -54,6 +66,8 @@ TEST_CASE(ctime) TEST_CASE(ctime_r) { + TimeZoneGuard guard("UTC"); + char buffer[26] {}; time_t epoch = 0; auto result = ctime_r(&epoch, buffer);