42using Hours = std::chrono::hours;
85 return system_clock_now();
89 return steady_clock_now();
92 return posix_clock_now(CLOCK_REALTIME);
95 return posix_clock_now(CLOCK_MONOTONIC);
98 return steady_clock_now();
110 current_clock_source_ = source;
116 template<
typename Rep,
typename Period>
119 std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()
126 template<
typename Duration>
128 return std::chrono::duration_cast<Duration>(std::chrono::nanoseconds(ns));
135 return ms * 1'000'000;
149 return ns / 1'000'000;
167 return (t1 > t2) ? (t1 - t2) : (t2 - t1);
181 return diff(timestamp, target) <= tolerance_ns;
194 std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
200 template<
typename Rep,
typename Period>
201 static void sleep(std::chrono::duration<Rep, Period> duration)
noexcept {
202 std::this_thread::sleep_for(duration);
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();
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();
219 static Timestamp posix_clock_now(clockid_t clock_id)
noexcept {
221 if (clock_gettime(clock_id, &ts) == 0) {
222 return static_cast<Timestamp>(ts.tv_sec) * 1'000'000'000 +
226 return steady_clock_now();
280 constexpr Timestamp operator""_ns(
unsigned long long ns)
noexcept {
284 constexpr Timestamp operator""_us(
unsigned long long us)
noexcept {
288 constexpr Timestamp operator""_ms(
unsigned long long ms)
noexcept {
292 constexpr Timestamp operator""_s(
unsigned long long s)
noexcept {
Time utility class - abstraction over clock sources.
static Timestamp now() noexcept
Get current timestamp in nanoseconds.
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.
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.
@ 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.
std::chrono::nanoseconds Nanoseconds
Duration type aliases (compatible with std::chrono but using uint64_t)
std::chrono::microseconds Microseconds
std::chrono::milliseconds Milliseconds
std::chrono::seconds Seconds
std::chrono::minutes Minutes