Lely core libraries 2.3.4
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
30namespace lely {
31namespace util {
32
34inline ::std::chrono::nanoseconds
35from_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
42template <class Rep, class Period>
43inline timespec
44to_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
63template <class Clock, class Duration>
64inline timespec
65to_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_
This header file is part of the compatibility library; it includes <chrono> and defines any missing f...
This header file is part of the C11 and POSIX compatibility library; it includes <time....
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
inline ::std::chrono::nanoseconds from_timespec(const timespec &ts) noexcept
Converts a C11 time interval to a C++11 duration.
Definition: chrono.hpp:35