Ocean
Loading...
Searching...
No Matches
Ocean::Timestamp::TimestampConverter Class Reference

This class is a helper class allowing to converter timestamps defined in a specific time domain to unix timestamps. More...

#include <Timestamp.h>

Public Types

enum  TimeDomain : uint32_t { TD_INVALID = 0u , TD_MONOTONIC , TD_BOOTTIME , TD_UPTIME_RAW }
 Definition of individual time domains. More...
 

Public Member Functions

 TimestampConverter ()=default
 Creates an invalid converter object.
 
 TimestampConverter (const TimeDomain timeDomain, const size_t necessaryMeasurements=100)
 Creates a new converter object for a specific time domain.
 
 TimestampConverter (TimestampConverter &&converter)
 Move constructor.
 
Timestamp toUnix (const int64_t domainTimestampNs)
 Converts a timestamp defined in the converter's time domain to a unix timestamp.
 
Timestamp toUnix (const double domainTimestampSeconds)
 Converts a timestamp defined in the converter's time domain to a unix timestamp.
 
bool isWithinRange (const int64_t domainTimestampNs, const double maxDistance=1.0, double *distance=nullptr)
 Returns whether a given domain timestamp is within a specified range of the current domain timestamp.
 
TimeDomain timeDomain () const
 Returns the time domain of this converter.
 
int64_t domainToUnixOffset ()
 Returns the offset between the domain time and the unix time, in nanoseconds.
 
bool isValid () const
 Returns whether this converter has been initialized with a valid time domain.
 
 operator bool () const
 Returns whether this converter is valid.
 
TimestampConverteroperator= (TimestampConverter &&converter)
 Move operator.
 

Static Public Member Functions

static int64_t currentTimestampNs (const TimeDomain timeDomain)
 Returns the current timestamp in a specified time domain.
 
static int64_t currentTimestampNs (const int posixClockId)
 Return the current timestamp in a specified POSIX clock id.
 

Static Public Attributes

static constexpr int64_t invalidValue_ = std::numeric_limits<int64_t>::lowest()
 Definition of an invalid value.
 

Static Protected Member Functions

static int posixClockId (const TimeDomain timeDomain)
 Returns the POSIX clock id associated with a time domain.
 

Protected Attributes

TimeDomain timeDomain_ = TD_INVALID
 The time domain of this converter.
 
std::atomic_int64_t domainToUnixOffsetNs_ = invalidValue_
 The offset between the domain time and the unix time, in nanoseconds.
 
int64_t initialDomainNs_ = invalidValue_
 The initial domain timestamp, in nanoseconds.
 
int64_t initialUnixNs_ = invalidValue_
 The initial unix timestamp, in nanoseconds.
 
int64_t sumDomainToUnixOffsetNs_ = 0
 The measured sum of the domain to unix offsets, in nanoseconds.
 
size_t measurements_ = 0
 The number of measurements.
 
size_t necessaryMeasurements_ = 0
 The number of necessary measurements before the converter keeps the determined offset fixed.
 
Lock lock_
 The converter's lock.
 
int domainPosixClockId_ = -1
 The POSIX clock id associated with the time domain.
 

Detailed Description

This class is a helper class allowing to converter timestamps defined in a specific time domain to unix timestamps.

Member Enumeration Documentation

◆ TimeDomain

Definition of individual time domains.

Enumerator
TD_INVALID 

An invalid time domain.

TD_MONOTONIC 

The monotonically increasing time domain defined in nanoseconds, not increasing during system sleep.

TD_BOOTTIME 

The monotonically increasing time domain defined in nanoseconds, increasing during system sleep, not available on Windows.

TD_UPTIME_RAW 

The monotonically increasing time domain defined in nanoseconds, the time the system has been awake since the last time it was restarted.

Constructor & Destructor Documentation

◆ TimestampConverter() [1/3]

Ocean::Timestamp::TimestampConverter::TimestampConverter ( )
default

Creates an invalid converter object.

See also
isValid().

◆ TimestampConverter() [2/3]

Ocean::Timestamp::TimestampConverter::TimestampConverter ( const TimeDomain  timeDomain,
const size_t  necessaryMeasurements = 100 
)
explicit

Creates a new converter object for a specific time domain.

Parameters
timeDomainThe time domain for which the converter will be created
necessaryMeasurementsThe number of measurements necessary to determine the offset between the domain time and the unix time, with range [1, infinity)

◆ TimestampConverter() [3/3]

Ocean::Timestamp::TimestampConverter::TimestampConverter ( TimestampConverter &&  converter)
inline

Move constructor.

Parameters
converterThe converter to be moved

Member Function Documentation

◆ currentTimestampNs() [1/2]

static int64_t Ocean::Timestamp::TimestampConverter::currentTimestampNs ( const int  posixClockId)
static

Return the current timestamp in a specified POSIX clock id.

Parameters
posixClockIdThe POSIX clock id for which the current timestamp will be returned
Returns
The current timestamp in the specified POSIX clock id, in nanoseconds

◆ currentTimestampNs() [2/2]

