Skip to content

Commit fb90f72

Browse files
committed
Timestamp test: add chrono duration formatter
1 parent 00e29ea commit fb90f72

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tests/Timestamp.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@
1414

1515
using namespace std;
1616

17+
#if AVCPP_CXX_STANDARD < 20 || __cpp_lib_chrono < 201907L
18+
template<typename Rep, typename Period>
19+
std::ostream& operator<<(std::ostream& ost, const std::chrono::duration<Rep, Period>& dur)
20+
{
21+
std::ostringstream s;
22+
s.flags(ost.flags());
23+
s.imbue(ost.getloc());
24+
s.precision(ost.precision());
25+
26+
s << +dur.count();
27+
28+
// Ref: https://en.cppreference.com/w/cpp/chrono/duration/operator_ltlt.html
29+
if constexpr (std::is_same_v<Period, std::atto>) s << "as"sv;
30+
else if constexpr (std::is_same_v<Period, std::femto>) s << "fs"sv;
31+
else if constexpr (std::is_same_v<Period, std::pico>) s << "fs"sv;
32+
else if constexpr (std::is_same_v<Period, std::nano>) s << "ns"sv;
33+
else if constexpr (std::is_same_v<Period, std::micro>) s << "us"sv;
34+
else if constexpr (std::is_same_v<Period, std::milli>) s << "ms"sv;
35+
else if constexpr (std::is_same_v<Period, std::centi>) s << "cs"sv;
36+
else if constexpr (std::is_same_v<Period, std::deci>) s << "ds"sv;
37+
else if constexpr (std::is_same_v<Period, std::ratio<1>>) s << "s"sv;
38+
else if constexpr (std::is_same_v<Period, std::deca>) s << "das"sv;
39+
else if constexpr (std::is_same_v<Period, std::hecto>) s << "hs"sv;
40+
else if constexpr (std::is_same_v<Period, std::kilo>) s << "ks"sv;
41+
else if constexpr (std::is_same_v<Period, std::mega>) s << "Ms"sv;
42+
else if constexpr (std::is_same_v<Period, std::giga>) s << "Gs"sv;
43+
else if constexpr (std::is_same_v<Period, std::tera>) s << "Ts"sv;
44+
else if constexpr (std::is_same_v<Period, std::peta>) s << "Ps"sv;
45+
else if constexpr (std::is_same_v<Period, std::exa>) s << "Es"sv;
46+
else if constexpr (std::is_same_v<Period, std::ratio<60>>) s << "min"sv;
47+
else if constexpr (std::is_same_v<Period, std::ratio<3600>>) s << "h"sv;
48+
else if constexpr (std::is_same_v<Period, std::ratio<86400>>) s << "d"sv;
49+
else if constexpr (Period::den == 1) s << '[' << Period::num << "]s"sv;
50+
else
51+
s << '[' << Period::num << '/' << Period::num << "]s"sv;
52+
ost << std::move(s).str();
53+
return ost;
54+
}
55+
#endif
56+
1757
TEST_CASE("Core::Timestamp", "Timestamp")
1858
{
1959
SECTION("Overflow operator+(a,b)")

0 commit comments

Comments
 (0)