22 #ifndef LELY_UTIL_CHRONO_HPP_
23 #define LELY_UTIL_CHRONO_HPP_
34 inline ::std::chrono::nanoseconds
36 using ::std::chrono::nanoseconds;
37 using ::std::chrono::seconds;
38 return seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec);
42 template <
class Rep,
class Period>
44 to_timespec(const ::std::chrono::duration<Rep, Period>& d) noexcept {
45 using ::std::chrono::duration_cast;
46 using ::std::chrono::nanoseconds;
47 using ::std::chrono::seconds;
48 auto sec = duration_cast<seconds>(d);
49 if (sec.count() < ::std::numeric_limits<time_t>::min())
50 return timespec{::std::numeric_limits<time_t>::min(), 0};
51 if (sec.count() > ::std::numeric_limits<time_t>::max())
52 return timespec{::std::numeric_limits<time_t>::max(), 0};
53 auto nsec = duration_cast<nanoseconds>(d - sec);
54 return timespec{
static_cast<time_t
>(sec.count()),
56 static_cast<long>(nsec.count())};
63 template <
class Clock,
class Duration>
65 to_timespec(const ::std::chrono::time_point<Clock, Duration>& t) noexcept {
72 #endif // !LELY_UTIL_CHRONO_HPP_