dateStringToEpoch: add the additional format
This adds the additional format for ISO 8601, such as YYYYMMDD or
YYYYMMDDThhmmssZ.
Tested:
Test case 1:
The input ISO 8601 timestamp: 20230531T000000Z
The output Epoch timestamp: 1685491200000000
Test case 2:
The input ISO 8601 timestamp: 20230531
The output Epoch timestamp: 1685491200000000
Signed-off-by: Hieu Huynh <hieuh@os.amperecomputing.com>
Change-Id: I23080a466b2edeecb5d8a4fb7ec0b00739454056
diff --git a/redfish-core/src/utils/time_utils.cpp b/redfish-core/src/utils/time_utils.cpp
index fa49657..2d5e0cc 100644
--- a/redfish-core/src/utils/time_utils.cpp
+++ b/redfish-core/src/utils/time_utils.cpp
@@ -14,7 +14,8 @@
std::optional<usSinceEpoch> dateStringToEpoch(std::string_view datetime)
{
- for (const char* format : std::to_array({"%FT%T%Ez", "%FT%TZ", "%FT%T"}))
+ for (const char* format : std::to_array(
+ {"%FT%T%Ez", "%FT%TZ", "%FT%T", "%Y%m%d", "%Y%m%dT%H%M%SZ"}))
{
// Parse using signed so we can detect negative dates
std::chrono::sys_time<usSinceEpoch> date;
diff --git a/test/redfish-core/include/utils/time_utils_test.cpp b/test/redfish-core/include/utils/time_utils_test.cpp
index ca11097..aa6f53b 100644
--- a/test/redfish-core/include/utils/time_utils_test.cpp
+++ b/test/redfish-core/include/utils/time_utils_test.cpp
@@ -152,6 +152,13 @@
EXPECT_EQ(dateStringToEpoch("2021-11-30T22:41:35"),
usSinceEpoch{1638312095000000});
+ // valid datetime format
+ EXPECT_EQ(dateStringToEpoch("20230531T000000Z"),
+ usSinceEpoch{1685491200000000});
+
+ // valid datetime format
+ EXPECT_EQ(dateStringToEpoch("20230531"), usSinceEpoch{1685491200000000});
+
// Non zero timezone
EXPECT_EQ(dateStringToEpoch("2021-11-30T22:41:35.123456+04:00"),
usSinceEpoch{1638297695123456});
@@ -203,6 +210,12 @@
// invalid timezone
EXPECT_EQ(dateStringToEpoch("2024-07-01T12:00:00X"), std::nullopt);
+
+ // invalid datetime format
+ EXPECT_EQ(dateStringToEpoch("202305"), std::nullopt);
+
+ // invalid month (13), day (99)
+ EXPECT_EQ(dateStringToEpoch("19991399"), std::nullopt);
}
} // namespace