Lely core libraries  2.2.5
chrono.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_UTIL_CHRONO_HPP_
23 #define LELY_UTIL_CHRONO_HPP_
24 
25 #include <lely/libc/chrono.hpp>
26 #include <lely/libc/time.h>
27 
28 #include <limits>
29 
30 namespace lely {
31 namespace util {
32 
34 inline ::std::chrono::nanoseconds
35 from_timespec(const timespec& ts) noexcept {
36  using ::std::chrono::nanoseconds;
37  using ::std::chrono::seconds;
38  return seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec);
39 }
40 
42 template <class Rep, class Period>
43 inline timespec
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()),
55  // NOLINTNEXTLINE(runtime/int)
56  static_cast<long>(nsec.count())};
57 }
58 
63 template <class Clock, class Duration>
64 inline timespec
65 to_timespec(const ::std::chrono::time_point<Clock, Duration>& t) noexcept {
66  return to_timespec(t.time_since_epoch());
67 }
68 
69 } // namespace util
70 } // namespace lely
71 
72 #endif // !LELY_UTIL_CHRONO_HPP_
chrono.hpp
lely::util::from_timespec
inline ::std::chrono::nanoseconds from_timespec(const timespec &ts) noexcept
Converts a C11 time interval to a C++11 duration.
Definition: chrono.hpp:35
lely::util::to_timespec
timespec to_timespec(const ::std::chrono::duration< Rep, Period > &d) noexcept
Converts a C++11 duration to a C11 time interval.
Definition: chrono.hpp:44
time.h