From a24912a9fee0a3771395810c8fe3945bf44c83f8 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 1 May 2016 01:12:59 +0800 Subject: [PATCH] ceph_time: add operator<< for more clock types Signed-off-by: Kefu Chai --- src/common/ceph_time.cc | 23 +++++++++++++++++++---- src/common/ceph_time.h | 10 ++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc index e93dcd97206..d7b512b0ac8 100644 --- a/src/common/ceph_time.cc +++ b/src/common/ceph_time.cc @@ -72,7 +72,10 @@ namespace ceph { using std::chrono::seconds; using std::chrono::microseconds; - std::ostream& operator<<(std::ostream& m, const mono_time& t) { + template::type*> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t) { return m << std::chrono::duration(t.time_since_epoch()).count() << "s"; } @@ -81,14 +84,17 @@ namespace ceph { return m << std::chrono::duration(t).count() << "s"; } - std::ostream& operator<<(std::ostream& m, const real_time& t) { + template::type*> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t) { m.setf(std::ios::right); char oldfill = m.fill(); m.fill('0'); // localtime. this looks like an absolute time. // aim for http://en.wikipedia.org/wiki/ISO_8601 struct tm bdt; - time_t tt = ceph::real_clock::to_time_t(t); + time_t tt = Clock::to_time_t(t); localtime_r(&tt, &bdt); m << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' << '-' << std::setw(2) << (bdt.tm_mon+1) @@ -103,4 +109,13 @@ namespace ceph { m.unsetf(std::ios::right); return m; } -}; + + template std::ostream& + operator<< (std::ostream& m, const mono_time& t); + template std::ostream& + operator<< (std::ostream& m, const real_time& t); + template std::ostream& + operator<< (std::ostream& m, const coarse_mono_time& t); + template std::ostream& + operator<< (std::ostream& m, const coarse_real_time& t); +} diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h index 1cfb6b8eeb0..e5197ba96ca 100644 --- a/src/common/ceph_time.h +++ b/src/common/ceph_time.h @@ -403,8 +403,14 @@ namespace ceph { } std::ostream& operator<<(std::ostream& m, const timespan& t); - std::ostream& operator<<(std::ostream& m, const real_time& t); - std::ostream& operator<<(std::ostream& m, const mono_time& t); + template::type* = nullptr> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t); + template::type* = nullptr> + std::ostream& operator<<(std::ostream& m, + const std::chrono::time_point& t); // The way std::chrono handles the return type of subtraction is not // wonderful. The difference of two unsigned types SHOULD be signed. -- 2.39.5