From 1b47623484e25ba414c3eb3604f1659c4808914c Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 7 Aug 2014 20:12:19 -0300 Subject: [PATCH] Timestamps can now be constructed from std::chrono::duration. --- include/timestamp.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/timestamp.h b/include/timestamp.h index 027796c..6911767 100644 --- a/include/timestamp.h +++ b/include/timestamp.h @@ -72,6 +72,22 @@ public: * Default constructs the timestamp. */ Timestamp() : tv() {} + + #if TINS_IS_CXX11 + /** + * Constructs a Timestamp from a std::chrono::duration. + */ + template + Timestamp(const std::chrono::duration& ts) { + using std::chrono::duration_cast; + using std::chrono::microseconds; + using std::chrono::seconds; + + tv.tv_sec = duration_cast(ts).count(); + tv.tv_usec = duration_cast( + ts - seconds(tv.tv_sec)).count(); + } + #endif /** * Constructs a timestamp from a timeval object.