]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: very lame timespan_str() helper
authorSage Weil <sage@redhat.com>
Fri, 8 Jun 2018 02:46:52 +0000 (21:46 -0500)
committerSage Weil <sage@redhat.com>
Mon, 11 Jun 2018 12:29:04 +0000 (07:29 -0500)
Signed-off-by: Sage Weil <sage@redhat.com>
src/common/ceph_time.cc
src/common/ceph_time.h

index a5c630a17865fdfe545a8c843a1bbc5778887032..fa56aee191163f2a111bca38225074aece0e7ed8 100644 (file)
@@ -21,6 +21,8 @@
 #include <mach/mach.h>
 #include <mach/mach_time.h>
 
+#include <ostringstream>
+
 #ifndef NSEC_PER_SEC
 #define NSEC_PER_SEC 1000000000ULL
 #endif
@@ -134,4 +136,49 @@ namespace ceph {
   operator<< <coarse_mono_clock>(std::ostream& m, const coarse_mono_time& t);
   template std::ostream&
   operator<< <coarse_real_clock>(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();
+  }
 }
index f6ea4014da9ab0ad66e64a6f455cbe9641aa0b4f..999c988eb468ec5ed944d0b160499ef32e008a00 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <chrono>
 #include <iostream>
+#include <string>
 #include <sys/time.h>
 
 #include "include/assert.h"
@@ -469,6 +470,8 @@ inline timespan to_timespan(signedspan z) {
   return std::chrono::duration_cast<timespan>(z);
 }
 
+std::string timespan_str(timespan t);
+
 // detects presence of Clock::to_timespec() and from_timespec()
 template <typename Clock, typename = std::void_t<>>
 struct converts_to_timespec : std::false_type {};