blob: 697770ce154f82b846d326e042f3585d71b330f4 [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 Schuhf257f3c2019-10-27 21:00:43 -070012#include "glog/logging.h"
brians343bc112013-02-10 01:53:46 +000013
Brian Silvermanc03a30c2019-02-16 18:21:56 -080014#else // __linux__
15
16#include "motors/core/kinetis.h"
17
18// The systick interrupt increments this every 1ms.
19extern "C" volatile uint32_t systick_millis_count;
20
21#endif // __linux__
22
Austin Schuhf2a50ba2016-12-24 16:16:26 -080023namespace chrono = ::std::chrono;
24
Brian Silvermanc03a30c2019-02-16 18:21:56 -080025#ifdef __linux__
26
Austin Schuh793d6b92016-05-01 13:28:14 -070027namespace std {
28namespace this_thread {
29template <>
30void sleep_until(const ::aos::monotonic_clock::time_point &end_time) {
31 struct timespec end_time_timespec;
32 ::std::chrono::seconds sec =
33 ::std::chrono::duration_cast<::std::chrono::seconds>(
34 end_time.time_since_epoch());
35 ::std::chrono::nanoseconds nsec =
36 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
37 end_time.time_since_epoch() - sec);
38 end_time_timespec.tv_sec = sec.count();
39 end_time_timespec.tv_nsec = nsec.count();
40 int returnval;
41 do {
42 returnval = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
43 &end_time_timespec, nullptr);
Austin Schuhf257f3c2019-10-27 21:00:43 -070044 PCHECK(returnval == 0 || returnval == EINTR)
45 << ": clock_nanosleep(" << static_cast<uintmax_t>(CLOCK_MONOTONIC)
46 << ", TIMER_ABSTIME, " << &end_time_timespec << ", nullptr) failed";
Austin Schuh793d6b92016-05-01 13:28:14 -070047 } while (returnval != 0);
48}
49
50} // namespace this_thread
51} // namespace std
52
Brian Silvermanc03a30c2019-02-16 18:21:56 -080053#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -070054
brians343bc112013-02-10 01:53:46 +000055namespace aos {
Austin Schuhde8a8ff2019-11-30 15:25:36 -080056
James Kuszmaul38735e82019-12-07 16:42:06 -080057std::ostream &operator<<(std::ostream &stream,
58 const aos::monotonic_clock::time_point &now) {
Austin Schuh40102d22019-12-27 23:30:06 -080059 if (now < monotonic_clock::epoch()) {
60 std::chrono::seconds seconds =
61 std::chrono::duration_cast<std::chrono::seconds>(
62 now.time_since_epoch());
63
64 stream << "-" << -seconds.count() << "." << std::setfill('0')
65 << std::setw(9)
66 << std::chrono::duration_cast<std::chrono::nanoseconds>(
67 seconds - now.time_since_epoch())
68 .count()
69 << "sec";
70 } else {
71 std::chrono::seconds seconds =
72 std::chrono::duration_cast<std::chrono::seconds>(
73 now.time_since_epoch());
74 stream << seconds.count() << "." << std::setfill('0') << std::setw(9)
75 << std::chrono::duration_cast<std::chrono::nanoseconds>(
76 now.time_since_epoch() - seconds)
77 .count()
78 << "sec";
79 }
80 return stream;
81}
82
Austin Schuh3ce0a922020-07-21 21:08:54 -070083std::optional<monotonic_clock::time_point> monotonic_clock::FromString(
84 const std::string_view now) {
85 // This should undo the operator << above.
86 if (now.size() < 14) {
87 return std::nullopt;
88 }
89
90 if (now.substr(now.size() - 3, now.size()) != "sec") {
91 return std::nullopt;
92 }
93
94 if (now.substr(now.size() - 13, 1) != ".") {
95 return std::nullopt;
96 }
97
98 bool negative = now.substr(0, 1) == "-";
99
100 std::string sec(
101 now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13)));
102 std::string nsec(now.substr(now.size() - 12, 9));
103
104 if (!std::all_of(sec.begin(), sec.end(), ::isdigit) ||
105 !std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
106 return std::nullopt;
107 }
108
109 return monotonic_clock::time_point(
110 std::chrono::seconds((negative ? -1 : 1) * atoll(sec.c_str())) +
111 std::chrono::nanoseconds((negative ? -1 : 1) * atoll(nsec.c_str())));
112}
113
114std::optional<realtime_clock::time_point> realtime_clock::FromString(
115 const std::string_view now) {
116 // This should undo the operator << above.
117
118 if (now.size() < 25) {
119 return std::nullopt;
120 }
121
122 if (now.substr(now.size() - 10, 1) != ".") {
123 return std::nullopt;
124 }
125
126 std::string nsec(now.substr(now.size() - 9, 9));
127
128 if (!std::all_of(nsec.begin(), nsec.end(), ::isdigit)) {
129 return std::nullopt;
130 }
131
132 struct tm tm;
133 std::istringstream ss(std::string(now.substr(0, now.size() - 10)));
134 ss >> std::get_time(&tm, "%Y-%m-%d_%H-%M-%S");
135 tm.tm_isdst = -1;
136
137 time_t seconds = mktime(&tm);
138
139 return realtime_clock::time_point(
140 std::chrono::seconds(seconds) +
141 std::chrono::nanoseconds(atoll(nsec.c_str())));
142}
143
Austin Schuh40102d22019-12-27 23:30:06 -0800144std::ostream &operator<<(std::ostream &stream,
145 const aos::realtime_clock::time_point &now) {
146 std::tm tm;
147 std::chrono::seconds seconds =
148 now < realtime_clock::epoch()
149 ? std::chrono::duration_cast<std::chrono::seconds>(
150 now.time_since_epoch() - std::chrono::nanoseconds(999999999))
151 : std::chrono::duration_cast<std::chrono::seconds>(
Austin Schuhe92f7e62019-12-25 13:54:41 -0800152 now.time_since_epoch());
153
Austin Schuh40102d22019-12-27 23:30:06 -0800154 std::time_t seconds_t = seconds.count();
155 stream << std::put_time(localtime_r(&seconds_t, &tm), "%Y-%m-%d_%H-%M-%S.")
156 << std::setfill('0') << std::setw(9)
157 << std::chrono::duration_cast<std::chrono::nanoseconds>(
158 now.time_since_epoch() - seconds)
159 .count();
160 return stream;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800161}
162
brians343bc112013-02-10 01:53:46 +0000163namespace time {
164
Austin Schuh40102d22019-12-27 23:30:06 -0800165struct timespec to_timespec(const ::aos::monotonic_clock::duration duration) {
Neil Balch229001a2018-01-07 18:22:52 -0800166 struct timespec time_timespec;
167 ::std::chrono::seconds sec =
168 ::std::chrono::duration_cast<::std::chrono::seconds>(duration);
169 ::std::chrono::nanoseconds nsec =
170 ::std::chrono::duration_cast<::std::chrono::nanoseconds>(duration - sec);
171 time_timespec.tv_sec = sec.count();
172 time_timespec.tv_nsec = nsec.count();
173 return time_timespec;
174}
175
Austin Schuh40102d22019-12-27 23:30:06 -0800176struct timespec to_timespec(const ::aos::monotonic_clock::time_point time) {
Neil Balch229001a2018-01-07 18:22:52 -0800177 return to_timespec(time.time_since_epoch());
178}
Brian Silverman967e5df2020-02-09 16:43:34 -0800179
180::aos::monotonic_clock::time_point from_timeval(struct timeval t) {
181 return monotonic_clock::epoch() + std::chrono::seconds(t.tv_sec) +
182 std::chrono::microseconds(t.tv_usec);
183}
184
brians343bc112013-02-10 01:53:46 +0000185} // namespace time
Austin Schuh858c0212016-11-25 17:23:30 -0800186
187constexpr monotonic_clock::time_point monotonic_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800188constexpr monotonic_clock::time_point monotonic_clock::max_time;
Brian Silverman2eb89762019-02-17 15:16:37 -0800189constexpr realtime_clock::time_point realtime_clock::min_time;
Brian Silverman94357272019-02-23 21:00:54 -0800190constexpr realtime_clock::time_point realtime_clock::max_time;
Austin Schuh858c0212016-11-25 17:23:30 -0800191
192monotonic_clock::time_point monotonic_clock::now() noexcept {
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800193#ifdef __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800194 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700195 PCHECK(clock_gettime(CLOCK_MONOTONIC, &current_time) == 0)
196 << ": clock_gettime(" << static_cast<uintmax_t>(CLOCK_MONOTONIC) << ", "
197 << &current_time << ") failed";
198
Austin Schuh858c0212016-11-25 17:23:30 -0800199 return time_point(::std::chrono::seconds(current_time.tv_sec) +
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 ::std::chrono::nanoseconds(current_time.tv_nsec));
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800201
202#else // __linux__
203
204 __disable_irq();
205 const uint32_t current_counter = SYST_CVR;
206 uint32_t ms_count = systick_millis_count;
207 const uint32_t istatus = SCB_ICSR;
208 __enable_irq();
209 // If the interrupt is pending and the timer has already wrapped from 0 back
210 // up to its max, then add another ms.
211 if ((istatus & SCB_ICSR_PENDSTSET) && current_counter > 50) {
212 ++ms_count;
213 }
214
215 // It counts down, but everything we care about counts up.
216 const uint32_t counter_up = ((F_CPU / 1000) - 1) - current_counter;
217
218 // "3.2.1.2 System Tick Timer" in the TRM says "The System Tick Timer's clock
219 // source is always the core clock, FCLK".
220 using systick_duration =
221 std::chrono::duration<uint32_t, std::ratio<1, F_CPU>>;
222
223 return time_point(aos::time::round<std::chrono::nanoseconds>(
224 std::chrono::milliseconds(ms_count) + systick_duration(counter_up)));
225
226#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800227}
228
Brian Silvermana3688802019-02-16 19:31:26 -0800229#ifdef __linux__
230realtime_clock::time_point realtime_clock::now() noexcept {
231 struct timespec current_time;
Austin Schuhf257f3c2019-10-27 21:00:43 -0700232 PCHECK(clock_gettime(CLOCK_REALTIME, &current_time) == 0)
233 << "clock_gettime(" << static_cast<uintmax_t>(CLOCK_REALTIME) << ", "
234 << &current_time << ") failed";
Brian Silvermana3688802019-02-16 19:31:26 -0800235
236 return time_point(::std::chrono::seconds(current_time.tv_sec) +
237 ::std::chrono::nanoseconds(current_time.tv_nsec));
238}
239#endif // __linux__
Austin Schuh858c0212016-11-25 17:23:30 -0800240
brians343bc112013-02-10 01:53:46 +0000241} // namespace aos