8#ifndef META_OCEAN_BASE_TIMESTAMP_H
9#define META_OCEAN_BASE_TIMESTAMP_H
19#ifndef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
20 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS) && defined(CLOCK_BOOTTIME)
21 #define OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
25#ifndef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
26 #if defined(OCEAN_PLATFORM_BUILD_APPLE) && defined(CLOCK_UPTIME_RAW)
27 #define OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
31#ifndef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
32 #if defined(OCEAN_PLATFORM_BUILD_ANDROID) && (defined(__arm__) || defined(__aarch64__))
33 #define OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
37#ifndef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
38 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS)
39 #define OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
93 inline explicit Timestamp(
const double timestamp);
244 explicit inline operator double()
const;
326#ifdef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
331#ifdef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
336#ifdef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
340#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
349 static constexpr int64_t
invalidValue_ = std::numeric_limits<int64_t>::lowest();
453#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
493 bool isWithinRange(
const int64_t domainTimestampNs,
const double maxDistance = 1.0,
double* distance =
nullptr);
530 explicit inline operator bool()
const;
554 static int64_t
timestampInNs(
const int64_t timeValue,
const int64_t timeDenominator);
556#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
607#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
635 ocean_assert(seconds >= 0.0);
636 ocean_assert(currentTimestamp.
isValid());
643 return double(*
this) + seconds <= double(currentTimestamp);
741inline Timestamp::operator double()
const
748 return std::hash<double>{}(double(timestamp));
828 *
this = std::move(converter);
848inline TimestampConverter::operator bool()
const
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
This class encapsulates the logic for calculating the offset between a domain time and unix time.
Definition Timestamp.h:361
OffsetCalculator(const size_t necessaryMeasurements, const bool useSlidingWindow)
Creates a new offset calculator.
size_t necessaryMeasurements_
The number of necessary measurements before the converter keeps the determined offset fixed.
Definition Timestamp.h:423
bool useSlidingWindow_
True, if a sliding window is used to determine the average offset; False, to determine the average of...
Definition Timestamp.h:426
int64_t domainToUnixOffsetNs(const int64_t currentDomainTimestampNs, const int64_t currentUnixTimestampNs, bool &isFinal)
Calculates the domain-to-unix offset based on a new pair of timestamps.
std::deque< int64_t > OffsetQueue
Definition of a double-ended queue holding offsets between the domain time and the unix time.
Definition Timestamp.h:367
int64_t initialUnixNs_
The initial unix timestamp, in nanoseconds.
Definition Timestamp.h:414
OffsetQueue domainToUnixOffsetQueueNs_
The queue holding the domain to unix offsets, in case a sliding window is used to determine the avera...
Definition Timestamp.h:429
size_t measurements_
The number of measurements.
Definition Timestamp.h:420
OffsetCalculator()=default
Default constructor creating an invalid calculator.
size_t measurements() const
Returns the number of measurements accumulated so far.
Definition Timestamp.h:816
int64_t initialDomainNs_
The initial domain timestamp, in nanoseconds.
Definition Timestamp.h:411
int64_t sumDomainToUnixOffsetNs_
The measured sum of the domain to unix offsets, in nanoseconds.
Definition Timestamp.h:417
bool isValid() const
Returns whether this calculator is valid.
Definition Timestamp.h:821
~TimestampConverter()=default
Default destructor.
static int64_t currentDomainTimestampNs(const TimeDomain timeDomain)
Returns the current timestamp in a specified time domain.
TimestampConverter(const TimeDomain timeDomain, const bool useSlidingWindow, const int customPosixClockId, const size_t necessaryMeasurements)
Creates a new converter object for a custom POSIX clock id.
static int posixClockId(const TimeDomain timeDomain)
Returns the POSIX clock id associated with a time domain.
TimestampConverter(const TimeDomain timeDomain, const bool useSlidingWindow, const size_t necessaryMeasurements=100)
Creates a new converter object for a specific time domain.
int64_t currentDomainTimestampNs() const
Returns the current timestamp in the time domain of this converter.
OffsetCalculator offsetCalculator_
The calculator used to determine the offset between the domain time and the unix time.
Definition Timestamp.h:596
TimeDomain timeDomain_
The time domain of this converter.
Definition Timestamp.h:590
static constexpr int64_t invalidValue_
Definition of an invalid value.
Definition Timestamp.h:349
size_t measurements() const
Returns the number of measurements.
Definition Timestamp.h:836
TimestampConverter & operator=(const TimestampConverter &)=delete
Disabled copy operator.
Timestamp toUnix(const double domainTimestampSeconds)
Converts a timestamp defined in the converter's time domain to a unix timestamp.
TimestampConverter(const TimestampConverter &)=delete
Disabled copy constructor.
Timestamp toUnix(const int64_t domainTimestampNs)
Converts a timestamp defined in the converter's time domain to a unix timestamp.
std::atomic_int64_t domainToUnixOffsetNs_
The average offset between the domain time and the unix time, in nanoseconds.
Definition Timestamp.h:593
TimestampConverter()=default
Creates an invalid converter object.
static int64_t timestampInNs(const int64_t timeValue, const int64_t timeDenominator)
Converts a timestamp which is defined in seconds to a timestamp which is defined in nanoseconds.
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.
Definition Timestamp.h:843
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.
uint64_t domainFrequency_
The optional frequency of the domain time, in Hz (in case the domain time is not defined in nanosecon...
Definition Timestamp.h:602
TimeDomain timeDomain() const
Returns the time domain of this converter.
Definition Timestamp.h:831
TimeDomain
Definition of individual time domains.
Definition Timestamp.h:320
@ TD_VIRTUAL_COUNTER_REGISTER
Definition Timestamp.h:337
@ TD_BOOTTIME
The monotonically increasing time domain defined in nanoseconds, increasing during system sleep,...
Definition Timestamp.h:328
@ TD_UPTIME_RAW
The monotonically increasing time domain defined in nanoseconds, the time the system has been awake s...
Definition Timestamp.h:333
@ TD_MONOTONIC
The monotonically increasing time domain defined in nanoseconds, not increasing during system sleep.
Definition Timestamp.h:324
@ TD_CUSTOM_POSIX
A custom POSIX clock id specified by the user.
Definition Timestamp.h:342
@ TD_INVALID
An invalid time domain.
Definition Timestamp.h:322
std::atomic< size_t > toggleCounter_
Counter used to alternate the order of domain/unix timestamp sampling, reducing systematic measuremen...
Definition Timestamp.h:599
int domainPosixClockId_
The POSIX clock id associated with the time domain.
Definition Timestamp.h:609
TimestampConverter & operator=(TimestampConverter &&converter) noexcept
Move operator.
Lock lock_
The converter's lock.
Definition Timestamp.h:605
static int64_t currentTimestampNs(const int posixClockId)
Return the current timestamp in a specified POSIX clock id.
This class implements a timestamp.
Definition Timestamp.h:64
bool operator>=(const Timestamp &right) const
Returns whether the this timestamp is greater or equal to the right one.
Definition Timestamp.h:726
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition Timestamp.h:751
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition Timestamp.h:646
size_t operator()(const Timestamp ×tamp) const
Hash function.
Definition Timestamp.h:746
static constexpr int64_t millisecondsPerSecond_
Definition of the number of milliseconds in one second.
Definition Timestamp.h:68
static constexpr double nanoseconds2seconds(const int64_t nanoseconds)
Converts nanoseconds to seconds.
Definition Timestamp.h:806
bool operator!=(const Timestamp &right) const
Returns whether two timestamps are not identical.
Definition Timestamp.h:736
Timestamp(const bool toNow)
Creates a new timestamp.
static constexpr int64_t seconds2microseconds(const double seconds)
Converts seconds to microseconds.
Definition Timestamp.h:765
static constexpr double microseconds2seconds(const int64_t microseconds)
Converts microseconds to seconds.
Definition Timestamp.h:801
bool hasTimePassed(const double seconds, const Timestamp ¤tTimestamp=Timestamp(true)) const
Returns whether a specified amount of time has passed since this timestamp.
Definition Timestamp.h:633
Timestamp & toInvalid()
Sets the timestamp to invalid.
Definition Timestamp.h:619
bool operator<=(const Timestamp &right) const
Returns whether the this timestamp is lesser or equal to the right one.
Definition Timestamp.h:716
bool operator==(const Timestamp &right) const
Returns whether two timestamps are identical.
Definition Timestamp.h:731
double value_
Timestamp value.
Definition Timestamp.h:306
static constexpr int64_t nanosecondsPerSecond_
Definition of the number of nanoseconds in one second.
Definition Timestamp.h:74
int64_t nanoseconds() const
Returns this timestamp in nanoseconds.
Definition Timestamp.h:626
bool operator>(const Timestamp &right) const
Returns whether the this timestamp is greater than the right one.
Definition Timestamp.h:721
bool operator<(const Timestamp &right) const
Returns whether the this timestamp is lesser than the right one.
Definition Timestamp.h:711
Timestamp operator-(const Timestamp &right) const
Subtracts two timestamps.
Definition Timestamp.h:687
Timestamp operator+(const Timestamp &right) const
Adds two timestamps.
Definition Timestamp.h:663
Timestamp & operator+=(const Timestamp &right)
Adds and assigns two timestamps.
Definition Timestamp.h:668
static constexpr int64_t seconds2nanoseconds(const double seconds)
Converts seconds to nanoseconds.
Definition Timestamp.h:780
bool isInvalid() const
Returns whether the timestamp holds an invalid time.
Definition Timestamp.h:651
Timestamp()=default
Creates a new timestamp with invalid time.
static constexpr double invalidTimestampValue()
Returns the of an invalid timestamp.
Definition Timestamp.h:811
Timestamp & toNow()
Sets the timestamp to the current time.
static constexpr int64_t microsecondsPerSecond_
Definition of the number of microseconds in one second.
Definition Timestamp.h:71
static constexpr double milliseconds2seconds(const int64_t milliseconds)
Converts milliseconds to seconds.
Definition Timestamp.h:796
Timestamp & operator=(const double timestamp)
Assign a new value.
Definition Timestamp.h:656
Timestamp & operator-=(const Timestamp &right)
Subtracts and assigns two timestamps.
Definition Timestamp.h:692
std::vector< Timestamp > Timestamps
Definition of a vector holding Timestamp objects.
Definition Timestamp.h:54
The namespace covering the entire Ocean framework.
Definition Accessor.h:15