PEL: Convert a BCDTime to epoch milliseconds
Add a new function to convert a BCDTime value from a PEL to the number
of milliseconds since the epoch.
This will be used to put the PEL creation timestamp on D-Bus.
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ic470c9324e959b3e6a7eea29db4162a45dd54fc3
diff --git a/extensions/openpower-pels/bcd_time.cpp b/extensions/openpower-pels/bcd_time.cpp
index c5dd0d8..42e2e73 100644
--- a/extensions/openpower-pels/bcd_time.cpp
+++ b/extensions/openpower-pels/bcd_time.cpp
@@ -15,6 +15,11 @@
*/
#include "bcd_time.hpp"
+#include <fmt/format.h>
+#include <time.h>
+
+#include <phosphor-logging/log.hpp>
+
namespace openpower
{
namespace pels
@@ -66,6 +71,29 @@
return getBCDTime(time);
}
+uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime)
+{
+ // Convert a UTC tm struct to a UTC time_t struct to a timepoint.
+ int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB);
+ tm utcTime;
+ utcTime.tm_year = year - 1900;
+ utcTime.tm_mon = fromBCD(bcdTime.month) - 1;
+ utcTime.tm_mday = fromBCD(bcdTime.day);
+ utcTime.tm_hour = fromBCD(bcdTime.hour);
+ utcTime.tm_min = fromBCD(bcdTime.minutes);
+ utcTime.tm_sec = fromBCD(bcdTime.seconds);
+ utcTime.tm_isdst = 0;
+
+ time_t t = timegm(&utcTime);
+ auto timepoint = std::chrono::system_clock::from_time_t(t);
+ int milliseconds = fromBCD(bcdTime.hundredths) * 10;
+ timepoint += std::chrono::milliseconds(milliseconds);
+
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ timepoint.time_since_epoch())
+ .count();
+}
+
Stream& operator>>(Stream& s, BCDTime& time)
{
s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;