From: Yehuda Sadeh Date: Tue, 15 Mar 2016 20:30:29 +0000 (-0700) Subject: utime: add gmtime_nsec() X-Git-Tag: v10.1.0~73^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6bfaa7e0b286738fade08c35581f272826325fc4;p=ceph.git utime: add gmtime_nsec() higher precision than gmtime() Signed-off-by: Yehuda Sadeh --- diff --git a/src/include/utime.h b/src/include/utime.h index f11cdb8a70a9..d08d804dac78 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -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);