openpower-pels: time: use gmtime for all operations
The test cases in `bcd_time_test.cpp` could fail if the executing
host were not in UTC. By default the BMC uses UTC but the development
systems are often in a user's local time zone. Switch all time
operations to work off UTC by using gmtime/timegm instead of
localtime/mktime.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I6230fd014f44123fe917a8e2b39e3b903d3a05e8
diff --git a/extensions/openpower-pels/bcd_time.cpp b/extensions/openpower-pels/bcd_time.cpp
index 554a230..cf11951 100644
--- a/extensions/openpower-pels/bcd_time.cpp
+++ b/extensions/openpower-pels/bcd_time.cpp
@@ -43,17 +43,17 @@
using namespace std::chrono;
time_t t = system_clock::to_time_t(time);
- tm* localTime = localtime(&t);
- assert(localTime != nullptr);
+ tm* gmTime = gmtime(&t);
+ assert(gmTime != nullptr);
- int year = 1900 + localTime->tm_year;
+ int year = 1900 + gmTime->tm_year;
bcd.yearMSB = toBCD(year / 100);
bcd.yearLSB = toBCD(year % 100);
- bcd.month = toBCD(localTime->tm_mon + 1);
- bcd.day = toBCD(localTime->tm_mday);
- bcd.hour = toBCD(localTime->tm_hour);
- bcd.minutes = toBCD(localTime->tm_min);
- bcd.seconds = toBCD(localTime->tm_sec);
+ bcd.month = toBCD(gmTime->tm_mon + 1);
+ bcd.day = toBCD(gmTime->tm_mday);
+ bcd.hour = toBCD(gmTime->tm_hour);
+ bcd.minutes = toBCD(gmTime->tm_min);
+ bcd.seconds = toBCD(gmTime->tm_sec);
auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
int hundredths = (ms % 1000) / 10;
diff --git a/extensions/openpower-pels/journal.cpp b/extensions/openpower-pels/journal.cpp
index 8b7f1f7..c9789fb 100644
--- a/extensions/openpower-pels/journal.cpp
+++ b/extensions/openpower-pels/journal.cpp
@@ -177,7 +177,7 @@
time_t secs = usec / 1000000;
// Convert seconds to tm struct required by strftime()
- struct tm* timeStruct = localtime(&secs);
+ struct tm* timeStruct = gmtime(&secs);
if (timeStruct == nullptr)
{
throw std::runtime_error{
diff --git a/test/openpower-pels/bcd_time_test.cpp b/test/openpower-pels/bcd_time_test.cpp
index 1f444b3..f0f3111 100644
--- a/test/openpower-pels/bcd_time_test.cpp
+++ b/test/openpower-pels/bcd_time_test.cpp
@@ -81,7 +81,7 @@
time_tm.tm_sec = 42;
time_tm.tm_isdst = 0;
- auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
+ auto timepoint = std::chrono::system_clock::from_time_t(timegm(&time_tm));
auto timeInBCD = getBCDTime(timepoint);
EXPECT_EQ(timeInBCD.yearMSB, 0x20);
diff --git a/test/openpower-pels/private_header_test.cpp b/test/openpower-pels/private_header_test.cpp
index f7c1446..3b9e8f0 100644
--- a/test/openpower-pels/private_header_test.cpp
+++ b/test/openpower-pels/private_header_test.cpp
@@ -156,7 +156,7 @@
time_tm.tm_isdst = 0;
// Convert the above time into a uint64_t in ms since the epoch time
- auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
+ auto timepoint = std::chrono::system_clock::from_time_t(timegm(&time_tm));
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
timepoint.time_since_epoch())
.count();