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
68 static constexpr int64_t millisecondsPerSecond_ = 1000;
71 static constexpr int64_t microsecondsPerSecond_ = millisecondsPerSecond_ * 1000;
74 static constexpr int64_t nanosecondsPerSecond_ = microsecondsPerSecond_ * 1000;
93 inline explicit Timestamp(
const double timestamp);
112 inline int64_t nanoseconds()
const;
121 inline bool hasTimePassed(
const double seconds,
const Timestamp& currentTimestamp =
Timestamp(
true))
const;
127 inline bool isValid()
const;
133 inline bool isInvalid()
const;
140 inline Timestamp& operator=(
const double timestamp);
168 inline Timestamp& operator+=(
const double seconds);
196 inline Timestamp& operator-=(
const double seconds);
203 inline bool operator<(
const Timestamp& right)
const;
210 inline bool operator<=(
const Timestamp& right)
const;
217 inline bool operator>(
const Timestamp& right)
const;
224 inline bool operator>=(
const Timestamp& right)
const;
231 inline bool operator==(
const Timestamp& right)
const;
238 inline bool operator!=(
const Timestamp& right)
const;
244 explicit inline operator double()
const;
251 inline size_t operator()(
const Timestamp& timestamp)
const;
258 static constexpr int64_t seconds2milliseconds(
const double seconds);
265 static constexpr int64_t seconds2microseconds(
const double seconds);
272 static constexpr int64_t seconds2nanoseconds(
const double seconds);
279 static constexpr double milliseconds2seconds(
const int64_t milliseconds);
286 static constexpr double microseconds2seconds(
const int64_t microseconds);
293 static constexpr double nanoseconds2seconds(
const int64_t nanoseconds);
301 static constexpr double invalidTimestampValue();
306 double value_ = invalidTimestampValue();
326#ifdef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
331#ifdef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
336#ifdef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
337 TD_VIRTUAL_COUNTER_REGISTER = 4u,
340#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
349 static constexpr int64_t invalidValue_ = std::numeric_limits<int64_t>::lowest();
393 int64_t
domainToUnixOffsetNs(
const int64_t currentDomainTimestampNs,
const int64_t currentUnixTimestampNs,
bool& isFinal);
400 inline size_t measurements()
const;
406 inline bool isValid()
const;
411 int64_t initialDomainNs_ = invalidValue_;
414 int64_t initialUnixNs_ = invalidValue_;
417 int64_t sumDomainToUnixOffsetNs_ = 0;
420 size_t measurements_ = 0;
423 size_t necessaryMeasurements_ = 0;
426 bool useSlidingWindow_ =
false;
453#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
493 bool isWithinRange(
const int64_t domainTimestampNs,
const double maxDistance = 1.0,
double* distance =
nullptr);
518 inline size_t measurements()
const;
524 inline bool isValid()
const;
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
593 std::atomic_int64_t domainToUnixOffsetNs_ = invalidValue_;
599 std::atomic<size_t> toggleCounter_ = 0;
602 uint64_t domainFrequency_ = 0ull;
607#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
609 int domainPosixClockId_ = -1;
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));
823 return necessaryMeasurements_ != 0;
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.
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
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
bool isValid() const
Returns whether this calculator is valid.
Definition Timestamp.h:821
This class is a helper class allowing to converter timestamps defined in a specific time domain to un...
Definition Timestamp.h:313
~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
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.
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.
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_INVALID
An invalid time domain.
Definition Timestamp.h:322
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
AutomaticDifferentiationT< T1, TNumeric1 > operator+(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition AutomaticDifferentiation.h:418
AutomaticDifferentiationT< T1, TNumeric1 > operator-(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition AutomaticDifferentiation.h:484