diff options
author | Yehuda Sadeh <yehuda@inktank.com> | 2013-05-23 23:12:35 -0700 |
---|---|---|
committer | Yehuda Sadeh <yehuda@inktank.com> | 2013-05-23 23:13:01 -0700 |
commit | 2655c1e459197e9c4d863f29ee05a95f9f1f1129 (patch) | |
tree | 3111d69208f7557e854b2d3b406f863a0a2c1736 | |
parent | 0c805b6c771bff259239458ccd5431543005c842 (diff) | |
download | ceph-2655c1e459197e9c4d863f29ee05a95f9f1f1129.tar.gz |
utime: add asctime()
dump time in a useful format
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
-rw-r--r-- | src/include/utime.h | 27 |
1 files changed, 27 insertions, 0 deletions
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(); |