From: Kefu Chai Date: Thu, 7 Jan 2021 03:56:25 +0000 (+0800) Subject: common/ceph_time: add operator<< for signedspan X-Git-Tag: v16.1.0~70^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a64b96dba14df1e61ee6eb449535a6ff4a9d64b3;p=ceph.git 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 --- diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc index 9b85fc88224d..603165efb43c 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 663d05f8feb2..2f99188e439b 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