aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/timestamp.cxx43
-rw-r--r--libbutl/timestamp.hxx18
2 files changed, 44 insertions, 17 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.
diff --git a/libbutl/timestamp.hxx b/libbutl/timestamp.hxx
index 9fb0f59..8a5c71a 100644
--- a/libbutl/timestamp.hxx
+++ b/libbutl/timestamp.hxx
@@ -88,7 +88,7 @@ namespace butl
bool special,
bool local);
- // Same as above, but provide the result as string. Note that it is
+ // Same as above, but provide the result as a string. Note that it is
// implemented via to_stream() and std::ostringstream.
//
LIBBUTL_EXPORT std::string
@@ -103,8 +103,22 @@ namespace butl
return to_stream (os, ts, "%Y-%m-%d %H:%M:%S%[.N]", true, true);
}
+ // Print human-readable representation of the duration.
+ //
LIBBUTL_EXPORT std::ostream&
- operator<< (std::ostream&, const duration&);
+ to_stream (std::ostream&, const duration&, bool nanoseconds);
+
+ // Same as above, but provide the result as a string. Note that it is
+ // implemented via to_stream() and std::ostringstream.
+ //
+ LIBBUTL_EXPORT std::string
+ to_string (const duration&, bool nanoseconds);
+
+ inline std::ostream&
+ operator<< (std::ostream& os, const duration& d)
+ {
+ return to_stream (os, d, true);
+ }
// Parse human-readable representation of the timestamp.
//