blob: e31de5c0d9cf5ec91ceff5b294d3b513b6332bf0 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_TIME_H_
2#define AOS_TIME_H_
brians343bc112013-02-10 01:53:46 +00003
4#include <stdint.h>
brians57dd5822013-02-27 21:44:15 +00005#include <sys/time.h>
James Kuszmaul651fc3f2019-05-15 21:14:25 -07006#include <time.h>
Brian4a424a22014-04-02 11:52:45 -07007
Austin Schuh793d6b92016-05-01 13:28:14 -07008#include <chrono>
brians343bc112013-02-10 01:53:46 +00009#include <ostream>
James Kuszmaul651fc3f2019-05-15 21:14:25 -070010#include <thread>
11#include <type_traits>
brians343bc112013-02-10 01:53:46 +000012
John Park33858a32018-09-28 23:05:48 -070013#include "aos/macros.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -070014#include "aos/type_traits/type_traits.h"
brians343bc112013-02-10 01:53:46 +000015
16namespace aos {
Austin Schuh793d6b92016-05-01 13:28:14 -070017
18class 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 Silvermana3688802019-02-16 19:31:26 -080026 // 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 Silverman94357272019-02-23 21:00:54 -080039 static constexpr time_point max_time{
40 time_point(duration(::std::numeric_limits<duration::rep>::max()))};
Brian Silvermana3688802019-02-16 19:31:26 -080041};
42
43class 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 Schuh40102d22019-12-27 23:30:06 -080048 typedef ::std::chrono::time_point<realtime_clock> time_point;
Brian Silvermana3688802019-02-16 19:31:26 -080049
50#ifdef __linux__
Austin Schuh40102d22019-12-27 23:30:06 -080051 static realtime_clock::time_point now() noexcept;
Brian Silvermana3688802019-02-16 19:31:26 -080052#endif // __linux__
53 static constexpr bool is_steady = false;
Austin Schuh793d6b92016-05-01 13:28:14 -070054
55 // Returns the epoch (0).
Austin Schuh40102d22019-12-27 23:30:06 -080056 static constexpr realtime_clock::time_point epoch() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -070057 return time_point(zero());
Austin Schuh793d6b92016-05-01 13:28:14 -070058 }
Austin Schuh8aec1ed2016-05-01 13:29:20 -070059
Austin Schuh40102d22019-12-27 23:30:06 -080060 static constexpr realtime_clock::duration zero() { return duration(0); }
Austin Schuh858c0212016-11-25 17:23:30 -080061
62 static constexpr time_point min_time{
63 time_point(duration(::std::numeric_limits<duration::rep>::min()))};
Brian Silverman94357272019-02-23 21:00:54 -080064 static constexpr time_point max_time{
65 time_point(duration(::std::numeric_limits<duration::rep>::max()))};
Austin Schuh793d6b92016-05-01 13:28:14 -070066};
67
James Kuszmaul38735e82019-12-07 16:42:06 -080068std::ostream &operator<<(std::ostream &stream,
69 const aos::monotonic_clock::time_point &now);
Austin Schuh40102d22019-12-27 23:30:06 -080070std::ostream &operator<<(std::ostream &stream,
71 const aos::realtime_clock::time_point &now);
Austin Schuhde8a8ff2019-11-30 15:25:36 -080072
brians343bc112013-02-10 01:53:46 +000073namespace time {
Brian Silvermanc03a30c2019-02-16 18:21:56 -080074#ifdef __linux__
75
Austin Schuhf2a50ba2016-12-24 16:16:26 -080076// Construct a time representing the period of hertz.
77constexpr ::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 Kuszmaul651fc3f2019-05-15 21:14:25 -070083template <typename Scalar>
84constexpr Scalar TypedDurationInSeconds(monotonic_clock::duration dt) {
85 return ::std::chrono::duration_cast<::std::chrono::duration<Scalar>>(dt)
86 .count();
87}
88
89constexpr double DurationInSeconds(monotonic_clock::duration dt) {
90 return TypedDurationInSeconds<double>(dt);
91}
92
Brian Silvermanc03a30c2019-02-16 18:21:56 -080093#endif // __linux__
94
Neil Balch229001a2018-01-07 18:22:52 -080095// Converts a monotonic_clock::duration into a timespec object.
96struct 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.
100struct timespec to_timespec(::aos::monotonic_clock::time_point time);
101
Brian Silverman967e5df2020-02-09 16:43:34 -0800102// Converts a timeval object to a monotonic_clock::time_point.
103::aos::monotonic_clock::time_point from_timeval(struct timeval t);
104
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800105namespace time_internal {
106
107template <class T>
108struct is_duration : std::false_type {};
109template <class Rep, class Period>
110struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
111
112} // namespace time_internal
113
114// Returns the greatest duration t representable in ToDuration that is less or
115// equal to d.
116// Implementation copied from
117// https://en.cppreference.com/w/cpp/chrono/duration/floor.
118// TODO(Brian): Remove once we have C++17 support.
119template <class To, class Rep, class Period,
120 class = std::enable_if_t<time_internal::is_duration<To>{}>>
121constexpr To floor(const std::chrono::duration<Rep, Period> &d) {
122 To t = std::chrono::duration_cast<To>(d);
123 if (t > d) return t - To{1};
124 return t;
125}
126
127// Returns the value t representable in ToDuration that is the closest to d. If
128// there are two such values, returns the even value (that is, the value t such
129// that t % 2 == 0).
130// Implementation copied from
131// https://en.cppreference.com/w/cpp/chrono/duration/round.
132// TODO(Brian): Remove once we have C++17 support.
133template <class To, class Rep, class Period,
134 class = std::enable_if_t<
135 time_internal::is_duration<To>{} &&
136 !std::chrono::treat_as_floating_point<typename To::rep>{}>>
137constexpr To round(const std::chrono::duration<Rep, Period> &d) {
138 To t0 = aos::time::floor<To>(d);
139 To t1 = t0 + To{1};
140 auto diff0 = d - t0;
141 auto diff1 = t1 - d;
142 if (diff0 == diff1) {
143 if (t0.count() & 1) return t1;
144 return t0;
145 } else if (diff0 < diff1) {
146 return t0;
147 }
148 return t1;
149}
150
151// Returns the nearest time point to tp representable in ToDuration, rounding to
152// even in halfway cases, like std::chrono::round in C++17.
153// Implementation copied from
154// https://en.cppreference.com/w/cpp/chrono/time_point/round.
155// TODO(Brian): Remove once we have C++17 support.
156template <class To, class Clock, class FromDuration,
157 class = std::enable_if_t<
158 time_internal::is_duration<To>{} &&
159 !std::chrono::treat_as_floating_point<typename To::rep>{}>>
160constexpr std::chrono::time_point<Clock, To> round(
161 const std::chrono::time_point<Clock, FromDuration> &tp) {
162 return std::chrono::time_point<Clock, To>{
163 aos::time::round<To>(tp.time_since_epoch())};
164}
165
brians343bc112013-02-10 01:53:46 +0000166} // namespace time
167} // namespace aos
168
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800169#ifdef __linux__
170
Austin Schuh793d6b92016-05-01 13:28:14 -0700171namespace std {
172namespace this_thread {
173// Template specialization for monotonic_clock, since we can use clock_nanosleep
174// with TIMER_ABSTIME and get very precise absolute time sleeps.
175template <>
176void sleep_until(const ::aos::monotonic_clock::time_point &end_time);
177
178} // namespace this_thread
179} // namespace std
180
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800181#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -0700182
John Park33858a32018-09-28 23:05:48 -0700183#endif // AOS_TIME_H_