Convert std::duration to human readable time
Agreed there is no standard implementation. Here is how you can write one yourself:
#include <iostream>
#include <iomanip>
#include <chrono>
std::ostream&
display(std::ostream& os, std::chrono::nanoseconds ns)
{
using namespace std;
using namespace std::chrono;
typedef duration<int, ratio<86400>> days;
char fill = os.fill();
os.fill('0');
auto d = duration_cast<days>(ns);
ns -= d;
auto h = duration_cast<hours>(ns);
ns -= h;
auto m = duration_cast<minutes>(ns);
ns -= m;
auto s = duration_cast<seconds>(ns);
os << setw(2) << d.count() << "d:"
<< setw(2) << h.count() << "h:"
<< setw(2) << m.count() << "m:"
<< setw(2) << s.count() << 's';
os.fill(fill);
return os;
};
int
main()
{
std::cout << "Operation took ";
display(std::cout, std::chrono::microseconds(918734000000));
std::cout << '\n';
}
Operation took 10d:15h:12m:14s
Based on Howard's answer, I wrote this to make sure that only the relevant data is printed out, so 120 seconds becomes 2m00s
instead of 00d:00h:02m00s
, and made sure to strip the leading zero, so its still 2m00s
and not 02m00s
.
Usage is simple:
std::chrono::seconds seconds{60*60*24 + 61};
std::string pretty_seconds = beautify_duration(seconds);
printf("seconds: %s", pretty_seconds.c_str());
>>seconds: 1d00h01m01s
Code:
std::string beautify_duration(std::chrono::seconds input_seconds)
{
using namespace std::chrono;
typedef duration<int, std::ratio<86400>> days;
auto d = duration_cast<days>(input_seconds);
input_seconds -= d;
auto h = duration_cast<hours>(input_seconds);
input_seconds -= h;
auto m = duration_cast<minutes>(input_seconds);
input_seconds -= m;
auto s = duration_cast<seconds>(input_seconds);
auto dc = d.count();
auto hc = h.count();
auto mc = m.count();
auto sc = s.count();
std::stringstream ss;
ss.fill('0');
if (dc) {
ss << d.count() << "d";
}
if (dc || hc) {
if (dc) { ss << std::setw(2); } //pad if second set of numbers
ss << h.count() << "h";
}
if (dc || hc || mc) {
if (dc || hc) { ss << std::setw(2); }
ss << m.count() << "m";
}
if (dc || hc || mc || sc) {
if (dc || hc || mc) { ss << std::setw(2); }
ss << s.count() << 's';
}
return ss.str();
}
Here is a version that allows you to inline a duration with operator<< It only prints what is necessary and allows setting the precision you want:
#include <chrono>
#include <iomanip>
#include <optional>
#include <ostream>
std::ostream& operator<<(std::ostream& os, std::chrono::nanoseconds ns)
{
using namespace std::chrono;
using days = duration<int, std::ratio<86400>>;
auto d = duration_cast<days>(ns);
ns -= d;
auto h = duration_cast<hours>(ns);
ns -= h;
auto m = duration_cast<minutes>(ns);
ns -= m;
auto s = duration_cast<seconds>(ns);
ns -= s;
std::optional<int> fs_count;
switch (os.precision()) {
case 9: fs_count = ns.count();
break;
case 6: fs_count = duration_cast<microseconds>(ns).count();
break;
case 3: fs_count = duration_cast<milliseconds>(ns).count();
break;
}
char fill = os.fill('0');
if (d.count())
os << d.count() << "d ";
if (d.count() || h.count())
os << std::setw(2) << h.count() << ":";
if (d.count() || h.count() || m.count())
os << std::setw(d.count() || h.count() ? 2 : 1) << m.count() << ":";
os << std::setw(d.count() || h.count() || m.count() ? 2 : 1) << s.count();
if (fs_count.has_value())
os << "." << std::setw(os.precision()) << fs_count.value();
if (!d.count() && !h.count() && !m.count())
os << "s";
os.fill(fill);
return os;
}
Here are some usage examples:
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono_literals;
int main()
{
cout << 918734032564785ns << "\n";
cout << setprecision(3) << 918734032564785ns << "\n";
cout << setprecision(9) << 918734032564785ns << "\n";
cout << setprecision(0) << 918734032564785ns << "\n";
cout << setprecision(3) << 432034ms << "\n";
cout << 14h + 32min + 37s + 645ms << "\n";
cout << 86472s << "\n";
cout << 4324ms << "\n";
return 0;
}
Output:
10d 15:12:14.032564
10d 15:12:14.032
10d 15:12:14.032564785
10d 15:12:14
7:12.034
14:32:37.645
1d 00:01:12.000
4.324s