From 2a32f75a240f78535623b528091839b9b71dcfc9 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sat, 5 Jun 2021 21:48:00 +0200 Subject: [PATCH] Time: Implement duration_cast between timeval and std::chrono::duration This will allow us to migrate step-by-step from timeval and related types to std::chrono. --- src/zm_analysis_thread.cpp | 2 +- src/zm_time.h | 60 +++++++++++++++++++++++++++++++++++++- src/zm_utils.h | 9 ------ 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index 1836cf6b1..7d803c16b 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -2,7 +2,7 @@ #include "zm_monitor.h" #include "zm_signal.h" -#include "zm_utils.h" +#include "zm_time.h" AnalysisThread::AnalysisThread(Monitor *monitor) : monitor_(monitor), terminate_(false) { diff --git a/src/zm_time.h b/src/zm_time.h index 5a54bea46..fbfe71c53 100644 --- a/src/zm_time.h +++ b/src/zm_time.h @@ -20,7 +20,8 @@ #ifndef ZM_TIME_H #define ZM_TIME_H -#include +#include "zm_logger.h" +#include #include // Structure used for storing the results of the subtraction @@ -205,4 +206,61 @@ inline struct timeval tvMake( time_t sec, suseconds_t usec ) return( t ); } +typedef std::chrono::microseconds Microseconds; +typedef std::chrono::milliseconds Milliseconds; +typedef std::chrono::seconds Seconds; +typedef std::chrono::minutes Minutes; +typedef std::chrono::hours Hours; + +typedef std::chrono::steady_clock::time_point TimePoint; +typedef std::chrono::system_clock::time_point SystemTimePoint; + +namespace zm { +namespace chrono { +namespace impl { + +template +struct posix_duration_cast; + +// chrono -> timeval caster +template +struct posix_duration_cast, timeval> { + static timeval cast(std::chrono::duration const &d) { + timeval tv = {}; + + Seconds const sec = std::chrono::duration_cast(d); + + tv.tv_sec = sec.count(); + tv.tv_usec = std::chrono::duration_cast(d - sec).count(); + + return tv; + } +}; + +// timeval -> chrono caster +template +struct posix_duration_cast> { + static std::chrono::duration cast(timeval const &tv) { + return std::chrono::duration_cast>( + Seconds(tv.tv_sec) + Microseconds(tv.tv_usec) + ); + } +}; + +} +// chrono -> timeval +template +auto duration_cast(std::chrono::duration const &d) +-> typename std::enable_if::value, timeval>::type { + return impl::posix_duration_cast, timeval>::cast(d); +} + +// timeval -> chrono +template +Duration duration_cast(timeval const &tv) { + return impl::posix_duration_cast::cast(tv); +} +} +} + #endif // ZM_TIME_H diff --git a/src/zm_utils.h b/src/zm_utils.h index 8ca58a22b..2d5ffb58e 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -136,15 +136,6 @@ template constexpr std::size_t size(const T(&)[N]) noexcept { return N; } } -typedef std::chrono::microseconds Microseconds; -typedef std::chrono::milliseconds Milliseconds; -typedef std::chrono::seconds Seconds; -typedef std::chrono::minutes Minutes; -typedef std::chrono::hours Hours; - -typedef std::chrono::steady_clock::time_point TimePoint; -typedef std::chrono::system_clock::time_point SystemTimePoint; - std::string UriDecode(const std::string &encoded); class QueryParameter {