frudevice: use strftime instead of asctime
asctime is obsolete. Additionally, the output was not strictly parsable
via get_time().
The sanitized output beforehand:
FRU Device Description : Motherboard (ID 5)
Board Mfg Date : Mon Jan 1 00:00:00 1996
Product Part Number : 1051855
Afterwards:
FRU Device Description : Motherboard (ID 5)
Board Mfg Date : Tue Jul 9 16:35:00 2019
Product Part Number : 1051855
Tested: Verified the format was now working in both directions, from FRU
to dbus, from dbus to IPMI. The difference with asctime was that it was
padding with a space instead of a 0.
phosphor-host-ipmid is expecting the following format: %a %b %d %H:%M:%S %Y
It would also make sense to convert the phosphor-host-ipmid format to
expect the leading whitespace, however, with asctime being obsolete,
this change is also valid. Submitting a change to phosphor-host-ipmid
to handle this format.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I7afd5ba0763bd4105dd8648370f656fa43e0f5af
diff --git a/src/FruDevice.cpp b/src/FruDevice.cpp
index 7c8ea10..aac0d89 100644
--- a/src/FruDevice.cpp
+++ b/src/FruDevice.cpp
@@ -733,13 +733,21 @@
*(fruBytesIter + 1) << 8 |
*(fruBytesIter + 2) << 16;
std::tm fruTime = intelEpoch();
- time_t timeValue = mktime(&fruTime);
+ std::time_t timeValue = std::mktime(&fruTime);
timeValue += minutes * 60;
- fruTime = *gmtime(&timeValue);
+ fruTime = *std::gmtime(&timeValue);
- result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
- result["BOARD_MANUFACTURE_DATE"]
- .pop_back(); // remove trailing null
+ // Tue Nov 20 23:08:00 2018
+ char timeString[32] = {0};
+ auto bytes = std::strftime(timeString, sizeof(timeString),
+ "%a %b %e %H:%M:%S %Y", &fruTime);
+ if (bytes == 0)
+ {
+ std::cerr << "invalid time string encountered\n";
+ return false;
+ }
+
+ result["BOARD_MANUFACTURE_DATE"] = std::string(timeString);
fruBytesIter += 3;
fieldData = &BOARD_FRU_AREAS;
}