Skip to content

Commit 253bfb7

Browse files
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 <[email protected]> Change-Id: I6230fd014f44123fe917a8e2b39e3b903d3a05e8
1 parent 916bb97 commit 253bfb7

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

extensions/openpower-pels/bcd_time.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
4343

4444
using namespace std::chrono;
4545
time_t t = system_clock::to_time_t(time);
46-
tm* localTime = localtime(&t);
47-
assert(localTime != nullptr);
46+
tm* gmTime = gmtime(&t);
47+
assert(gmTime != nullptr);
4848

49-
int year = 1900 + localTime->tm_year;
49+
int year = 1900 + gmTime->tm_year;
5050
bcd.yearMSB = toBCD(year / 100);
5151
bcd.yearLSB = toBCD(year % 100);
52-
bcd.month = toBCD(localTime->tm_mon + 1);
53-
bcd.day = toBCD(localTime->tm_mday);
54-
bcd.hour = toBCD(localTime->tm_hour);
55-
bcd.minutes = toBCD(localTime->tm_min);
56-
bcd.seconds = toBCD(localTime->tm_sec);
52+
bcd.month = toBCD(gmTime->tm_mon + 1);
53+
bcd.day = toBCD(gmTime->tm_mday);
54+
bcd.hour = toBCD(gmTime->tm_hour);
55+
bcd.minutes = toBCD(gmTime->tm_min);
56+
bcd.seconds = toBCD(gmTime->tm_sec);
5757

5858
auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
5959
int hundredths = (ms % 1000) / 10;

extensions/openpower-pels/journal.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ std::string Journal::getTimeStamp(sd_journal* journal) const
177177
time_t secs = usec / 1000000;
178178

179179
// Convert seconds to tm struct required by strftime()
180-
struct tm* timeStruct = localtime(&secs);
180+
struct tm* timeStruct = gmtime(&secs);
181181
if (timeStruct == nullptr)
182182
{
183183
throw std::runtime_error{

test/openpower-pels/bcd_time_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ TEST(BCDTimeTest, ConvertTest)
8181
time_tm.tm_sec = 42;
8282
time_tm.tm_isdst = 0;
8383

84-
auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
84+
auto timepoint = std::chrono::system_clock::from_time_t(timegm(&time_tm));
8585
auto timeInBCD = getBCDTime(timepoint);
8686

8787
EXPECT_EQ(timeInBCD.yearMSB, 0x20);

test/openpower-pels/private_header_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ TEST_F(PrivateHeaderTest, ConstructionTest)
156156
time_tm.tm_isdst = 0;
157157

158158
// Convert the above time into a uint64_t in ms since the epoch time
159-
auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
159+
auto timepoint = std::chrono::system_clock::from_time_t(timegm(&time_tm));
160160
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
161161
timepoint.time_since_epoch())
162162
.count();

0 commit comments

Comments
 (0)