Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TimeBase Class Reference

Helper class for implementing fast clocks. More...

#include <TimeBase.hxx>

Inheritance diagram for TimeBase:
Atomic openlcb::BroadcastTime openlcb::BroadcastTimeClient openlcb::BroadcastTimeServer

Public Member Functions

void sync (const TimeBase &other)
 Synchronizes this time with a different clock.
 
std::pair< time_t, int16_t > time_and_rate_quarters ()
 Get the time as a value of seconds relative to the system epoch.
 
time_t compare_realtime (time_t t1, time_t t2)
 Get the difference in time scaled to real time.
 
time_t time ()
 Get the time as a value of seconds relative to the system epoch.
 
struct tm * gmtime_r (struct tm *result)
 Get the time as a standard struct tm.
 
int date (int *month, int *day)
 Get the date (month/day).
 
int day_of_week ()
 Get the day of the week.
 
int day_of_year ()
 Get the day of the year.
 
int year ()
 Get the year.
 
int16_t get_rate_quarters ()
 Report the clock rate as a 12-bit fixed point number (-512.00 to 511.75).
 
bool is_running ()
 Test of the clock is running.
 
bool is_started ()
 Test of the clock is started (rate could still be 0).
 
bool fast_sec_to_real_nsec_period (int16_t rate, time_t fast_sec, long long *real_nsec)
 Convert fast clock period to a period in real nsec.
 
bool real_nsec_to_fast_sec_period (int16_t rate, long long real_nsec, time_t *fast_sec)
 Convert period in real nsec to a fast clock period.
 
bool real_nsec_until_fast_time_abs (time_t fast_sec, long long *real_nsec)
 Convert a fast time to absolute nsec until it will occur.
 
bool fast_sec_to_real_nsec_period_abs (time_t fast_sec, long long *real_nsec)
 Convert fast clock absolute (negative or positive) period to a positive (absolute) period in real nsec.
 

Protected Attributes

long long timestamp_ {OSTime::get_monotonic()}
 OS time at the last time update.
 
time_t seconds_ {0}
 Clock time at the last time update.
 
int16_t rate_ {0}
 effective clock rate
 
uint16_t started_: 1
 true if clock is started
 

Additional Inherited Members

- Protected Member Functions inherited from Atomic
void lock ()
 
void unlock ()
 

Detailed Description

Helper class for implementing fast clocks.

Represents the fast time against the OS (monotonic) clock.

Definition at line 43 of file TimeBase.hxx.

Constructor & Destructor Documentation

◆ TimeBase()

TimeBase::TimeBase ( )
inline

Definition at line 46 of file TimeBase.hxx.

Member Function Documentation

◆ compare_realtime()

time_t TimeBase::compare_realtime ( time_t  t1,
time_t  t2 
)
inline

Get the difference in time scaled to real time.

Parameters
t1time 1 to compare
t2time 2 to compare
Returns
(t1 - t2) scaled to real time.

Definition at line 74 of file TimeBase.hxx.

◆ date()

int TimeBase::date ( int *  month,
int *  day 
)
inline

Get the date (month/day).

Parameters
monthmonth (1 to 12)
dayday of month (1 to 31)
Returns
0 upon success, else -1 on failure

Definition at line 118 of file TimeBase.hxx.

◆ day_of_week()

int TimeBase::day_of_week ( )
inline

Get the day of the week.

Returns
day of the week (0 - 6, Sunday - Saturday) upon success, else -1 on failure

Definition at line 133 of file TimeBase.hxx.

◆ day_of_year()

int TimeBase::day_of_year ( )
inline

Get the day of the year.

Returns
day of the year (0 - 365) upon success, else -1 on failure

Definition at line 145 of file TimeBase.hxx.

◆ fast_sec_to_real_nsec_period()

bool TimeBase::fast_sec_to_real_nsec_period ( int16_t  rate,
time_t  fast_sec,
long long *  real_nsec 
)
inline

Convert fast clock period to a period in real nsec.

Result will preserve sign.

Parameters
raterate to use in conversion
fast_secperiod in seconds in fast clock time
real_nsecperiod in nsec
Returns
true if successfull, else false if clock is not running

