From: Yehuda Sadeh Date: Fri, 24 May 2013 06:12:35 +0000 (-0700) Subject: utime: add asctime() X-Git-Tag: v0.67-rc1~128^2~102 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2655c1e459197e9c4d863f29ee05a95f9f1f1129;p=ceph.git utime: add asctime() dump time in a useful format Signed-off-by: Yehuda Sadeh --- diff --git a/src/include/utime.h b/src/include/utime.h index f433fff4467..bfbc512263a 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -169,6 +169,33 @@ public: return out; } + // output + ostream& asctime(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); + + char buf[128]; + asctime_r(&bdt, buf); + int len = strlen(buf); + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + out << buf; + } + out.fill(oldfill); + out.unsetf(std::ios::right); + return out; + } + ostream& localtime(ostream& out) const { out.setf(std::ios::right); char oldfill = out.fill();