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 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <ctime> |
Stephan Pleines | cd3701f | 2024-05-30 10:56:04 -0700 | [diff] [blame] | 6 | #include <limits> |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 7 | #include <optional> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include <ostream> |
Stephan Pleines | cd3701f | 2024-05-30 10:56:04 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <thread> // IWYU pragma: keep |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 14 | |
| 15 | class monotonic_clock { |
| 16 | public: |
| 17 | typedef ::std::chrono::nanoseconds::rep rep; |
| 18 | typedef ::std::chrono::nanoseconds::period period; |
| 19 | typedef ::std::chrono::nanoseconds duration; |
| 20 | typedef ::std::chrono::time_point<monotonic_clock> time_point; |
| 21 | |
| 22 | static monotonic_clock::time_point now() noexcept; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 23 | // This clock is still subject to rate adjustments based on adjtime, so it is |
| 24 | // not steady. |
| 25 | static constexpr bool is_steady = false; |
| 26 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 27 | // Converts the time string to a time_point if it is well formatted. This is |
| 28 | // designed to reverse operator <<. |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 29 | #ifdef __linux__ |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 30 | static std::optional<monotonic_clock::time_point> FromString( |
| 31 | const std::string_view now); |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 32 | #endif |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 33 | |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 34 | // Returns the epoch (0). |
| 35 | static constexpr monotonic_clock::time_point epoch() { |
| 36 | return time_point(zero()); |
| 37 | } |
| 38 | |
| 39 | static constexpr monotonic_clock::duration zero() { return duration(0); } |
| 40 | |
| 41 | static constexpr time_point min_time{ |
| 42 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 43 | static constexpr time_point max_time{ |
| 44 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class realtime_clock { |
| 48 | public: |
| 49 | typedef ::std::chrono::nanoseconds::rep rep; |
| 50 | typedef ::std::chrono::nanoseconds::period period; |
| 51 | typedef ::std::chrono::nanoseconds duration; |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 52 | typedef ::std::chrono::time_point<realtime_clock> time_point; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 53 | |
| 54 | #ifdef __linux__ |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 55 | static realtime_clock::time_point now() noexcept; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 56 | #endif // __linux__ |
| 57 | static constexpr bool is_steady = false; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 58 | |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 59 | // Converts the time string to a time_point if it is well formatted. This is |
| 60 | // designed to reverse operator <<. |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 61 | #ifdef __linux__ |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 62 | static std::optional<realtime_clock::time_point> FromString( |
| 63 | const std::string_view now); |
Austin Schuh | d90499f | 2023-03-24 15:10:51 -0700 | [diff] [blame] | 64 | #endif |
Austin Schuh | 3ce0a92 | 2020-07-21 21:08:54 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 66 | // Returns the epoch (0). |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 67 | static constexpr realtime_clock::time_point epoch() { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 68 | return time_point(zero()); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 69 | } |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 70 | |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 71 | static constexpr realtime_clock::duration zero() { return duration(0); } |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 72 | |
| 73 | static constexpr time_point min_time{ |
| 74 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Brian Silverman | 9435727 | 2019-02-23 21:00:54 -0800 | [diff] [blame] | 75 | static constexpr time_point max_time{ |
| 76 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 79 | std::ostream &operator<<(std::ostream &stream, |
| 80 | const aos::monotonic_clock::time_point &now); |
Austin Schuh | 40102d2 | 2019-12-27 23:30:06 -0800 | [diff] [blame] | 81 | std::ostream &operator<<(std::ostream &stream, |
| 82 | const aos::realtime_clock::time_point &now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 83 | |
Eric Schmiedeberg | 42273f4 | 2023-12-14 13:48:45 -0700 | [diff] [blame] | 84 | std::string ToString(const aos::monotonic_clock::time_point &now); |
| 85 | std::string ToString(const aos::realtime_clock::time_point &now); |
| 86 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | namespace time { |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 88 | #ifdef __linux__ |
| 89 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 90 | // Construct a time representing the period of hertz. |
| 91 | constexpr ::std::chrono::nanoseconds FromRate(int hertz) { |
| 92 | return ::std::chrono::duration_cast<::std::chrono::nanoseconds>( |
| 93 | ::std::chrono::seconds(1)) / |
| 94 | hertz; |
| 95 | } |
| 96 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 97 | template <typename Scalar> |
| 98 | constexpr Scalar TypedDurationInSeconds(monotonic_clock::duration dt) { |
| 99 | return ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(dt) |
| 100 | .count(); |
| 101 | } |
| 102 | |
| 103 | constexpr double DurationInSeconds(monotonic_clock::duration dt) { |
| 104 | return TypedDurationInSeconds<double>(dt); |
| 105 | } |
| 106 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 107 | #endif // __linux__ |
| 108 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 109 | // Converts a monotonic_clock::duration into a timespec object. |
| 110 | struct timespec to_timespec(::aos::monotonic_clock::duration duration); |
| 111 | |
| 112 | // Converts a monotonic_clock::time_point into a timespec object as time since |
| 113 | // epoch. |
| 114 | struct timespec to_timespec(::aos::monotonic_clock::time_point time); |
| 115 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 116 | // Converts a timeval object to a monotonic_clock::time_point. |
| 117 | ::aos::monotonic_clock::time_point from_timeval(struct timeval t); |
| 118 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | } // namespace time |
| 120 | } // namespace aos |
| 121 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 122 | #ifdef __linux__ |
| 123 | |
Alexei Strots | 08b9423 | 2024-05-24 16:48:07 -0700 | [diff] [blame] | 124 | namespace aos::this_thread { |
| 125 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time); |
| 126 | } // namespace aos::this_thread |
| 127 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 128 | // Template specialization for monotonic_clock, since we can use clock_nanosleep |
| 129 | // with TIMER_ABSTIME and get very precise absolute time sleeps. |
| 130 | template <> |
Alexei Strots | 08b9423 | 2024-05-24 16:48:07 -0700 | [diff] [blame] | 131 | inline void std::this_thread::sleep_until( |
| 132 | const ::aos::monotonic_clock::time_point &end_time) { |
| 133 | ::aos::this_thread::sleep_until(end_time); |
| 134 | } |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 135 | |
Brian Silverman | c03a30c | 2019-02-16 18:21:56 -0800 | [diff] [blame] | 136 | #endif // __linux__ |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 137 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 138 | #endif // AOS_TIME_H_ |