CommRaT 2.0.0
C++20 Real-Time Messaging Framework
Loading...
Searching...
No Matches
commrat::Time Class Reference

Time utility class - abstraction over clock sources. More...

#include <commrat/platform/timestamp.hpp>

Public Types

enum class  ClockSource {
  SYSTEM_CLOCK , STEADY_CLOCK , HIGH_RES_CLOCK , REALTIME_CLOCK ,
  MONOTONIC_CLOCK
}
 Clock source types. More...
 

Static Public Member Functions

static Timestamp now () noexcept
 Get current timestamp in nanoseconds.
 
static Timestamp get_timestamp (ClockSource source=ClockSource::STEADY_CLOCK) noexcept
 Get current timestamp from specific clock source.
 
static void set_clock_source (ClockSource source) noexcept
 Set default clock source for all future now() calls.
 
template<typename Rep , typename Period >
static constexpr Timestamp to_nanoseconds (std::chrono::duration< Rep, Period > duration) noexcept
 Convert std::chrono::duration to nanoseconds.
 
template<typename Duration >
static constexpr Duration from_nanoseconds (Timestamp ns) noexcept
 Convert nanoseconds to std::chrono::duration.
 
static constexpr Timestamp milliseconds_to_ns (uint64_t ms) noexcept
 Convert milliseconds to nanoseconds.
 
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 uint64_t ns_to_microseconds (Timestamp ns) noexcept
 Convert nanoseconds to microseconds.
 
static constexpr Timestamp diff (Timestamp t1, Timestamp t2) noexcept
 Calculate absolute time difference between two timestamps.
 
static constexpr bool is_within_tolerance (Timestamp timestamp, Timestamp target, Timestamp tolerance_ns) noexcept
 Check if timestamp is within tolerance of target.
 
static void sleep_ns (Timestamp ns) noexcept
 Sleep for specified nanoseconds.
 
template<typename Rep , typename Period >
static void sleep (std::chrono::duration< Rep, Period > duration) noexcept
 Sleep for specified duration.
 

Detailed Description

Time utility class - abstraction over clock sources.

Provides unified interface for getting timestamps and converting units. Can be configured to use different clock sources (system, monotonic, realtime).

Thread-safe: All methods are const and stateless.

Definition at line 52 of file timestamp.hpp.

Member Enumeration Documentation

◆ ClockSource

enum class commrat::Time::ClockSource
strong

Clock source types.

Enumerator
SYSTEM_CLOCK 

std::chrono::system_clock (wall time)

STEADY_CLOCK 

std::chrono::steady_clock (monotonic)

HIGH_RES_CLOCK 

std::chrono::high_resolution_clock

REALTIME_CLOCK 

CLOCK_REALTIME (future: PTP, NTP sync)

MONOTONIC_CLOCK 

CLOCK_MONOTONIC (future: realtime monotonic)

Definition at line 57 of file timestamp.hpp.

Member Function Documentation

◆ diff()

static constexpr Timestamp commrat::Time::diff ( Timestamp  t1,
Timestamp  t2 
)
inlinestaticconstexprnoexcept

Calculate absolute time difference between two timestamps.

Parameters
t1First timestamp
t2Second timestamp
Returns
Absolute difference in nanoseconds

Definition at line 166 of file timestamp.hpp.

Referenced by is_within_tolerance().

◆ from_nanoseconds()

template<typename Duration >
static constexpr Duration commrat::Time::from_nanoseconds ( Timestamp  ns)
inlinestaticconstexprnoexcept

Convert nanoseconds to std::chrono::duration.

Definition at line 127 of file timestamp.hpp.

◆ get_timestamp()

static Timestamp commrat::Time::get_timestamp ( ClockSource  source = ClockSource::STEADY_CLOCK)
inlinestaticnoexcept

Get current timestamp from specific clock source.

Parameters
sourceClock source to use
Returns
Timestamp in nanoseconds

Definition at line 82 of file timestamp.hpp.

References HIGH_RES_CLOCK, MONOTONIC_CLOCK, REALTIME_CLOCK, STEADY_CLOCK, and SYSTEM_CLOCK.

Referenced by now().

◆ is_within_tolerance()

static constexpr bool commrat::Time::is_within_tolerance ( Timestamp  timestamp,
Timestamp  target,
Timestamp  tolerance_ns 
)
inlinestaticconstexprnoexcept

Check if timestamp is within tolerance of target.

Parameters
timestampTimestamp to check
targetTarget timestamp
tolerance_nsTolerance in nanoseconds
Returns
true if |timestamp - target| <= tolerance

Definition at line 178 of file timestamp.hpp.

References diff().

◆ microseconds_to_ns()

static constexpr Timestamp commrat::Time::microseconds_to_ns ( uint64_t  us)
inlinestaticconstexprnoexcept

Convert microseconds to nanoseconds.

Definition at line 141 of file timestamp.hpp.

Referenced by commrat::literals::operator""_us().

◆ milliseconds_to_ns()

static constexpr Timestamp commrat::Time::milliseconds_to_ns ( uint64_t  ms)
inlinestaticconstexprnoexcept

Convert milliseconds to nanoseconds.

Definition at line 134 of file timestamp.hpp.

Referenced by commrat::literals::operator""_ms(), and commrat::literals::operator""_s().

◆ now()

static Timestamp commrat::Time::now ( )
inlinestaticnoexcept

Get current timestamp in nanoseconds.

Returns
Current time as uint64_t nanoseconds

Real-time safe: Yes (if using MONOTONIC_CLOCK)

Definition at line 72 of file timestamp.hpp.

References get_timestamp().

Referenced by commrat::LoopExecutor< ModuleType >::free_loop(), and commrat::LoopExecutor< ModuleType >::periodic_loop().

◆ ns_to_microseconds()

static constexpr uint64_t commrat::Time::ns_to_microseconds ( Timestamp  ns)
inlinestaticconstexprnoexcept

Convert nanoseconds to microseconds.

Definition at line 155 of file timestamp.hpp.

◆ ns_to_milliseconds()

static constexpr uint64_t commrat::Time::ns_to_milliseconds ( Timestamp  ns)
inlinestaticconstexprnoexcept

Convert nanoseconds to milliseconds.

Definition at line 148 of file timestamp.hpp.

◆ set_clock_source()

static void commrat::Time::set_clock_source ( ClockSource  source)
inlinestaticnoexcept

Set default clock source for all future now() calls.

Parameters
sourceClock source to use

Not thread-safe: Should be called once at initialization

Definition at line 109 of file timestamp.hpp.

◆ sleep()

template<typename Rep , typename Period >
static void commrat::Time::sleep ( std::chrono::duration< Rep, Period >  duration)
inlinestaticnoexcept

Sleep for specified duration.

Definition at line 201 of file timestamp.hpp.

Referenced by commrat::module_main().

◆ sleep_ns()

static void commrat::Time::sleep_ns ( Timestamp  ns)
inlinestaticnoexcept

Sleep for specified nanoseconds.

Uses high-resolution sleep if available.

Parameters
nsNanoseconds to sleep

Real-time safe: Depends on OS scheduler

Definition at line 193 of file timestamp.hpp.

◆ to_nanoseconds()

template<typename Rep , typename Period >
static constexpr Timestamp commrat::Time::to_nanoseconds ( std::chrono::duration< Rep, Period >  duration)
inlinestaticconstexprnoexcept

Convert std::chrono::duration to nanoseconds.

Definition at line 117 of file timestamp.hpp.


The documentation for this class was generated from the following file: