diff options
author | Ed Tanous <edtanous@google.com> | 2022-07-24 03:32:15 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2022-08-10 18:58:16 +0300 |
commit | 2c98676dca920b736b937ad33004bc05b75a450d (patch) | |
tree | 3dad8aa0a61f5127b064c1436f31f265a2ed49ae /http/utility.hpp | |
parent | 0553fb5703a0020e76bf5eef7dede50b3679ac82 (diff) | |
download | bmcweb-2c98676dca920b736b937ad33004bc05b75a450d.tar.xz |
Support micro and milli resolution on timestamps
Supporting higher precision on our timestamps seems worthwhile, and
useful to logging implementations for getting more accurate numbers from
redfish LogService interfaces.
This commit updates the code to add millisecond and microsecond
precision to timestamps.
Tested: Unit tests pass, pretty good coverage here.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I0914908966702a6cf1bcb59f22761dc24c46b4c3
Diffstat (limited to 'http/utility.hpp')
-rw-r--r-- | http/utility.hpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/http/utility.hpp b/http/utility.hpp index 5ca1dea151..c9728f5b6d 100644 --- a/http/utility.hpp +++ b/http/utility.hpp @@ -636,6 +636,22 @@ std::string toISO8061ExtendedStr(std::chrono::duration<IntType, Period> t) seconds se = duration_cast<seconds>(t); out += details::padZeros(se.count(), 2); + t -= se; + + if constexpr (std::is_same_v<typename decltype(t)::period, std::milli>) + { + out += '.'; + using MilliDuration = std::chrono::duration<int, std::milli>; + MilliDuration subsec = duration_cast<MilliDuration>(t); + out += details::padZeros(subsec.count(), 3); + } + else if constexpr (std::is_same_v<typename decltype(t)::period, std::micro>) + { + out += '.'; + using MicroDuration = std::chrono::duration<int, std::micro>; + MicroDuration subsec = duration_cast<MicroDuration>(t); + out += details::padZeros(subsec.count(), 6); + } out += "+00:00"; return out; @@ -653,7 +669,7 @@ inline std::string getDateTimeUint(uint64_t secondsSinceEpoch) return details::toISO8061ExtendedStr(sinceEpoch); } -// Returns the formatted date time string. +// Returns the formatted date time string with millisecond precision // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if // the given |secondsSinceEpoch| is too large, we return the maximum supported // date. @@ -664,6 +680,14 @@ inline std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch) return details::toISO8061ExtendedStr(sinceEpoch); } +// Returns the formatted date time string with microsecond precision +inline std::string getDateTimeUintUs(uint64_t microSecondsSinceEpoch) +{ + using DurationType = std::chrono::duration<uint64_t, std::micro>; + DurationType sinceEpoch(microSecondsSinceEpoch); + return details::toISO8061ExtendedStr(sinceEpoch); +} + inline std::string getDateTimeStdtime(std::time_t secondsSinceEpoch) { using DurationType = std::chrono::duration<std::time_t>; |