I needed to print std::chrono::duration
that may be greater than 24 hours in easily-readable way, like "2 d. 09:10:11"
.
C++20's std::chrono::time_point
and std::chrono::duration
formatting (based on Howard Hinnant's date library) contains tons of useful format specifiers but does not have one for number of days in std::chrono::duration
. The closest one is %j
but it pads it with zeros. Also there is no specifier for number of hours within the day. The closest are %H
(but it includes whole days hours too) and %OH
(but it crashes on assertion that number of hours is less than 24).
Am I missing something?
Does it make sense to add more specifiers to C++20's std::format
?
So far I'm using workaround function:
#include <chrono>
#include <cstring> // std::strncmp
#include <date.h> // use Howard Hinnant's date library until C++20 is supported by compilers
#include <string>
// Workaround for C++20 std::chrono::format's inability to produce number of days in std::chrono::duration.
// Temporary format specifier for this is `%J`. It must be in the beginning of the format string.
template<typename Rep, typename Period>
std::string duration_to_str(const char* format, const std::chrono::duration<Rep, Period>& duration)
{
if (std::strncmp(format, "%J", 2))
return date::format(format, duration);
static const auto one_day = std::chrono::hours(24);
const size_t days = std::chrono::duration_cast<std::chrono::hours>(duration).count() / 24;
return std::to_string(days) + date::format(format + 2, duration - days * one_day);
}
Test:
#include <iostream>
template<typename Rep, typename Period>
void test_dur_to_str(const std::chrono::duration<Rep, Period>& duration)
{
std::cout << duration_to_str("%T", duration) << ":\n"
<< duration_to_str("%j d. %T", duration) << '\n'
<< duration_to_str("%J d. %T", duration) << "\n\n";
}
int main()
{
test_dur_to_str(std::chrono::seconds(24 * 60 * 60 - 1));
test_dur_to_str(std::chrono::seconds(24 * 60 * 60));
test_dur_to_str(std::chrono::seconds(91297));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60 - 1));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60 + 1));
test_dur_to_str(std::chrono::seconds(13 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(365 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(366 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(367 * 24 * 60 * 60 + 5));
}
Output:
23:59:59:
000 d. 23:59:59
0 d. 23:59:59
24:00:00:
001 d. 24:00:00
1 d. 00:00:00
25:21:37:
001 d. 25:21:37
1 d. 01:21:37
47:59:59:
001 d. 47:59:59
1 d. 23:59:59
48:00:00:
002 d. 48:00:00
2 d. 00:00:00
48:00:01:
002 d. 48:00:01
2 d. 00:00:01
312:00:05:
013 d. 312:00:05
13 d. 00:00:05
8760:00:05:
365 d. 8760:00:05
365 d. 00:00:05
8784:00:05:
366 d. 8784:00:05
366 d. 00:00:05
8808:00:05:
367 d. 8808:00:05
367 d. 00:00:05
I want to show simplified version of workaround function, as suggested by Howard Hinnant in his comment, for those who may find it useful:
#include <chrono>
#include <cstring> // std::strncmp
#include <date.h> // use Howard Hinnant's date library until C++20 is supported by compilers
#include <string>
template<typename Rep, typename Period>
std::string duration_to_str(const char* format, const std::chrono::duration<Rep, Period>& duration)
{
if (std::strncmp(format, "%J", 2))
return date::format(format, duration);
const auto days = std::chrono::duration_cast<date::days>(duration);
return std::to_string(days.count()) + date::format(format + 2, duration - days);
}