8#ifndef META_OCEAN_BASE_TIMESTAMP_H
9#define META_OCEAN_BASE_TIMESTAMP_H
18#if !defined(OCEAN_PLATFORM_BUILD_WINDOWS) && defined(CLOCK_BOOTTIME)
19 #define OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
22#if defined(OCEAN_PLATFORM_BUILD_APPLE) && defined(CLOCK_UPTIME_RAW)
23 #define OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
67#ifdef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
72#ifdef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
81 static constexpr int64_t invalidValue_ = std::numeric_limits<int64_t>::lowest();
125 bool isWithinRange(
const int64_t domainTimestampNs,
const double maxDistance = 1.0,
double* distance =
nullptr);
144 inline bool isValid()
const;
150 inline operator bool()
const;
166#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
192 std::atomic_int64_t domainToUnixOffsetNs_ = invalidValue_;
195 int64_t initialDomainNs_ = invalidValue_;
198 int64_t initialUnixNs_ = invalidValue_;
201 int64_t sumDomainToUnixOffsetNs_ = 0;
204 size_t measurements_ = 0;
207 size_t necessaryMeasurements_ = 0;
212#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
214 int domainPosixClockId_ = -1;
235 inline explicit Timestamp(
const double timestamp);
254 inline int64_t nanoseconds()
const;
263 inline bool hasTimePassed(
const double seconds,
const Timestamp& currentTimestamp =
Timestamp(
true))
const;
269 inline bool isValid()
const;
275 inline bool isInvalid()
const;
282 inline Timestamp& operator=(
const double timestamp);
310 inline Timestamp& operator+=(
const double seconds);
338 inline Timestamp& operator-=(
const double seconds);
345 inline bool operator<(
const Timestamp& right)
const;
352 inline bool operator<=(
const Timestamp& right)
const;
359 inline bool operator>(
const Timestamp& right)
const;
366 inline bool operator>=(
const Timestamp& right)
const;
373 inline bool operator==(
const Timestamp& right)
const;
380 inline bool operator!=(
const Timestamp& right)
const;
386 explicit inline operator double()
const;
393 inline size_t operator()(
const Timestamp& timestamp)
const;
400 static constexpr int64_t seconds2milliseconds(
const double seconds);
407 static constexpr int64_t seconds2microseconds(
const double seconds);
414 static constexpr int64_t seconds2nanoseconds(
const double seconds);
421 static constexpr double milliseconds2seconds(
const int64_t milliseconds);
428 static constexpr double microseconds2seconds(
const int64_t microseconds);
435 static constexpr double nanoseconds2seconds(
const int64_t nanoseconds);
443 static constexpr double invalidTimestampValue();
448 double value_ = invalidTimestampValue();
453 *
this = std::move(converter);
463 return timeDomain_ != TD_INVALID && necessaryMeasurements_ != 0;
466inline Timestamp::TimestampConverter::operator bool()
const
493 ocean_assert(seconds >= 0.0);
494 ocean_assert(currentTimestamp.
isValid());
501 return double(*
this) + seconds <= double(currentTimestamp);
599inline Timestamp::operator double()
const
606 return std::hash<double>{}(double(timestamp));
615 return int64_t(seconds * 1.0e3 + 0.5);
619 return int64_t(seconds * 1.0e3 - 0.5);
630 return int64_t(seconds * 1.0e6 + 0.5);
634 return int64_t(seconds * 1.0e6 - 0.5);
646 return int64_t(seconds * 1.0e9 + 0.5);
650 return int64_t(seconds * 1.0e9 - 0.5);
656 return double(milliseconds) / 1.0e3;
661 return double(microseconds) / 1.0e6;
This class implements a recursive lock object.
Definition Lock.h:31
This class is a helper class allowing to converter timestamps defined in a specific time domain to un...
Definition Timestamp.h:54
int64_t domainToUnixOffset()
Returns the offset between the domain time and the unix time, in nanoseconds.
TimeDomain timeDomain() const
Returns the time domain of this converter.
Definition Timestamp.h:456
TimeDomain
Definition of individual time domains.
Definition Timestamp.h:61
@ TD_MONOTONIC
The monotonically increasing time domain defined in nanoseconds, not increasing during system sleep.
Definition Timestamp.h:65
@ TD_UPTIME_RAW
The monotonically increasing time domain defined in nanoseconds, the time the system has been awake s...
Definition Timestamp.h:74
@ TD_BOOTTIME
The monotonically increasing time domain defined in nanoseconds, increasing during system sleep,...
Definition Timestamp.h:69
static int64_t currentTimestampNs(const TimeDomain timeDomain)
Returns the current timestamp in a specified time domain.
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.
bool isValid() const
Returns whether this converter has been initialized with a valid time domain.
Definition Timestamp.h:461
Timestamp toUnix(const double domainTimestampSeconds)
Converts a timestamp defined in the converter's time domain to a unix timestamp.
static int posixClockId(const TimeDomain timeDomain)
Returns the POSIX clock id associated with a time domain.
Timestamp toUnix(const int64_t domainTimestampNs)
Converts a timestamp defined in the converter's time domain to a unix timestamp.
Lock lock_
The converter's lock.
Definition Timestamp.h:210
TimestampConverter()=default
Creates an invalid converter object.
static int64_t currentTimestampNs(const int posixClockId)
Return the current timestamp in a specified POSIX clock id.
TimestampConverter(const TimeDomain timeDomain, const size_t necessaryMeasurements=100)
Creates a new converter object for a specific time domain.
TimestampConverter & operator=(TimestampConverter &&converter)
Move operator.
This class implements a timestamp.
Definition Timestamp.h:47
bool operator>=(const Timestamp &right) const
Returns whether the this timestamp is greater or equal to the right one.
Definition Timestamp.h:584
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition Timestamp.h:609
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition Timestamp.h:504
size_t operator()(const Timestamp ×tamp) const
Hash function.
Definition Timestamp.h:604
static constexpr double nanoseconds2seconds(const int64_t nanoseconds)
Converts nanoseconds to seconds.
Definition Timestamp.h:664
bool operator!=(const Timestamp &right) const
Returns whether two timestamps are not identical.
Definition Timestamp.h:594
Timestamp(const bool toNow)
Creates a new timestamp.
static constexpr int64_t seconds2microseconds(const double seconds)
Converts seconds to microseconds.
Definition Timestamp.h:623
static constexpr double microseconds2seconds(const int64_t microseconds)
Converts microseconds to seconds.
Definition Timestamp.h:659
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:491
Timestamp & toInvalid()
Sets the timestamp to invalid.
Definition Timestamp.h:477
bool operator<=(const Timestamp &right) const
Returns whether the this timestamp is lesser or equal to the right one.
Definition Timestamp.h:574
bool operator==(const Timestamp &right) const
Returns whether two timestamps are identical.
Definition Timestamp.h:589
double value_
Timestamp value.
Definition Timestamp.h:448
int64_t nanoseconds() const
Returns this timestamp in nanoseconds.
Definition Timestamp.h:484
bool operator>(const Timestamp &right) const
Returns whether the this timestamp is greater than the right one.
Definition Timestamp.h:579
bool operator<(const Timestamp &right) const
Returns whether the this timestamp is lesser than the right one.
Definition Timestamp.h:569
Timestamp operator-(const Timestamp &right) const
Subtracts two timestamps.
Definition Timestamp.h:545
Timestamp operator+(const Timestamp &right) const
Adds two timestamps.
Definition Timestamp.h:521
Timestamp & operator+=(const Timestamp &right)
Adds and assigns two timestamps.
Definition Timestamp.h:526
static constexpr int64_t seconds2nanoseconds(const double seconds)
Converts seconds to nanoseconds.
Definition Timestamp.h:638
bool isInvalid() const
Returns whether the timestamp holds an invalid time.
Definition Timestamp.h:509
Timestamp()=default
Creates a new timestamp with invalid time.
static constexpr double invalidTimestampValue()
Returns the of an invalid timestamp.
Definition Timestamp.h:669
Timestamp & toNow()
Sets the timestamp to the current time.
static constexpr double milliseconds2seconds(const int64_t milliseconds)
Converts milliseconds to seconds.
Definition Timestamp.h:654
Timestamp & operator=(const double timestamp)
Assign a new value.
Definition Timestamp.h:514
Timestamp & operator-=(const Timestamp &right)
Subtracts and assigns two timestamps.
Definition Timestamp.h:550
std::vector< Timestamp > Timestamps
Definition of a vector holding Timestamp objects.
Definition Timestamp.h:37
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