From 8dce1f311ab575b549495ff29a296f0c2b068386 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 25 Apr 2019 16:20:36 +0200 Subject: [PATCH] explicitly set timezone for tests that depend on it Some timefunc tests rely on the time zone to be CE[S]T and will fail otherwise. Not a good thing for a correctness test to do, so set the zone explicitly in those cases. Example: $ ./test/test_i2ncommon --run_test=TestTimeFunc/FormatFullTime Running 1 test case... *** No errors detected $ TZ=UTC ./test/test_i2ncommon --run_test=TestTimeFunc/FormatFullTime Running 1 test case... /home/philipp/src/i2ncommon/libi2ncommon-git/test/test_timefunc.cpp(480): error: in "TestTimeFunc/FormatFullTime": check "17.10.2011 11:33" == format_full_time(seconds) has failed [17.10.2011 11:33 != 17.10.2011 09:33] --- test/test_timefunc.cpp | 16 +++++++++++++--- 1 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/test_timefunc.cpp b/test/test_timefunc.cpp index 885bb67..ebc22ae 100644 --- a/test/test_timefunc.cpp +++ b/test/test_timefunc.cpp @@ -69,13 +69,13 @@ protected: used_check_files.clear(); } // eo remove_check_files - void set_utc (void) + void set_tz (const std::string &tzname) { errno = 0; - if (setenv ("TZ", "UTC", 1) == -1) + if (setenv ("TZ", tzname.c_str (), 1) == -1) { std::cerr - << "error setting environment 'TZ': [" << this->tz << "]" + << "error setting environment 'TZ': [" << tzname << "]" << std::endl ; return; @@ -84,6 +84,11 @@ protected: tzset (); } + inline void set_utc (void) + { + this->set_tz ("UTC"); + } + public: TestTimeFuncFixture() : tz (secure_getenv ("TZ") ?: "") @@ -475,6 +480,7 @@ BOOST_AUTO_TEST_CASE(WeekDisplayString13) BOOST_AUTO_TEST_CASE(FormatFullTime) { + this->set_tz ("CET"); time_t seconds = 1318844005; BOOST_CHECK_EQUAL("17.10.2011 11:33", format_full_time(seconds)); @@ -482,12 +488,14 @@ BOOST_AUTO_TEST_CASE(FormatFullTime) BOOST_AUTO_TEST_CASE(DateToSeconds1) { + this->set_tz ("CET"); // no DST BOOST_CHECK_EQUAL(1325372400, date_to_seconds("2012-01-01")); } BOOST_AUTO_TEST_CASE(DateToSeconds2) { + this->set_tz ("CET"); // DST BOOST_CHECK_EQUAL(1341093600, date_to_seconds("2012-07-01")); } @@ -501,6 +509,7 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_T) BOOST_AUTO_TEST_CASE(FormatISO8601_TZ_local) { + this->set_tz ("CET"); const time_t moment = 1515492684; BOOST_CHECK_EQUAL("11:11:24Z+0100", format_iso8601 (moment, false, false, true, true)); @@ -515,6 +524,7 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_TZ) BOOST_AUTO_TEST_CASE(FormatISO8601_DTZ_local) { + this->set_tz ("CET"); const time_t moment = 1515492684; BOOST_CHECK_EQUAL("2018-01-09T11:11:24Z+0100", format_iso8601 (moment, false, true, true, true)); -- 1.7.1