aboutsummaryrefslogtreecommitdiff
path: root/libbutl/timestamp.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-06-08 23:23:07 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-06-14 17:54:26 +0300
commitcf703f3e302312e1602fe46dc2c1e32dee786e6b (patch)
treef5e5fa80faad6f4f87765e3a7fd51c5d65f753b9 /libbutl/timestamp.cxx
parent0f4d52b4e907fcefabf8fd05c9d122da48664b4b (diff)
Add to_stream(duration) and to_string(duration)
Diffstat (limited to 'libbutl/timestamp.cxx')
-rw-r--r--libbutl/timestamp.cxx43
1 files changed, 28 insertions, 15 deletions
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<nanoseconds> (ts - sec));
-
- if (ns != nanoseconds::zero ())
+ if (ns)
{
- if (fmt != nullptr)
+ timestamp sec (system_clock::from_time_t (t));
+ nanoseconds nsec (duration_cast<nanoseconds> (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.