Definition at line 197 of file TimeBase.hxx.

◆ fast_sec_to_real_nsec_period_abs()

bool TimeBase::fast_sec_to_real_nsec_period_abs ( time_t  fast_sec,
long long *  real_nsec 
)
inline

Convert fast clock absolute (negative or positive) period to a positive (absolute) period in real nsec.

Caution!!! Not an atomic operation if called from a different thread or executor than the thread or executor being used by this object.

Parameters
secperiod in seconds in fast clock time
real_nsecperiod in nsec
Returns
true if successfull, else false if clock is not running

Definition at line 273 of file TimeBase.hxx.

◆ get_rate_quarters()

int16_t TimeBase::get_rate_quarters ( )
inline

Report the clock rate as a 12-bit fixed point number (-512.00 to 511.75).

Returns
clock rate

Definition at line 170 of file TimeBase.hxx.

◆ gmtime_r()

struct tm * TimeBase::gmtime_r ( struct tm *  result)
inline

Get the time as a standard struct tm.

Parameters
resulta pointer to the structure that will hold the result
Returns
pointer to the passed in result on success, nullptr on failure

Definition at line 108 of file TimeBase.hxx.

◆ is_running()

bool TimeBase::is_running ( )
inline

Test of the clock is running.

Running backwards (negative rate) also qualifies as running.

Returns
true if running, else false

Definition at line 178 of file TimeBase.hxx.

◆ is_started()

bool TimeBase::is_started ( )
inline

Test of the clock is started (rate could still be 0).

Returns
true if started, else false

Definition at line 186 of file TimeBase.hxx.

◆ real_nsec_to_fast_sec_period()

bool TimeBase::real_nsec_to_fast_sec_period ( int16_t  rate,
long long  real_nsec,
time_t *  fast_sec 
)
inline

Convert period in real nsec to a fast clock period.

Result will preserve sign.

Parameters
raterate to use in conversion
real_nsecperiod in nsec
fast_secperiod in seconds in fast clock time
Returns
true if successfull, else false if clock is not running

Definition at line 223 of file TimeBase.hxx.

◆ real_nsec_until_fast_time_abs()

bool TimeBase::real_nsec_until_fast_time_abs ( time_t  fast_sec,
long long *  real_nsec 
)
inline

Convert a fast time to absolute nsec until it will occur.

Caution!!! Not an atomic operation if called from a different thread or executor than the thread or executor being used by this object.

Parameters
fast_secseconds in rate time
real_nsecperiod in nsec until it will occure
Returns
true if successfull, else false if clock is not running

Definition at line 249 of file TimeBase.hxx.

◆ sync()

void TimeBase::sync ( const TimeBase other)
inline

Synchronizes this time with a different clock.

Parameters
othera different clock. The current clock will take over that other clocks's state.

Definition at line 53 of file TimeBase.hxx.

◆ time()

time_t TimeBase::time ( )
inline

Get the time as a value of seconds relative to the system epoch.

Returns
time in seconds relative to the system epoch

Definition at line 87 of file TimeBase.hxx.

◆ time_and_rate_quarters()

std::pair< time_t, int16_t > TimeBase::time_and_rate_quarters ( )
inline

Get the time as a value of seconds relative to the system epoch.

At the same time get an atomic matching pair of the rate

Returns
pair<time in seconds relative to the system epoch, rate>

Definition at line 64 of file TimeBase.hxx.

◆ year()

int TimeBase::year ( )
inline

Get the year.

Returns
year (0 - 4095) upon success, else -1 on failure

Definition at line 157 of file TimeBase.hxx.

Member Data Documentation

◆ rate_

int16_t TimeBase::rate_ {0}
protected

effective clock rate

Definition at line 291 of file TimeBase.hxx.

◆ seconds_

time_t TimeBase::seconds_ {0}
protected

Clock time at the last time update.

Definition at line 290 of file TimeBase.hxx.

◆ started_

uint16_t TimeBase::started_
protected

true if clock is started

Definition at line 293 of file TimeBase.hxx.

◆ timestamp_

long long TimeBase::timestamp_ {OSTime::get_monotonic()}
protected

OS time at the last time update.

Definition at line 288 of file TimeBase.hxx.


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