static int64_t Ocean::Timestamp::TimestampConverter::currentTimestampNs ( const TimeDomain  timeDomain)
static

Returns the current timestamp in a specified time domain.

Parameters
timeDomainThe time domain for which the current timestamp will be returned
Returns
The current timestamp in the specified time domain, in nanoseconds

◆ domainToUnixOffset()

int64_t Ocean::Timestamp::TimestampConverter::domainToUnixOffset ( )

Returns the offset between the domain time and the unix time, in nanoseconds.

Unix time = domain time + domainToUnixOffset

Returns
The offset between the domain time and the unix time, in nanoseconds, with range (-infinity, infinity)

◆ isValid()

bool Ocean::Timestamp::TimestampConverter::isValid ( ) const
inline

Returns whether this converter has been initialized with a valid time domain.

Returns
True, if so

◆ isWithinRange()

bool Ocean::Timestamp::TimestampConverter::isWithinRange ( const int64_t  domainTimestampNs,
const double  maxDistance = 1.0,
double *  distance = nullptr 
)

Returns whether a given domain timestamp is within a specified range of the current domain timestamp.

Parameters
domainTimestampNsThe domain timestamp to check, in nanoseconds, with range (-infinity, infinity)
maxDistanceThe maximal distance between the domain timestamp and the current domain timestamp, in seconds, with range [0, infinity)
distanceOptional resulting distance between the domain timestamp and the current domain timestamp, in seconds, with range (-infinity, infinity)
Returns
True, if so

◆ operator bool()

Ocean::Timestamp::TimestampConverter::operator bool ( ) const
inline

Returns whether this converter is valid.

Returns
True, if so

◆ operator=()

TimestampConverter & Ocean::Timestamp::TimestampConverter::operator= ( TimestampConverter &&  converter)

Move operator.

Parameters
converterThe converter to be moved
Returns
Reference to this object

◆ posixClockId()

static int Ocean::Timestamp::TimestampConverter::posixClockId ( const TimeDomain  timeDomain)
staticprotected

Returns the POSIX clock id associated with a time domain.

Parameters
timeDomainThe time domain for which the associated POSIX clock id will be returned
Returns
The POSIX clock id associated with the specified time domain, -1 if no associated POSIX clock id exists

◆ timeDomain()

Timestamp::TimestampConverter::TimeDomain Ocean::Timestamp::TimestampConverter::timeDomain ( ) const
inline

Returns the time domain of this converter.

Returns
The converter's time domain

◆ toUnix() [1/2]

Timestamp Ocean::Timestamp::TimestampConverter::toUnix ( const double  domainTimestampSeconds)

Converts a timestamp defined in the converter's time domain to a unix timestamp.

Parameters
domainTimestampSecondsThe timestamp in the converter's time domain, in seconds, with range (-infinity, infinity)
Returns
The converted unix timestamp

◆ toUnix() [2/2]

Timestamp Ocean::Timestamp::TimestampConverter::toUnix ( const int64_t  domainTimestampNs)

Converts a timestamp defined in the converter's time domain to a unix timestamp.

Parameters
domainTimestampNsThe timestamp in the converter's time domain, in nanoseconds, with range (-infinity, infinity)
Returns
The converted unix timestamp

Field Documentation

◆ domainPosixClockId_

int Ocean::Timestamp::TimestampConverter::domainPosixClockId_ = -1
protected

The POSIX clock id associated with the time domain.

◆ domainToUnixOffsetNs_

std::atomic_int64_t Ocean::Timestamp::TimestampConverter::domainToUnixOffsetNs_ = invalidValue_
protected

The offset between the domain time and the unix time, in nanoseconds.

◆ initialDomainNs_

int64_t Ocean::Timestamp::TimestampConverter::initialDomainNs_ = invalidValue_
protected

The initial domain timestamp, in nanoseconds.

◆ initialUnixNs_

int64_t Ocean::Timestamp::TimestampConverter::initialUnixNs_ = invalidValue_
protected

The initial unix timestamp, in nanoseconds.

◆ invalidValue_

constexpr int64_t Ocean::Timestamp::TimestampConverter::invalidValue_ = std::numeric_limits<int64_t>::lowest()
staticconstexpr

Definition of an invalid value.

◆ lock_

Lock Ocean::Timestamp::TimestampConverter::lock_
protected

The converter's lock.

◆ measurements_

size_t Ocean::Timestamp::TimestampConverter::measurements_ = 0
protected

The number of measurements.

◆ necessaryMeasurements_

size_t Ocean::Timestamp::TimestampConverter::necessaryMeasurements_ = 0
protected

The number of necessary measurements before the converter keeps the determined offset fixed.

◆ sumDomainToUnixOffsetNs_

int64_t Ocean::Timestamp::TimestampConverter::sumDomainToUnixOffsetNs_ = 0
protected

The measured sum of the domain to unix offsets, in nanoseconds.

◆ timeDomain_

TimeDomain Ocean::Timestamp::TimestampConverter::timeDomain_ = TD_INVALID
protected

The time domain of this converter.


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