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;
diff --git a/extensions/openpower-pels/bcd_time.hpp b/extensions/openpower-pels/bcd_time.hpp
index 5679a5b..c2f6611 100644
--- a/extensions/openpower-pels/bcd_time.hpp
+++ b/extensions/openpower-pels/bcd_time.hpp
@@ -57,6 +57,15 @@
BCDTime getBCDTime(uint64_t milliseconds);
/**
+ * @brief Convert a BCDTime value into the number of
+ * milliseconds since the epoch (1/1/1970).
+ *
+ * @param[in] bcdTime - The BCD time value
+ * @return uint64_t - The milliseconds value
+ */
+uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime);
+
+/**
* @brief Converts a number to a BCD.
*
* For example 32 -> 0x32.
@@ -86,6 +95,19 @@
}
/**
+ * @brief Converts a BCD byte to a decimal number.
+ *
+ * For example 0x22 -> 22.
+ *
+ * @param[in] bcd - The value in BCD
+ * @return int - The number in decimal
+ */
+inline int fromBCD(uint8_t bcd)
+{
+ return (((bcd & 0xF0) >> 4) * 10) + (bcd & 0xF);
+}
+
+/**
* @brief Stream extraction operator for BCDTime
*
* @param[in] s - the Stream