]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
utime: add gmtime_nsec()
authorYehuda Sadeh <yehuda@redhat.com>
Tue, 15 Mar 2016 20:30:29 +0000 (13:30 -0700)
committerYehuda Sadeh <yehuda@redhat.com>
Tue, 15 Mar 2016 20:30:29 +0000 (13:30 -0700)
higher precision than gmtime()

Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/include/utime.h

index f11cdb8a70a98dcb063920861ea65c0d6becb439..d08d804dac788578f02e60935fc666540cb26e6b 100644 (file)
@@ -210,6 +210,35 @@ public:
     return out;
   }
 
+  // output
+  ostream& gmtime_nsec(ostream& out) const {
+    out.setf(std::ios::right);
+    char oldfill = out.fill();
+    out.fill('0');
+    if (sec() < ((time_t)(60*60*24*365*10))) {
+      // raw seconds.  this looks like a relative time.
+      out << (long)sec() << "." << std::setw(6) << usec();
+    } else {
+      // localtime.  this looks like an absolute time.
+      //  aim for http://en.wikipedia.org/wiki/ISO_8601
+      struct tm bdt;
+      time_t tt = sec();
+      gmtime_r(&tt, &bdt);
+      out << std::setw(4) << (bdt.tm_year+1900)  // 2007 -> '07'
+         << '-' << std::setw(2) << (bdt.tm_mon+1)
+         << '-' << std::setw(2) << bdt.tm_mday
+         << ' '
+         << std::setw(2) << bdt.tm_hour
+         << ':' << std::setw(2) << bdt.tm_min
+         << ':' << std::setw(2) << bdt.tm_sec;
+      out << "." << std::setw(9) << nsec();
+      out << "Z";
+    }
+    out.fill(oldfill);
+    out.unsetf(std::ios::right);
+    return out;
+  }
+
   // output
   ostream& asctime(ostream& out) const {
     out.setf(std::ios::right);