blob: 4f39c36065f2f247ffb828c5337fa5330ee48b09 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/time/time.h"
brians343bc112013-02-10 01:53:46 +00002
Austin Schuh3ce0a922020-07-21 21:08:54 -07003#include <algorithm>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08004#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
6#include <cstring>
James Kuszmaul38735e82019-12-07 16:42:06 -08007#include <ctime>
Austin Schuhe92f7e62019-12-25 13:54:41 -08008#include <iomanip>
Brian Silvermand0575692015-02-21 16:24:02 -05009
Brian Silvermanc03a30c2019-02-16 18:21:56 -080010#ifdef __linux__
11
Austin Schuhd90499f2023-03-24 15:10:51 -070012#include "absl/strings/numbers.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070013#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000014
Brian Silvermanc03a30c2019-02-16 18:21:56 -080015#else // __linux__
16
17#include "motors/core/kinetis.h"
18
19// The systick interrupt increments this every 1ms.
20extern "C" volatile uint32_t systick_millis_count;
21
22#endif // __linux__
23
Austin Schuhf2a50ba2016-12-24 16:16:26 -080024namespace chrono = ::std::chrono;
25
Brian Silvermanc03a30c2019-02-16 18:21:56 -080026#ifdef __linux__
27
Austin Schuh793d6b92016-05-01 13:28:14 -070028namespace std {
29namespace this_thread {
30template <>
31void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
32 struct timespec end_time_timespec;
33 ::std::chrono::seconds sec =
34 ::std::chrono::duration_cast<::std::chrono::seconds>(
35 end_time.time_since_epoch());
36 ::std::chrono::nanoseconds nsec =
37 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
38 end_time.time_since_epoch() - sec);
39 end_time_timespec.tv_sec = sec.count();
40 end_time_timespec.tv_nsec = nsec.count();
41 int returnval;
42 do {
43 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
44 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070045 PCHECK(returnval == 0 || returnval == EINTR)
46 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
47 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070048 } while (returnval != 0);
49}
50
51} // namespace this_thread
52} // namespace std
53
Brian Silvermanc03a30c2019-02-16 18:21:56 -080054#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070055
brians343bc112013-02-10 01:53:46 +000056namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080057
James Kuszmaul38735e82019-12-07 16:42:06 -080058std::ostream &operator<<(std::ostream &stream,
59 const aos::monotonic_clock::time_point &now) {
Austin Schuh40102d22019-12-27 23:30:06 -080060 if (now < monotonic_clock::epoch()) {
61 std::chrono::seconds seconds =
62 std::chrono::duration_cast<std::chrono::seconds>(
63 now.time_since_epoch());
64
65 stream << "-" << -seconds.count() << "." << std::setfill('0')
66 << std::setw(9)
67 << std::chrono::duration_cast<std::chrono::nanoseconds>(
68 seconds - now.time_since_epoch())
69 .count()
70 << "sec";
71 } else {
72 std::chrono::seconds seconds =
73 std::chrono::duration_cast<std::chrono::seconds>(
74 now.time_since_epoch());
75 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
76 << std::chrono::duration_cast<std::chrono::nanoseconds>(
77 now.time_since_epoch() - seconds)
78 .count()
79 << "sec";
80 }
81 return stream;
82}
83
Austin Schuhd90499f2023-03-24 15:10:51 -070084#ifdef __linux__
Austin Schuh3ce0a922020-07-21 21:08:54 -070085std::optional<monotonic_clock::time_point> monotonic_clock::FromString(
86 const std::string_view now) {
87 // This should undo the operator << above.
88 if (now.size() < 14) {
89 return std::nullopt;
90 }
91
92 if (now.substr(now.size() - 3, now.size()) != "sec") {
93 return std::nullopt;
94 }
95
Brian J Griglak259048b2022-01-27 12:30:55 -070096 if (now[now.size() - 13] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -070097 return std::nullopt;
98 }
99
Brian J Griglak259048b2022-01-27 12:30:55 -0700100 bool negative = now[0] == '-';
Austin Schuh3ce0a922020-07-21 21:08:54 -0700101
Austin Schuhd90499f2023-03-24 15:10:51 -0700102 std::string_view sec(
Austin Schuh3ce0a922020-07-21 21:08:54 -0700103 now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13)));
Austin Schuhd90499f2023-03-24 15:10:51 -0700104 std::string_view nsec(now.substr(now.size() - 12, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700105
106 if (!std::all_of(sec.begin(), sec.end(), ::isdigit) ||
107 !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
108 return std::nullopt;
109 }
110
Austin Schuhd90499f2023-03-24 15:10:51 -0700111 std::chrono::seconds::rep seconds_data;
112 if (!absl::SimpleAtoi(sec, &seconds_data)) {
113 return std::nullopt;
114 }
115
116 std::chrono::nanoseconds::rep nanoseconds_data;
117 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
118 return std::nullopt;
119 }
120
Austin Schuh3ce0a922020-07-21 21:08:54 -0700121 return monotonic_clock::time_point(
Austin Schuhd90499f2023-03-24 15:10:51 -0700122 std::chrono::seconds((negative ? -1 : 1) * seconds_data) +
123 std::chrono::nanoseconds((negative ? -1 : 1) * nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700124}
125
126std::optional<realtime_clock::time_point> realtime_clock::FromString(
127 const std::string_view now) {
128 // This should undo the operator << above.
129
130 if (now.size() < 25) {
131 return std::nullopt;
132 }
133
Brian J Griglak259048b2022-01-27 12:30:55 -0700134 if (now[now.size() - 10] != '.') {
Austin Schuh3ce0a922020-07-21 21:08:54 -0700135 return std::nullopt;
136 }
137
Austin Schuhd90499f2023-03-24 15:10:51 -0700138 std::string_view nsec(now.substr(now.size() - 9, 9));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700139
140 if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
141 return std::nullopt;
142 }
143
144 struct tm tm;
145 std::istringstream ss(std::string(now.substr(0, now.size() - 10)));
146 ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S");
Austin Schuh76a313c2021-11-30 19:16:35 -0800147 if (ss.fail()) {
148 return std::nullopt;
149 }
Austin Schuh3ce0a922020-07-21 21:08:54 -0700150 tm.tm_isdst = -1;
151
152 time_t seconds = mktime(&tm);
153
Austin Schuhd90499f2023-03-24 15:10:51 -0700154 std::chrono::nanoseconds::rep nanoseconds_data;
155 if (!absl::SimpleAtoi(nsec, &nanoseconds_data)) {
156 return std::nullopt;
157 }
158
Austin Schuh3ce0a922020-07-21 21:08:54 -0700159 return realtime_clock::time_point(
160 std::chrono::seconds(seconds) +
Austin Schuhd90499f2023-03-24 15:10:51 -0700161 std::chrono::nanoseconds(nanoseconds_data));
Austin Schuh3ce0a922020-07-21 21:08:54 -0700162}
Austin Schuhd90499f2023-03-24 15:10:51 -0700163#endif
Austin Schuh3ce0a922020-07-21 21:08:54 -0700164
Austin Schuh40102d22019-12-27 23:30:06 -0800165std::ostream &operator<<(std::ostream &stream,
166 const aos::realtime_clock::time_point &now) {
167 std::tm tm;
168 std::chrono::seconds seconds =
169 now < realtime_clock::epoch()
Austin Schuhb2ac0562021-09-13 23:23:33 -0700170 ? (std::chrono::duration_cast<std::chrono::seconds>(
171 now.time_since_epoch() + std::chrono::nanoseconds(1)) -
172 std::chrono::seconds(1))
Austin Schuh40102d22019-12-27 23:30:06 -0800173 : std::chrono::duration_cast<std::chrono::seconds>(
Austin Schuhe92f7e62019-12-25 13:54:41 -0800174 now.time_since_epoch());
175
Austin Schuh40102d22019-12-27 23:30:06 -0800176 std::time_t seconds_t = seconds.count();
177 stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.")
178 << std::setfill('0') << std::setw(9)
179 << std::chrono::duration_cast<std::chrono::nanoseconds>(
180 now.time_since_epoch() - seconds)
181 .count();
182 return stream;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800183}
184
brians343bc112013-02-10 01:53:46 +0000185namespace time {
186
Austin Schuh40102d22019-12-27 23:30:06 -0800187struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) {
Neil Balch229001a2018-01-07 18:22:52 -0800188 struct timespec time_timespec;
189 ::std::chrono::seconds sec =
190 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
191 ::std::chrono::nanoseconds nsec =
192 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
193 time_timespec.tv_sec = sec.count();
194 time_timespec.tv_nsec = nsec.count();
195 return time_timespec;
196}
197
Austin Schuh40102d22019-12-27 23:30:06 -0800198struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) {
Neil Balch229001a2018-01-07 18:22:52 -0800199 return to_timespec(time.time_since_epoch());
200}
Brian Silverman967e5df2020-02-09 16:43:34 -0800201
202::aos::monotonic_clock::time_point from_timeval(struct timeval t) {
203 return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) +
204 std::chrono::microseconds(t.tv_usec);
205}
206
brians343bc112013-02-10 01:53:46 +0000207} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -0800208
209constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800210constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -0800211constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800212constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -0800213
214monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800215#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800216 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700217 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
218 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
219 << &current_time << ") failed";
220
Austin Schuh858c0212016-11-25 17:23:30 -0800221 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800223
224#else // __linux__
225
226 __disable_irq();
227 const uint32_t current_counter = SYST_CVR;
228 uint32_t ms_count = systick_millis_count;
229 const uint32_t istatus = SCB_ICSR;
230 __enable_irq();
231 // If the interrupt is pending and the timer has already wrapped from 0 back
232 // up to its max, then add another ms.
233 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
234 ++ms_count;
235 }
236
237 // It counts down, but everything we care about counts up.
238 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
239
240 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
241 // source is always the core clock, FCLK".
242 using systick_duration =
243 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
244
Milo Linfd40acb2021-11-06 14:51:36 -0700245 return time_point(std::chrono::round<std::chrono::nanoseconds>(
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800246 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
247
248#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800249}
250
Brian Silvermana3688802019-02-16 19:31:26 -0800251#ifdef __linux__
252realtime_clock::time_point realtime_clock::now() noexcept {
253 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700254 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
255 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
256 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800257
258 return time_point(::std::chrono::seconds(current_time.tv_sec) +
259 ::std::chrono::nanoseconds(current_time.tv_nsec));
260}
261#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800262
brians343bc112013-02-10 01:53:46 +0000263} // namespace aos