From: Sage Weil Date: Wed, 19 Dec 2018 04:10:39 +0000 (-0600) Subject: common/ceph_time: add exact_timespan_str X-Git-Tag: v14.1.0~496^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d033f92d1e0b866b666a6cf05b718bf9a0eb4628;p=ceph.git common/ceph_time: add exact_timespan_str e.g., 302 -> "5m2s" Signed-off-by: Sage Weil --- diff --git a/src/common/ceph_time.cc b/src/common/ceph_time.cc index fa56aee19116..b292697bc5eb 100644 --- a/src/common/ceph_time.cc +++ b/src/common/ceph_time.cc @@ -181,4 +181,54 @@ namespace ceph { ss << yr << "y"; return ss.str(); } + + std::string exact_timespan_str(timespan t) + { + uint64_t nsec = std::chrono::nanoseconds(t).count(); + uint64_t sec = nsec / 1000000000; + nsec %= 1000000000; + uint64_t yr = sec / (60 * 60 * 24 * 365); + ostringstream ss; + if (yr) { + ss << yr << "y"; + sec -= yr * (60 * 60 * 24 * 365); + } + /* I don't like the capital M -sage + uint64_t mn = sec / (60 * 60 * 24 * 30); + if (mn >= 3) { + ss << mn << "M"; + sec -= mn * (60 * 60 * 24 * 30); + } + */ + uint64_t wk = sec / (60 * 60 * 24 * 7); + if (wk >= 2) { + ss << wk << "w"; + sec -= wk * (60 * 60 * 24 * 7); + } + uint64_t day = sec / (60 * 60 * 24); + if (day >= 2) { + ss << day << "d"; + sec -= day * (60 * 60 * 24); + } + uint64_t hr = sec / (60 * 60); + if (hr >= 2) { + ss << hr << "h"; + sec -= hr * (60 * 60); + } + uint64_t min = sec / 60; + if (min >= 2) { + ss << min << "m"; + sec -= min * 60; + } + if (sec) { + ss << sec; + } + if (nsec) { + ss << ((float)nsec / 1000000000); + } + if (sec || nsec) { + ss << "s"; + } + return ss.str(); + } } diff --git a/src/common/ceph_time.h b/src/common/ceph_time.h index 8b300e215894..ca9f88b1ec7d 100644 --- a/src/common/ceph_time.h +++ b/src/common/ceph_time.h @@ -487,6 +487,7 @@ inline timespan to_timespan(signedspan z) { } std::string timespan_str(timespan t); +std::string exact_timespan_str(timespan t); // detects presence of Clock::to_timespec() and from_timespec() template >