]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_time: stringify in ISO 8601 format
authorSage Weil <sage@redhat.com>
Thu, 25 Apr 2019 22:17:45 +0000 (17:17 -0500)
committerSage Weil <sage@redhat.com>
Wed, 29 May 2019 19:12:01 +0000 (14:12 -0500)
Signed-off-by: Sage Weil <sage@redhat.com>
src/common/ceph_time.cc
src/test/common/test_time.cc

index f097e8142521de0386132a599b22ff078fb45a17..7b5d52415dcb1e106d3afc55e1087005022ae52e 100644 (file)
@@ -111,19 +111,22 @@ namespace ceph {
     char oldfill = m.fill();
     m.fill('0');
     // localtime.  this looks like an absolute time.
-    //  aim for http://en.wikipedia.org/wiki/ISO_8601
+    //  conform to http://en.wikipedia.org/wiki/ISO_8601
     struct tm bdt;
     time_t tt = Clock::to_time_t(t);
     localtime_r(&tt, &bdt);
+    char tz[32] = { 0 };
+    strftime(tz, sizeof(tz), "%z", &bdt);
     m << std::setw(4) << (bdt.tm_year+1900)  // 2007 -> '07'
       << '-' << std::setw(2) << (bdt.tm_mon+1)
       << '-' << std::setw(2) << bdt.tm_mday
-      << ' '
+      << 'T'
       << std::setw(2) << bdt.tm_hour
       << ':' << std::setw(2) << bdt.tm_min
       << ':' << std::setw(2) << bdt.tm_sec
       << "." << std::setw(6) << duration_cast<microseconds>(
-       t.time_since_epoch() % seconds(1));
+       t.time_since_epoch() % seconds(1)).count()
+      << tz;
     m.fill(oldfill);
     m.unsetf(std::ios::right);
     return m;
index 3ab2d8d8f562e380d363b0e0a9b33e627f33cef7..a643337d4ae7f6ae42c3ac6e835c6d0531daf7ef 100644 (file)
@@ -18,6 +18,7 @@
 #include "common/ceph_time.h"
 #include "include/rados.h"
 #include "gtest/gtest.h"
+#include "include/stringify.h"
 
 
 using ceph::real_clock;
@@ -176,3 +177,18 @@ TEST(TimePoints, SignedSubtraciton) {
   ASSERT_GT(cmtb - cmta, ceph::signedspan::zero());
   ASSERT_GT((cmtb - cmta).count(), 0);
 }
+
+TEST(TimePoints, stringify) {
+  ceph::real_clock::time_point tp(seconds(1556122013) + nanoseconds(39923122));
+  string s = stringify(tp);
+  ASSERT_EQ(s.size(), strlen("2019-04-24T11:06:53.039923-0500"));
+  ASSERT_TRUE(s[26] == '-' || s[26] == '+');
+  ASSERT_EQ(s.substr(0, 9), "2019-04-2");
+
+  ceph::coarse_real_clock::time_point ctp(seconds(1556122013) +
+                                         nanoseconds(399000000));
+  s = stringify(ctp);
+  ASSERT_EQ(s.size(), strlen("2019-04-24T11:06:53.399000-0500"));
+  ASSERT_TRUE(s[26] == '-' || s[26] == '+');
+  ASSERT_EQ(s.substr(0, 9), "2019-04-2");
+}