From 73020685ee765c7c577c849db33fd896a14c905d Mon Sep 17 00:00:00 2001 From: Jesse Haber-Kucharsky Date: Wed, 21 Jun 2017 10:46:16 -0400 Subject: [PATCH] Add `time_t` conversions for `gc_clock` `gc_clock` reports system time, and these conversion functions allow for manipulating time points produced by the clock without making assumptions about its epoch. --- gc_clock.hh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gc_clock.hh b/gc_clock.hh index 2d66c6612e..ffc9796fa5 100644 --- a/gc_clock.hh +++ b/gc_clock.hh @@ -42,6 +42,14 @@ public: using duration = std::chrono::duration; using time_point = std::chrono::time_point; + static constexpr std::time_t to_time_t(time_point t) { + return std::chrono::duration_cast(t.time_since_epoch()).count(); + } + + static constexpr time_point from_time_t(std::time_t t) { + return time_point(std::chrono::duration_cast(std::chrono::seconds(t))); + } + static time_point now() { return time_point(std::chrono::duration_cast(base::now().time_since_epoch())) + get_clocks_offset(); }