问题
Is there a standard implementation to print std::duration
as a human readable duration?
steady_clock::time_point start = steady_clock::now();
doSomeFoo();
steady_clock::time_point end = steady_clock::now();
std::cout << "Operation took "
<< may_be_std::theMagic(start-end) << std::endl;
Which should print something similar to:
"Operation took 10d:15h:12m:14:s"
or something similar.
回答1:
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
回答2:
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();
}
回答3:
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
回答4:
No, there is no standard implementation. You can get a human readable version of a std::chrono::time_point via std::put_time but not a std::chrono::duration.
回答5:
Here is a templated version which works with all timeunits from chrono.
Credit goes to the responders before me.
template<typename T>
inline std::string format(T timeunit) {
nanoseconds ns = duration_cast<nanoseconds>(timeunit);
std::ostringstream os;
bool foundNonZero = false;
os.fill('0');
typedef duration<int, std::ratio<86400*365>> years;
const auto y = duration_cast<years>(ns);
if (y.count()) {
foundNonZero = true;
os << y.count() << "y:";
ns -= y;
}
typedef duration<int, std::ratio<86400>> days;
const auto d = duration_cast<days>(ns);
if (d.count()) {
foundNonZero = true;
os << d.count() << "d:";
ns -= d;
}
const auto h = duration_cast<hours>(ns);
if (h.count() || foundNonZero) {
foundNonZero = true;
os << h.count() << "h:";
ns -= h;
}
const auto m = duration_cast<minutes>(ns);
if (m.count() || foundNonZero) {
foundNonZero = true;
os << m.count() << "m:";
ns -= m;
}
const auto s = duration_cast<seconds>(ns);
if (s.count() || foundNonZero) {
foundNonZero = true;
os << s.count() << "s:";
ns -= s;
}
const auto ms = duration_cast<milliseconds>(ns);
if (ms.count() || foundNonZero) {
if (foundNonZero) {
os << std::setw(3);
}
os << ms.count() << ".";
ns -= ms;
foundNonZero = true;
}
const auto us = duration_cast<microseconds>(ns);
if (us.count() || foundNonZero) {
if (foundNonZero) {
os << std::setw(3);
}
os << us.count() << ".";
ns -= us;
}
os << std::setw(3) << ns.count() << "ns" ;
return os.str();
}
Output:
59y:325d:20h:33m:19s:008.800.999ns
20d:13h:53m:19s:008.800.999ns
33m:19s:008.800.999ns
1s:000.000.999ns
1.000.099ns
10.000ns
100ns
来源:https://stackoverflow.com/questions/22590821/convert-stdduration-to-human-readable-time