Skip to content

Commit 72bf0e9

Browse files
committed
util: Add FormatISO8601Time function for time-only ISO format
Adds a new utility function FormatISO8601Time that formats a Unix timestamp into an ISO 8601 time string (HH:MM:SSZ). This complements the existing FormatISO8601DateTime and FormatISO8601Date functions, providing a way to format just the time component of a timestamp. The function uses C++20 chrono features to handle time calculations and formatting, maintaining consistency with the existing ISO 8601 formatting functions.
1 parent 1aff5ee commit 72bf0e9

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/util/time.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ void MockableSteadyClock::ClearMockTime()
7575

7676
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
7777

78+
std::string FormatISO8601Time(int64_t nTime)
79+
{
80+
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
81+
const auto days{std::chrono::floor<std::chrono::days>(secs)};
82+
const std::chrono::hh_mm_ss hms{secs - days};
83+
return strprintf("%02i:%02i:%02iZ", hms.hours().count(), hms.minutes().count(), hms.seconds().count());
84+
}
85+
7886
std::string FormatISO8601DateTime(int64_t nTime)
7987
{
8088
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};

src/util/time.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ T GetTime()
130130
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}
131131
* helper functions if possible.
132132
*/
133+
std::string FormatISO8601Time(int64_t nTime);
133134
std::string FormatISO8601DateTime(int64_t nTime);
134135
std::string FormatISO8601Date(int64_t nTime);
135136
std::optional<int64_t> ParseISO8601DateTime(std::string_view str);

0 commit comments

Comments
 (0)