Created
May 21, 2021 10:05
-
-
Save JoaoBaptMG/d907baa8df2c76970f7ab063c7ad9fb7 to your computer and use it in GitHub Desktop.
Chrono ostream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename Period2, typename Rep, typename Period> | |
constexpr auto fractional_count(const std::chrono::duration<Rep, Period>& duration) | |
{ | |
return std::chrono::duration<double, Period2>(duration).count(); | |
} | |
template <typename Rep, typename Period> | |
std::ostream& operator<<(std::ostream& out, const std::chrono::duration<Rep, Period>& duration) | |
{ | |
namespace ch = std::chrono; | |
if (ch::duration_cast<ch::seconds>(duration).count() > 0) | |
return out << fractional_count<std::ratio<1>>(duration) << "s"; | |
else if (ch::duration_cast<ch::milliseconds>(duration).count() > 0) | |
return out << fractional_count<std::milli>(duration) << "ms"; | |
else if (ch::duration_cast<ch::microseconds>(duration).count() > 0) | |
return out << fractional_count<std::micro>(duration) << "us"; | |
else if (ch::duration_cast<ch::nanoseconds>(duration).count() > 0) | |
return out << fractional_count<std::nano>(duration) << "ns"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment