John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_TIME_H_ |
| 2 | #define AOS_TIME_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 5 | #include <sys/time.h> |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 6 | #include <time.h> |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 7 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 8 | #include <chrono> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | #include <ostream> |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 10 | #include <thread> |
| 11 | #include <type_traits> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 13 | #include "aos/macros.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 14 | #include "aos/type_traits/type_traits.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | |
| 16 | namespace aos { |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 17 | |
| 18 | class monotonic_clock { |
| 19 | public: |
| 20 | typedef ::std::chrono::nanoseconds::rep rep; |
| 21 | typedef ::std::chrono::nanoseconds::period period; |
| 22 | typedef ::std::chrono::nanoseconds duration; |
| 23 | typedef ::std::chrono::time_point<monotonic_clock> time_point; |
| 24 | |
| 25 | static monotonic_clock::time_point now() noexcept; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 26 | // This clock is still subject to rate adjustments based on adjtime, so it is |
| 27 | // not steady. |
| 28 | static constexpr bool is_steady = false; |
| 29 | |
| 30 | // Returns the epoch (0). |
| 31 | static constexpr monotonic_clock::time_point epoch() { |
| 32 | return time_point(zero()); |
| 33 | } |
| 34 | |
| 35 | static constexpr monotonic_clock::duration zero() { return duration(0); } |
| 36 | |
| 37 | static constexpr time_point min_time{ |
| 38 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 39 | static constexpr time_point max_time{ |
| 40 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class realtime_clock { |
| 44 | public: |
| 45 | typedef ::std::chrono::nanoseconds::rep rep; |
| 46 | typedef ::std::chrono::nanoseconds::period period; |
| 47 | typedef ::std::chrono::nanoseconds duration; |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame^] | 48 | typedef ::std::chrono::time_point<realtime_clock> time_point; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 49 | |
| 50 | #ifdef __linux__ |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame^] | 51 | static realtime_clock::time_point now() noexcept; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 52 | #endif // __linux__ |
| 53 | static constexpr bool is_steady = false; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 54 | |
| 55 | // Returns the epoch (0). |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame^] | 56 | static constexpr realtime_clock::time_point epoch() { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 57 | return time_point(zero()); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 58 | } |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 59 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame^] | 60 | static constexpr realtime_clock::duration zero() { return duration(0); } |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 61 | |
| 62 | static constexpr time_point min_time{ |
| 63 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 64 | static constexpr time_point max_time{ |
| 65 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 68 | std::ostream &operator<<(std::ostream &stream, |
| 69 | const aos::monotonic_clock::time_point &now); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame^] | 70 | std::ostream &operator<<(std::ostream &stream, |
| 71 | const aos::realtime_clock::time_point &now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 72 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | namespace time { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 74 | #ifdef __linux__ |
| 75 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 76 | // Construct a time representing the period of hertz. |
| 77 | constexpr ::std::chrono::nanoseconds FromRate(int hertz) { |
| 78 | return ::std::chrono::duration_cast<::std::chrono::nanoseconds>( |
| 79 | ::std::chrono::seconds(1)) / |
| 80 | hertz; |
| 81 | } |
| 82 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 83 | template <typename Scalar> |
| 84 | constexpr Scalar TypedDurationInSeconds(monotonic_clock::duration dt) { |
| 85 | return ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(dt) |
| 86 | .count(); |
| 87 | } |
| 88 | |
| 89 | constexpr double DurationInSeconds(monotonic_clock::duration dt) { |
| 90 | return TypedDurationInSeconds<double>(dt); |
| 91 | } |
| 92 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 93 | #endif // __linux__ |
| 94 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 95 | // Converts a monotonic_clock::duration into a timespec object. |
| 96 | struct timespec to_timespec(::aos::monotonic_clock::duration duration); |
| 97 | |
| 98 | // Converts a monotonic_clock::time_point into a timespec object as time since |
| 99 | // epoch. |
| 100 | struct timespec to_timespec(::aos::monotonic_clock::time_point time); |
| 101 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 102 | namespace time_internal { |
| 103 | |
| 104 | template <class T> |
| 105 | struct is_duration : std::false_type {}; |
| 106 | template <class Rep, class Period> |
| 107 | struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {}; |
| 108 | |
| 109 | } // namespace time_internal |
| 110 | |
| 111 | // Returns the greatest duration t representable in ToDuration that is less or |
| 112 | // equal to d. |
| 113 | // Implementation copied from |
| 114 | // https://en.cppreference.com/w/cpp/chrono/duration/floor. |
| 115 | // TODO(Brian): Remove once we have C++17 support. |
| 116 | template <class To, class Rep, class Period, |
| 117 | class = std::enable_if_t<time_internal::is_duration<To>{}>> |
| 118 | constexpr To floor(const std::chrono::duration<Rep, Period> &d) { |
| 119 | To t = std::chrono::duration_cast<To>(d); |
| 120 | if (t > d) return t - To{1}; |
| 121 | return t; |
| 122 | } |
| 123 | |
| 124 | // Returns the value t representable in ToDuration that is the closest to d. If |
| 125 | // there are two such values, returns the even value (that is, the value t such |
| 126 | // that t % 2 == 0). |
| 127 | // Implementation copied from |
| 128 | // https://en.cppreference.com/w/cpp/chrono/duration/round. |
| 129 | // TODO(Brian): Remove once we have C++17 support. |
| 130 | template <class To, class Rep, class Period, |
| 131 | class = std::enable_if_t< |
| 132 | time_internal::is_duration<To>{} && |
| 133 | !std::chrono::treat_as_floating_point<typename To::rep>{}>> |
| 134 | constexpr To round(const std::chrono::duration<Rep, Period> &d) { |
| 135 | To t0 = aos::time::floor<To>(d); |
| 136 | To t1 = t0 + To{1}; |
| 137 | auto diff0 = d - t0; |
| 138 | auto diff1 = t1 - d; |
| 139 | if (diff0 == diff1) { |
| 140 | if (t0.count() & 1) return t1; |
| 141 | return t0; |
| 142 | } else if (diff0 < diff1) { |
| 143 | return t0; |
| 144 | } |
| 145 | return t1; |
| 146 | } |
| 147 | |
| 148 | // Returns the nearest time point to tp representable in ToDuration, rounding to |
| 149 | // even in halfway cases, like std::chrono::round in C++17. |
| 150 | // Implementation copied from |
| 151 | // https://en.cppreference.com/w/cpp/chrono/time_point/round. |
| 152 | // TODO(Brian): Remove once we have C++17 support. |
| 153 | template <class To, class Clock, class FromDuration, |
| 154 | class = std::enable_if_t< |
| 155 | time_internal::is_duration<To>{} && |
| 156 | !std::chrono::treat_as_floating_point<typename To::rep>{}>> |
| 157 | constexpr std::chrono::time_point<Clock, To> round( |
| 158 | const std::chrono::time_point<Clock, FromDuration> &tp) { |
| 159 | return std::chrono::time_point<Clock, To>{ |
| 160 | aos::time::round<To>(tp.time_since_epoch())}; |
| 161 | } |
| 162 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 163 | } // namespace time |
| 164 | } // namespace aos |
| 165 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 166 | #ifdef __linux__ |
| 167 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 168 | namespace std { |
| 169 | namespace this_thread { |
| 170 | // Template specialization for monotonic_clock, since we can use clock_nanosleep |
| 171 | // with TIMER_ABSTIME and get very precise absolute time sleeps. |
| 172 | template <> |
| 173 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time); |
| 174 | |
| 175 | } // namespace this_thread |
| 176 | } // namespace std |
| 177 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 178 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 179 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 180 | #endif // AOS_TIME_H_ |