]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/utime: add utime_t<>(duration)
authorKefu Chai <kchai@redhat.com>
Sun, 7 Jul 2019 03:13:46 +0000 (11:13 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 3 Aug 2019 01:34:50 +0000 (09:34 +0800)
to ease the conversion from chrono durations to utime_t

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/utime.h

index 4a78ba4be6f62c1a88a3e22244d1823a636a3d97..04a4945b7051ba05706607389400626a1bbff286 100644 (file)
@@ -81,6 +81,13 @@ public:
   explicit utime_t(const std::chrono::time_point<Clock>& t)
     : utime_t(Clock::to_timespec(t)) {} // forward to timespec ctor
 
+  template<class Rep, class Period>
+  explicit utime_t(const std::chrono::duration<Rep, Period>& dur) {
+    using common_t = std::common_type_t<Rep, int>;
+    tv.tv_sec = std::max<common_t>(std::chrono::duration_cast<std::chrono::seconds>(dur).count(), 0);
+    tv.tv_nsec = std::max<common_t>((std::chrono::duration_cast<std::chrono::nanoseconds>(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<std::chrono::seconds>(