summaryrefslogtreecommitdiffstats
path: root/src/common/ceph_time.cc
diff options
context:
space:
mode:
authorKefu Chai <kchai@redhat.com>2021-01-07 04:56:25 +0100
committerKefu Chai <kchai@redhat.com>2021-01-08 06:57:18 +0100
commita64b96dba14df1e61ee6eb449535a6ff4a9d64b3 (patch)
tree5e1992603d1f014e7951024f6b73f791cc5ba914 /src/common/ceph_time.cc
parentcommon/ceph_time: move operator<<(ostream&, timespan&) into std namespace (diff)
downloadceph-a64b96dba14df1e61ee6eb449535a6ff4a9d64b3.tar.xz
ceph-a64b96dba14df1e61ee6eb449535a6ff4a9d64b3.zip
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 <kchai@redhat.com>
Diffstat (limited to 'src/common/ceph_time.cc')
-rw-r--r--src/common/ceph_time.cc29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc
index 9b85fc88224..603165efb43 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<float>;
- ::fmt::print(m, "{:.9}", chrono::duration_cast<seconds_t>(t));
+template<typename Rep, typename Period>
+ostream& operator<<(ostream& m, const chrono::duration<Rep, Period>& t) {
+ if constexpr (chrono::treat_as_floating_point_v<Rep>) {
+ using seconds_t = chrono::duration<float>;
+ ::fmt::print(m, "{:.9}", chrono::duration_cast<seconds_t>(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<< <chrono::seconds::rep,
+ chrono::seconds::period> (ostream&, const chrono::seconds&);
+
+template ostream&
+operator<< <chrono::milliseconds::rep,
+ chrono::milliseconds::period> (ostream&, const chrono::milliseconds&);
+
} // namespace std