From 24e05cd878553ad2379d513951f06ec984b08594 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Jan 2016 10:00:32 +0200 Subject: Generalize timestamp printing code --- butl/timestamp | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'butl/timestamp') diff --git a/butl/timestamp b/butl/timestamp index 77af4f1..e68a59f 100644 --- a/butl/timestamp +++ b/butl/timestamp @@ -30,7 +30,7 @@ namespace butl // using std::chrono::system_clock; - // Note that uninitialized timestamp has the timestamp_nonexistent + // Note that the default-initialized timestamp has the timestamp_nonexistent // value. // using timestamp = system_clock::time_point; @@ -41,11 +41,45 @@ namespace butl const timestamp timestamp_unknown {duration {-1}}; const timestamp timestamp_nonexistent {duration {0}}; - // Human-readable representation. Note that these operators - // may throw std::system_error. + // Human-readable representation. By default the timestamp is printed by + // localtime_r() in the local timezone, so tzset() from should be + // called prior to using the corresponding operator or the to_stream() + // function (normally from main() or equivalent). + // + // The format argument in the to_stream() function is the put_time() format + // string except that it also supports the nanoseconds conversion specifier + // in the form %[N] where is the optional single delimiter character, + // for example '.'. If the nanoseconds part is 0, then it is not printed (nor + // the delimiter character). + // + // The special argument in the to_stream() function indicates whether the + // special timestamp_unknown and timestamp_nonexistent values should be + // printed as '' and '', respectively. + // + // The local argument in the to_stream() function indicates whether to use + // localtime_r() or gmtime_r(). + // + // Note also that these operators/function may throw std::system_error. + // + // Potential improvements: + // - add flag to to_stream() to use + // - support %[U] (microseconds) and %[M] (milliseconds). + // - make to_stream() a manipulator, similar to put_time() + // - support %(N) version for non-optional printing + // - support for suffix %[N], for example %[N nsec] // std::ostream& - operator<< (std::ostream&, const timestamp&); + to_stream (std::ostream&, + const timestamp&, + const char* format, + bool special, + bool local); + + inline std::ostream& + operator<< (std::ostream& os, const timestamp& ts) + { + return to_stream (os, ts, "%Y-%m-%d %H:%M:%S%[.N]", true, true); + } std::ostream& operator<< (std::ostream&, const duration&); -- cgit v1.1