aboutsummaryrefslogtreecommitdiff
path: root/libbutl/timestamp.cxx
diff options
context:
space:
mode:
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.