From fbf53a25702ab40e59cb231974037dc0c31793be Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 7 Jan 2021 11:56:25 +0800 Subject: [PATCH] common/ceph_time: add operator<< for signedspan * templatize operator<<(ostream&, duration<>), so it works for more duration<> classes with minimal efforts -- we just need to explicitly instantiate these template operators * explicitly instantiate operator<< for timespan, signedspan, seconds and milliseconds. they are most likely to be used in Ceph. we can add more of them when necessary. Signed-off-by: Kefu Chai (cherry picked from commit a64b96dba14df1e61ee6eb449535a6ff4a9d64b3) --- src/common/ceph_time.cc | 29 +++++++++++++++++++++++++---- src/common/ceph_time.h | 3 ++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc index 9b85fc88224d3..603165efb43c5 100644 --- a/src/common/ceph_time.cc +++ b/src/common/ceph_time.cc @@ -322,10 +322,31 @@ std::chrono::seconds parse_timespan(const std::string& s) } namespace std { -ostream& operator<<(ostream& m, const ::ceph::timespan& t) { - static_assert(is_unsigned_v<::ceph::timespan::rep>); - using seconds_t = chrono::duration; - ::fmt::print(m, "{:.9}", chrono::duration_cast(t)); +template +ostream& operator<<(ostream& m, const chrono::duration& t) { + if constexpr (chrono::treat_as_floating_point_v) { + using seconds_t = chrono::duration; + ::fmt::print(m, "{:.9}", chrono::duration_cast(t)); + } else { + ::fmt::print(m, "{}", t); + } return m; } + +template ostream& +operator<< <::ceph::timespan::rep, + ::ceph::timespan::period> (ostream&, const ::ceph::timespan&); + +template ostream& +operator<< <::ceph::signedspan::rep, + ::ceph::signedspan::period> (ostream&, const ::ceph::signedspan&); + +template ostream& +operator<< (ostream&, const chrono::seconds&); + +template ostream& +operator<< (ostream&, const chrono::milliseconds&); + } // namespace std diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h index 663d05f8feb2b..2f99188e439b4 100644 --- a/src/common/ceph_time.h +++ b/src/common/ceph_time.h @@ -519,7 +519,8 @@ static Rep to_microseconds(T t) { } // namespace ceph namespace std { -ostream& operator<<(ostream& m, const ::ceph::timespan& t); +template +ostream& operator<<(ostream& m, const chrono::duration& t); } #endif // COMMON_CEPH_TIME_H -- 2.39.5