]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: format duration using libfmt
authorKefu Chai <kchai@redhat.com>
Thu, 7 Jan 2021 03:55:48 +0000 (11:55 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 8 Jan 2021 05:57:17 +0000 (13:57 +0800)
for better readability.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/ceph_time.cc

index c450ef9af42184b6804e26abb493ef1942c7cb11..aa72f65cc7023f49a69f20672c4150d6fa2922e5 100644 (file)
 
 // For ceph_timespec
 #include "ceph_time.h"
+
+#include <fmt/chrono.h>
+#include <fmt/ostream.h>
+
 #include "log/LogClock.h"
 #include "config.h"
 #include "strtol.h"
@@ -104,14 +108,9 @@ std::ostream& operator<<(std::ostream& m,
 
 std::ostream& operator<<(std::ostream& m, const timespan& t) {
   static_assert(std::is_unsigned_v<timespan::rep>);
-  m << std::chrono::duration_cast<std::chrono::seconds>(t).count();
-  if (auto ns = (t % 1s).count(); ns > 0) {
-    char oldfill = m.fill();
-    m.fill('0');
-    m << '.' << std::setw(9) << ns;
-    m.fill(oldfill);
-  }
-  return m << 's';
+  using seconds_t = std::chrono::duration<float>;
+  fmt::print(m, "{:.9}", std::chrono::duration_cast<seconds_t>(t));
+  return m;
 }
 
 template<typename Clock,