From: Sage Weil Date: Fri, 8 Jun 2018 02:46:52 +0000 (-0500) Subject: common/ceph_time: very lame timespan_str() helper X-Git-Tag: v14.0.1~1131^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b4efd7362c75531085184c12b75deb81e6bded09;p=ceph.git common/ceph_time: very lame timespan_str() helper Signed-off-by: Sage Weil --- diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc index a5c630a17865..fa56aee19116 100644 --- a/src/common/ceph_time.cc +++ b/src/common/ceph_time.cc @@ -21,6 +21,8 @@ #include #include +#include + #ifndef NSEC_PER_SEC #define NSEC_PER_SEC 1000000000ULL #endif @@ -134,4 +136,49 @@ namespace ceph { operator<< (std::ostream& m, const coarse_mono_time& t); template std::ostream& operator<< (std::ostream& m, const coarse_real_time& t); + + std::string timespan_str(timespan t) + { + // FIXME: somebody pretty please make a version of this function + // that isn't as lame as this one! + uint64_t nsec = std::chrono::nanoseconds(t).count(); + ostringstream ss; + if (nsec < 2000000000) { + ss << ((float)nsec / 1000000000) << "s"; + return ss.str(); + } + uint64_t sec = nsec / 1000000000; + if (sec < 120) { + ss << sec << "s"; + return ss.str(); + } + uint64_t min = sec / 60; + if (min < 120) { + ss << min << "m"; + return ss.str(); + } + uint64_t hr = min / 60; + if (hr < 48) { + ss << hr << "h"; + return ss.str(); + } + uint64_t day = hr / 24; + if (day < 14) { + ss << day << "d"; + return ss.str(); + } + uint64_t wk = day / 7; + if (wk < 12) { + ss << wk << "w"; + return ss.str(); + } + uint64_t mn = day / 30; + if (mn < 24) { + ss << mn << "M"; + return ss.str(); + } + uint64_t yr = day / 365; + ss << yr << "y"; + return ss.str(); + } } diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h index f6ea4014da9a..999c988eb468 100644 --- a/src/common/ceph_time.h +++ b/src/common/ceph_time.h @@ -17,6 +17,7 @@ #include #include +#include #include #include "include/assert.h" @@ -469,6 +470,8 @@ inline timespan to_timespan(signedspan z) { return std::chrono::duration_cast(z); } +std::string timespan_str(timespan t); + // detects presence of Clock::to_timespec() and from_timespec() template > struct converts_to_timespec : std::false_type {};