8#ifndef META_OCEAN_BASE_TIMESTAMP_H
9#define META_OCEAN_BASE_TIMESTAMP_H
18#ifndef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
19 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS) && defined(CLOCK_BOOTTIME)
20 #define OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
24#ifndef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
25 #if defined(OCEAN_PLATFORM_BUILD_APPLE) && defined(CLOCK_UPTIME_RAW)
26 #define OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
30#ifndef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
31 #if defined(OCEAN_PLATFORM_BUILD_ANDROID) && (defined(__arm__) || defined(__aarch64__))
32 #define OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
36#ifndef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
37 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS)
38 #define OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
67 static constexpr int64_t millisecondsPerSecond_ = 1000;
70 static constexpr int64_t microsecondsPerSecond_ = millisecondsPerSecond_ * 1000;
73 static constexpr int64_t nanosecondsPerSecond_ = microsecondsPerSecond_ * 1000;
92 inline explicit Timestamp(
const double timestamp);
111 inline int64_t nanoseconds()
const;
120 inline bool hasTimePassed(
const double seconds,
const Timestamp& currentTimestamp =
Timestamp(
true))
const;
126 inline bool isValid()
const;
132 inline bool isInvalid()
const;
139 inline Timestamp& operator=(
const double timestamp);
167 inline Timestamp& operator+=(
const double seconds);
195 inline Timestamp& operator-=(
const double seconds);
202 inline bool operator<(
const Timestamp& right)
const;
209 inline bool operator<=(
const Timestamp& right)
const;
216 inline bool operator>(
const Timestamp& right)
const;
223 inline bool operator>=(
const Timestamp& right)
const;
230 inline bool operator==(
const Timestamp& right)
const;
237 inline bool operator!=(
const Timestamp& right)
const;
243 explicit inline operator double()
const;
250 inline size_t operator()(
const Timestamp& timestamp)
const;
257 static constexpr int64_t seconds2milliseconds(
const double seconds);
264 static constexpr int64_t seconds2microseconds(
const double seconds);
271 static constexpr int64_t seconds2nanoseconds(
const double seconds);
278 static constexpr double milliseconds2seconds(
const int64_t milliseconds);
285 static constexpr double microseconds2seconds(
const int64_t microseconds);
292 static constexpr double nanoseconds2seconds(
const int64_t nanoseconds);
300 static constexpr double invalidTimestampValue();
305 double value_ = invalidTimestampValue();
325#ifdef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
330#ifdef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
335#ifdef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
339#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
348 static constexpr int64_t invalidValue_ = std::numeric_limits<int64_t>::lowest();
370#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
408 bool isWithinRange(
const int64_t domainTimestampNs,
const double maxDistance = 1.0,
double* distance =
nullptr);
433 inline size_t measurements()
const;
439 inline bool isValid()
const;
445 inline operator bool()
const;
469 static int64_t
timestampInNs(
const int64_t timeValue,
const int64_t timeDenominator);
471#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
497 std::atomic_int64_t domainToUnixOffsetNs_ = invalidValue_;
500 int64_t initialDomainNs_ = invalidValue_;
503 int64_t initialUnixNs_ = invalidValue_;
506 int64_t sumDomainToUnixOffsetNs_ = 0;
509 size_t measurements_ = 0;
512 size_t necessaryMeasurements_ = 0;
515 uint64_t domainFrequency_ = 0ull;
520#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
522 int domainPosixClockId_ = -1;
548 ocean_assert(seconds >= 0.0);
549 ocean_assert(currentTimestamp.
isValid());
556 return double(*
this) + seconds <= double(currentTimestamp);
654inline Timestamp::operator double()
const
661 return std::hash<double>{}(double(timestamp));
731 *
this = std::move(converter);
751inline 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 is a helper class allowing to converter timestamps defined in a specific time domain to un...
Definition Timestamp.h:312
~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 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.
int64_t currentDomainTimestampNs() const
Returns the current timestamp in the time domain of this converter.
TimeDomain timeDomain_
The time domain of this converter.
Definition Timestamp.h:494
size_t measurements() const
Returns the number of measurements.
Definition Timestamp.h:739
Timestamp toUnix(const double domainTimestampSeconds)
Converts a timestamp defined in the converter's time domain to a unix timestamp.
size_t measurements_
The number of measurements.
Definition Timestamp.h:509
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.
size_t necessaryMeasurements_
The number of necessary measurements before the converter keeps the determined offset fixed.
Definition Timestamp.h:512
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:746
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:734
TimeDomain
Definition of individual time domains.
Definition Timestamp.h:319
@ TD_VIRTUAL_COUNTER_REGISTER
Definition Timestamp.h:336
@ TD_BOOTTIME
The monotonically increasing time domain defined in nanoseconds, increasing during system sleep,...
Definition Timestamp.h:327
@ TD_UPTIME_RAW
The monotonically increasing time domain defined in nanoseconds, the time the system has been awake s...
Definition Timestamp.h:332
@ TD_MONOTONIC
The monotonically increasing time domain defined in nanoseconds, not increasing during system sleep.
Definition Timestamp.h:323
@ TD_INVALID
An invalid time domain.
Definition Timestamp.h:321
TimestampConverter & operator=(TimestampConverter &&converter) noexcept
Move operator.
TimestampConverter(const TimeDomain timeDomain, const size_t necessaryMeasurements=100)
Creates a new converter object for a specific time domain.
Lock lock_
The converter's lock.
Definition Timestamp.h:518
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:63
bool operator>=(const Timestamp &right) const
Returns whether the this timestamp is greater or equal to the right one.
Definition Timestamp.h:639
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition Timestamp.h:664
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition Timestamp.h:559
size_t operator()(const Timestamp ×tamp) const
Hash function.
Definition Timestamp.h:659
static constexpr int64_t millisecondsPerSecond_
Definition of the number of milliseconds in one second.
Definition Timestamp.h:67
static constexpr double nanoseconds2seconds(const int64_t nanoseconds)
Converts nanoseconds to seconds.
Definition Timestamp.h:719
bool operator!=(const Timestamp &right) const
Returns whether two timestamps are not identical.
Definition Timestamp.h:649
Timestamp(const bool toNow)
Creates a new timestamp.
static constexpr int64_t seconds2microseconds(const double seconds)
Converts seconds to microseconds.
Definition Timestamp.h:678
static constexpr double microseconds2seconds(const int64_t microseconds)
Converts microseconds to seconds.
Definition Timestamp.h:714
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:546
Timestamp & toInvalid()
Sets the timestamp to invalid.
Definition Timestamp.h:532
bool operator<=(const Timestamp &right) const
Returns whether the this timestamp is lesser or equal to the right one.
Definition Timestamp.h:629
bool operator==(const Timestamp &right) const
Returns whether two timestamps are identical.
Definition Timestamp.h:644
double value_
Timestamp value.
Definition Timestamp.h:305
static constexpr int64_t nanosecondsPerSecond_
Definition of the number of nanoseconds in one second.
Definition Timestamp.h:73
int64_t nanoseconds() const
Returns this timestamp in nanoseconds.
Definition Timestamp.h:539
bool operator>(const Timestamp &right) const
Returns whether the this timestamp is greater than the right one.
Definition Timestamp.h:634
bool operator<(const Timestamp &right) const
Returns whether the this timestamp is lesser than the right one.
Definition Timestamp.h:624
Timestamp operator-(const Timestamp &right) const
Subtracts two timestamps.
Definition Timestamp.h:600
Timestamp operator+(const Timestamp &right) const
Adds two timestamps.
Definition Timestamp.h:576
Timestamp & operator+=(const Timestamp &right)
Adds and assigns two timestamps.
Definition Timestamp.h:581
static constexpr int64_t seconds2nanoseconds(const double seconds)
Converts seconds to nanoseconds.
Definition Timestamp.h:693
bool isInvalid() const
Returns whether the timestamp holds an invalid time.
Definition Timestamp.h:564
Timestamp()=default
Creates a new timestamp with invalid time.
static constexpr double invalidTimestampValue()
Returns the of an invalid timestamp.
Definition Timestamp.h:724
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:70
static constexpr double milliseconds2seconds(const int64_t milliseconds)
Converts milliseconds to seconds.
Definition Timestamp.h:709
Timestamp & operator=(const double timestamp)
Assign a new value.
Definition Timestamp.h:569
Timestamp & operator-=(const Timestamp &right)
Subtracts and assigns two timestamps.
Definition Timestamp.h:605
std::vector< Timestamp > Timestamps
Definition of a vector holding Timestamp objects.
Definition Timestamp.h:53
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