From cf703f3e302312e1602fe46dc2c1e32dee786e6b Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 8 Jun 2017 23:23:07 +0300 Subject: Add to_stream(duration) and to_string(duration) --- libbutl/timestamp.cxx | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'libbutl/timestamp.cxx') diff --git a/libbutl/timestamp.cxx b/libbutl/timestamp.cxx index cf84dd5..aae4622 100644 --- a/libbutl/timestamp.cxx +++ b/libbutl/timestamp.cxx @@ -232,7 +232,7 @@ namespace butl } ostream& - operator<< (ostream& os, const duration& d) + to_stream (ostream& os, const duration& d, bool ns) { if (os.width () != 0) // We always print nanosecond. throw runtime_error ( @@ -300,29 +300,42 @@ namespace butl using namespace chrono; - timestamp sec (system_clock::from_time_t (t)); - nanoseconds ns (duration_cast (ts - sec)); - - if (ns != nanoseconds::zero ()) + if (ns) { - if (fmt != nullptr) + timestamp sec (system_clock::from_time_t (t)); + nanoseconds nsec (duration_cast (ts - sec)); + + if (nsec != nanoseconds::zero ()) { - ostream::fmtflags fl (os.flags ()); - char fc (os.fill ('0')); - os << '.' << dec << right << setw (9) << ns.count (); - os.fill (fc); - os.flags (fl); - } - else - os << ns.count (); + if (fmt != nullptr) + { + ostream::fmtflags fl (os.flags ()); + char fc (os.fill ('0')); + os << '.' << dec << right << setw (9) << nsec.count (); + os.fill (fc); + os.flags (fl); + } + else + os << nsec.count (); - os << ' ' << unt; + os << ' ' << unt; + } + else if (fmt == nullptr) + os << '0'; } else if (fmt == nullptr) os << '0'; return os; } + + string + to_string (const duration& d, bool nsec) + { + ostringstream o; + to_stream (o, d, nsec); + return o.str (); + } } // Implementation of strptime() and timegm() for Windows. -- cgit v1.1