]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: add exact_timespan_str
authorSage Weil <sage@redhat.com>
Wed, 19 Dec 2018 04:10:39 +0000 (22:10 -0600)
committerSage Weil <sage@redhat.com>
Wed, 19 Dec 2018 19:24:39 +0000 (13:24 -0600)
e.g., 302 -> "5m2s"

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/ceph_time.cc
src/common/ceph_time.h

index fa56aee191163f2a111bca38225074aece0e7ed8..b292697bc5eb7bdb1360921eb4c67a92c6fa3fe4 100644 (file)
@@ -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();
+  }
 }
index 8b300e2158949139bcc56237cce7c7ea748007f1..ca9f88b1ec7dc46653eb951d1559784c5d8e6b2a 100644 (file)
@@ -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 <typename Clock, typename = std::void_t<>>