CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
timestamp.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <chrono>
17#include <thread> // For std::this_thread::sleep_for
18#include <cstdint>
19#include <ctime>
20
21namespace commrat {
22
29using Timestamp = uint64_t;
30
37using Nanoseconds = std::chrono::nanoseconds;
38using Microseconds = std::chrono::microseconds;
39using Milliseconds = std::chrono::milliseconds;
40using Seconds = std::chrono::seconds;
41using Minutes = std::chrono::minutes;
42using Hours = std::chrono::hours;
43
52class Time {
53public:
64
72 static Timestamp now() noexcept {
73 return get_timestamp(current_clock_source_);
74 }
75
83 switch (source) {
85 return system_clock_now();
86
89 return steady_clock_now();
90
92 return posix_clock_now(CLOCK_REALTIME);
93
95 return posix_clock_now(CLOCK_MONOTONIC);
96
97 default:
98 return steady_clock_now();
99 }
100 }
101
109 static void set_clock_source(ClockSource source) noexcept {
110 current_clock_source_ = source;
111 }
112
116 template<typename Rep, typename Period>
117 static constexpr Timestamp to_nanoseconds(std::chrono::duration<Rep, Period> duration) noexcept {
118 return static_cast<Timestamp>(
119 std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()
120 );
121 }
122
126 template<typename Duration>
127 static constexpr Duration from_nanoseconds(Timestamp ns) noexcept {
128 return std::chrono::duration_cast<Duration>(std::chrono::nanoseconds(ns));
129 }
130
134 static constexpr Timestamp milliseconds_to_ns(uint64_t ms) noexcept {
135 return ms * 1'000'000;
136 }
137
141 static constexpr Timestamp microseconds_to_ns(uint64_t us) noexcept {
142 return us * 1'000;
143 }
144
148 static constexpr uint64_t ns_to_milliseconds(Timestamp ns) noexcept {
149 return ns / 1'000'000;
150 }
151
155 static constexpr uint64_t ns_to_microseconds(Timestamp ns) noexcept {
156 return ns / 1'000;
157 }
158
166 static constexpr Timestamp diff(Timestamp t1, Timestamp t2) noexcept {
167 return (t1 > t2) ? (t1 - t2) : (t2 - t1);
168 }
169
178 static constexpr bool is_within_tolerance(Timestamp timestamp,
179 Timestamp target,
180 Timestamp tolerance_ns) noexcept {
181 return diff(timestamp, target) <= tolerance_ns;
182 }
183
193 static void sleep_ns(Timestamp ns) noexcept {
194 std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
195 }
196
200 template<typename Rep, typename Period>
201 static void sleep(std::chrono::duration<Rep, Period> duration) noexcept {
202 std::this_thread::sleep_for(duration);
203 }
204
205private:
206 // Implementation helpers
207 static Timestamp system_clock_now() noexcept {
208 auto now = std::chrono::system_clock::now();
209 auto duration = now.time_since_epoch();
210 return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
211 }
212
213 static Timestamp steady_clock_now() noexcept {
214 auto now = std::chrono::steady_clock::now();
215 auto duration = now.time_since_epoch();
216 return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
217 }
218
219 static Timestamp posix_clock_now(clockid_t clock_id) noexcept {
220 struct timespec ts;
221 if (clock_gettime(clock_id, &ts) == 0) {
222 return static_cast<Timestamp>(ts.tv_sec) * 1'000'000'000 +
223 static_cast<Timestamp>(ts.tv_nsec);
224 }
225 // Fallback to steady clock on error
226 return steady_clock_now();
227 }
228
229 // Default clock source (can be changed via set_clock_source)
230 static inline ClockSource current_clock_source_ = ClockSource::STEADY_CLOCK;
231};
232
241/*
242inline constexpr bool operator<(Timestamp lhs, Timestamp rhs) noexcept {
243 return lhs < rhs;
244}
245
246inline constexpr bool operator<=(Timestamp lhs, Timestamp rhs) noexcept {
247 return lhs <= rhs;
248}
249
250inline constexpr bool operator>(Timestamp lhs, Timestamp rhs) noexcept {
251 return lhs > rhs;
252}
253
254inline constexpr bool operator>=(Timestamp lhs, Timestamp rhs) noexcept {
255 return lhs >= rhs;
256}
257
258inline constexpr Timestamp operator+(Timestamp ts, uint64_t ns) noexcept {
259 return ts + ns;
260}
261
262inline constexpr Timestamp operator-(Timestamp ts, uint64_t ns) noexcept {
263 return ts - ns;
264}
265
266inline constexpr Timestamp operator-(Timestamp t1, Timestamp t2) noexcept {
267 return t1 - t2;
268}
269*/
270
279namespace literals {
280 constexpr Timestamp operator""_ns(unsigned long long ns) noexcept {
281 return static_cast<Timestamp>(ns);
282 }
283
284 constexpr Timestamp operator""_us(unsigned long long us) noexcept {
285 return Time::microseconds_to_ns(us);
286 }
287
288 constexpr Timestamp operator""_ms(unsigned long long ms) noexcept {
289 return Time::milliseconds_to_ns(ms);
290 }
291
292 constexpr Timestamp operator""_s(unsigned long long s) noexcept {
293 return Time::milliseconds_to_ns(s * 1000);
294 }
295} // namespace literals
296
297} // namespace commrat
Time utility class - abstraction over clock sources.
Definition timestamp.hpp:52
static Timestamp now() noexcept
Get current timestamp in nanoseconds.
Definition timestamp.hpp:72
static constexpr Duration from_nanoseconds(Timestamp ns) noexcept
Convert nanoseconds to std::chrono::duration.
static void set_clock_source(ClockSource source) noexcept
Set default clock source for all future now() calls.
static Timestamp get_timestamp(ClockSource source=ClockSource::STEADY_CLOCK) noexcept
Get current timestamp from specific clock source.
Definition timestamp.hpp:82
static void sleep_ns(Timestamp ns) noexcept
Sleep for specified nanoseconds.
static constexpr Timestamp milliseconds_to_ns(uint64_t ms) noexcept
Convert milliseconds to nanoseconds.
static constexpr Timestamp to_nanoseconds(std::chrono::duration< Rep, Period > duration) noexcept
Convert std::chrono::duration to nanoseconds.
static void sleep(std::chrono::duration< Rep, Period > duration) noexcept
Sleep for specified duration.
ClockSource
Clock source types.
Definition timestamp.hpp:57
@ SYSTEM_CLOCK
std::chrono::system_clock (wall time)
@ REALTIME_CLOCK
CLOCK_REALTIME (future: PTP, NTP sync)
@ STEADY_CLOCK
std::chrono::steady_clock (monotonic)
@ HIGH_RES_CLOCK
std::chrono::high_resolution_clock
@ MONOTONIC_CLOCK
CLOCK_MONOTONIC (future: realtime monotonic)
static constexpr Timestamp microseconds_to_ns(uint64_t us) noexcept
Convert microseconds to nanoseconds.
static constexpr uint64_t ns_to_milliseconds(Timestamp ns) noexcept
Convert nanoseconds to milliseconds.
static constexpr Timestamp diff(Timestamp t1, Timestamp t2) noexcept
Calculate absolute time difference between two timestamps.
static constexpr uint64_t ns_to_microseconds(Timestamp ns) noexcept
Convert nanoseconds to microseconds.
static constexpr bool is_within_tolerance(Timestamp timestamp, Timestamp target, Timestamp tolerance_ns) noexcept
Check if timestamp is within tolerance of target.
CommRaT - Modern C++ Real-Time Communication Framework.
uint64_t Timestamp
Timestamp type - uint64_t nanoseconds since epoch.
Definition timestamp.hpp:29
std::chrono::nanoseconds Nanoseconds
Duration type aliases (compatible with std::chrono but using uint64_t)
Definition timestamp.hpp:37
std::chrono::microseconds Microseconds
Definition timestamp.hpp:38
std::chrono::milliseconds Milliseconds
Definition timestamp.hpp:39
std::chrono::hours Hours
Definition timestamp.hpp:42
std::chrono::seconds Seconds
Definition timestamp.hpp:40
std::chrono::minutes Minutes
Definition timestamp.hpp:41