From 118be25980a7d32801fc5fd30f69252d9163e904 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 7 Jul 2019 11:13:46 +0800 Subject: [PATCH] include/utime: add utime_t<>(duration) to ease the conversion from chrono durations to utime_t Signed-off-by: Kefu Chai --- src/include/utime.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/include/utime.h b/src/include/utime.h index 4a78ba4be6f..04a4945b705 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -81,6 +81,13 @@ public: explicit utime_t(const std::chrono::time_point& t) : utime_t(Clock::to_timespec(t)) {} // forward to timespec ctor + template + explicit utime_t(const std::chrono::duration& dur) { + using common_t = std::common_type_t; + tv.tv_sec = std::max(std::chrono::duration_cast(dur).count(), 0); + tv.tv_nsec = std::max((std::chrono::duration_cast(dur) % + std::chrono::seconds(1)).count(), 0); + } #if defined(WITH_SEASTAR) explicit utime_t(const seastar::lowres_system_clock::time_point& t) { tv.tv_sec = std::chrono::duration_cast( -- 2.39.5