From 154b17d5f3bd47025214be96b8c3e62588f47744 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 6 Jun 2021 14:41:56 +0200 Subject: [PATCH] Time: Implement duration_cast from DeltaTimeval to std::chrono::duration This will allow us to migrate step-by-step from DeltaTimeval to std::chrono. --- src/zm_time.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/zm_time.h b/src/zm_time.h index fbfe71c53..e9f04a861 100644 --- a/src/zm_time.h +++ b/src/zm_time.h @@ -247,7 +247,28 @@ struct posix_duration_cast> { } }; +// chrono -> DeltaTimeval caster +template +struct posix_duration_cast, DeltaTimeval> { + template + static DeltaTimeval cast(std::chrono::duration const &d) { + typedef std::chrono::duration> fsec_t; + DeltaTimeval res = {}; + + Seconds secs = std::chrono::duration_cast(d); + fsec_t fsec = std::chrono::duration_cast>(d - secs); + + res.positive = fsec >= Seconds::zero(); + res.delta = abs((secs + fsec).count()); + res.sec = secs.count(); + res.fsec = fsec.count(); + res.prec = Prec; + + return res; + } +}; } + // chrono -> timeval template auto duration_cast(std::chrono::duration const &d) @@ -260,6 +281,13 @@ template Duration duration_cast(timeval const &tv) { return impl::posix_duration_cast::cast(tv); } + +// chrono -> DeltaTimeval +template +auto duration_cast(std::chrono::duration const &d) +-> typename std::enable_if::value, DeltaTimeval>::type { + return impl::posix_duration_cast, DeltaTimeval>::template cast(d); +} } }