Ocean
Loading...
Searching...
No Matches
Timestamp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_TIMESTAMP_H
9#define META_OCEAN_BASE_TIMESTAMP_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Lock.h"
13
14#include <atomic>
15#include <cfloat>
16#include <limits>
17
18#ifndef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
19 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS) && defined(CLOCK_BOOTTIME)
20 #define OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
21 #endif
22#endif // OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
23
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
27 #endif
28#endif // OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
29
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
33 #endif
34#endif // OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
35
36#ifndef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
37 #if !defined(OCEAN_PLATFORM_BUILD_WINDOWS)
38 #define OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
39 #endif
40#endif
41
42namespace Ocean
43{
44
45// Forward declaration.
46class Timestamp;
47
48/**
49 * Definition of a vector holding Timestamp objects.
50 * @see Timestamp
51 * @ingroup base
52 */
53using Timestamps = std::vector<Timestamp>;
54
55/**
56 * This class implements a timestamp.
57 * The timestamp is specified by the number of seconds since 01.01.1970 00:00:00 in UTC time.<br>
58 * Depending on the underlying hardware the accuracy (update rate) of the timestamps can vary.<br>
59 * The timestamp class wraps a floating value with 64 bit precision.
60 * @ingroup base
61 */
62class OCEAN_BASE_EXPORT Timestamp
63{
64 public:
65
66 /// Definition of the number of milliseconds in one second.
67 static constexpr int64_t millisecondsPerSecond_ = 1000;
68
69 /// Definition of the number of microseconds in one second.
70 static constexpr int64_t microsecondsPerSecond_ = millisecondsPerSecond_ * 1000;
71
72 /// Definition of the number of nanoseconds in one second.
73 static constexpr int64_t nanosecondsPerSecond_ = microsecondsPerSecond_ * 1000;
74
75 public:
76
77 /**
78 * Creates a new timestamp with invalid time.
79 */
80 Timestamp() = default;
81
82 /**
83 * Creates a new timestamp.
84 * @param toNow Determines whether the timestamp holds the seconds since 01.01.1970 00:00:00 in UTC time, otherwise the timestamp will be initialized as invalid
85 */
86 explicit Timestamp(const bool toNow);
87
88 /**
89 * Creates a new timestamp with a given value.
90 * @param timestamp Timestamp value
91 */
92 inline explicit Timestamp(const double timestamp);
93
94 /**
95 * Sets the timestamp to the current time.
96 * The timestamp holds the seconds since 01.01.1970 00:00:00 in UTC time.
97 * @return Reference to this object
98 */
100
101 /**
102 * Sets the timestamp to invalid.
103 * @return Reference to this object
104 */
105 inline Timestamp& toInvalid();
106
107 /**
108 * Returns this timestamp in nanoseconds.
109 * @return The timestamp in nanoseconds, with range (-infinity, infinity)
110 */
111 inline int64_t nanoseconds() const;
112
113 /**
114 * Returns whether a specified amount of time has passed since this timestamp.
115 * This function returns 'thisTimestamp + seconds <= currentTimestamp'.
116 * @param seconds The number of seconds defining the time to check, with range [0, infinity)
117 * @param currentTimestamp The current timestamp to use for comparison
118 * @return True, if the time has passed or if this timestamp is invalid
119 */
120 inline bool hasTimePassed(const double seconds, const Timestamp& currentTimestamp = Timestamp(true)) const;
121
122 /**
123 * Returns whether the timestamp holds a valid time.
124 * @return True, if so
125 */
126 inline bool isValid() const;
127
128 /**
129 * Returns whether the timestamp holds an invalid time.
130 * @return True, if so
131 */
132 inline bool isInvalid() const;
133
134 /**
135 * Assign a new value.
136 * @param timestamp Value to assign
137 * @return Reference to this timestamp
138 */
139 inline Timestamp& operator=(const double timestamp);
140
141 /**
142 * Adds two timestamps.
143 * @param right The right timestamp
144 * @return New timestamp
145 */
146 inline Timestamp operator+(const Timestamp& right) const;
147
148 /**
149 * Adds and assigns two timestamps.
150 * @param right The right timestamp
151 * @return Reference to this timestamp
152 */
153 inline Timestamp& operator+=(const Timestamp& right);
154
155 /**
156 * Adds seconds to this timestamps.
157 * @param seconds The number of seconds to add, with range (-infinity, infinity)
158 * @return New timestamp
159 */
160 inline Timestamp operator+(const double seconds) const;
161
162 /**
163 * Adds and assigns seconds to this timestamps.
164 * @param seconds The number of seconds to add, with range (-infinity, infinity)
165 * @return Reference to this timestamp
166 */
167 inline Timestamp& operator+=(const double seconds);
168
169 /**
170 * Subtracts two timestamps.
171 * @param right The right timestamp
172 * @return New timestamp
173 */
174 inline Timestamp operator-(const Timestamp& right) const;
175
176 /**
177 * Subtracts and assigns two timestamps.
178 * @param right The right timestamp
179 * @return Reference to this timestamp
180 */
181 inline Timestamp& operator-=(const Timestamp& right);
182
183 /**
184 * Subtracts seconds from this timestamp.
185 * @param seconds The number of seconds to subtract, with range (-infinity, infinity)
186 * @return New timestamp
187 */
188 inline Timestamp operator-(const double seconds) const;
189
190 /**
191 * Subtracts and assigns seconds from this timestamp.
192 * @param seconds The number of seconds to subtract, with range (-infinity, infinity)
193 * @return Reference to this timestamp
194 */
195 inline Timestamp& operator-=(const double seconds);
196
197 /**
198 * Returns whether the this timestamp is lesser than the right one.
199 * @param right The right timestamp
200 * @return True, if so
201 */
202 inline bool operator<(const Timestamp& right) const;
203
204 /**
205 * Returns whether the this timestamp is lesser or equal to the right one.
206 * @param right The right timestamp
207 * @return True, if so
208 */
209 inline bool operator<=(const Timestamp& right) const;
210
211 /**
212 * Returns whether the this timestamp is greater than the right one.
213 * @param right The right timestamp
214 * @return True, if so
215 */
216 inline bool operator>(const Timestamp& right) const;
217
218 /**
219 * Returns whether the this timestamp is greater or equal to the right one.
220 * @param right The right timestamp
221 * @return True, if so
222 */
223 inline bool operator>=(const Timestamp& right) const;
224
225 /**
226 * Returns whether two timestamps are identical.
227 * @param right The right timestamp
228 * @return True, if so
229 */
230 inline bool operator==(const Timestamp& right) const;
231
232 /**
233 * Returns whether two timestamps are not identical.
234 * @param right The right timestamp
235 * @return True, if so
236 */
237 inline bool operator!=(const Timestamp& right) const;
238
239 /**
240 * Cast operator for the timestamp value.
241 * @return Timestamp
242 */
243 explicit inline operator double() const;
244
245 /**
246 * Hash function.
247 * @param timestamp The timestamp for which the hash value will be determined
248 * @return The resulting hash value
249 */
250 inline size_t operator()(const Timestamp& timestamp) const;
251
252 /**
253 * Converts seconds to milliseconds.
254 * @param seconds The seconds to convert, with range (-infinity, infinity)
255 * @return The resulting milliseconds
256 */
257 static constexpr int64_t seconds2milliseconds(const double seconds);
258
259 /**
260 * Converts seconds to microseconds.
261 * @param seconds The seconds to convert, with range (-infinity, infinity)
262 * @return The resulting microseconds
263 */
264 static constexpr int64_t seconds2microseconds(const double seconds);
265
266 /**
267 * Converts seconds to nanoseconds.
268 * @param seconds The seconds to convert, with range (-infinity, infinity)
269 * @return The resulting nanoseconds
270 */
271 static constexpr int64_t seconds2nanoseconds(const double seconds);
272
273 /**
274 * Converts milliseconds to seconds.
275 * @param milliseconds The milliseconds to convert, with range (-infinity, infinity)
276 * @return The resulting seconds
277 */
278 static constexpr double milliseconds2seconds(const int64_t milliseconds);
279
280 /**
281 * Converts microseconds to seconds.
282 * @param microseconds The microseconds to convert, with range (-infinity, infinity)
283 * @return The resulting seconds
284 */
285 static constexpr double microseconds2seconds(const int64_t microseconds);
286
287 /**
288 * Converts nanoseconds to seconds.
289 * @param nanoseconds The nanoseconds to convert, with range (-infinity, infinity)
290 * @return The resulting seconds
291 */
292 static constexpr double nanoseconds2seconds(const int64_t nanoseconds);
293
294 protected:
295
296 /**
297 * Returns the of an invalid timestamp.
298 * @return Invalid timestamp value
299 */
300 static constexpr double invalidTimestampValue();
301
302 protected:
303
304 /// Timestamp value.
305 double value_ = invalidTimestampValue();
306};
307
308/**
309 * This class is a helper class allowing to converter timestamps defined in a specific time domain to unix timestamps.
310 */
311class OCEAN_BASE_EXPORT TimestampConverter
312{
313 public:
314
315 /**
316 * Definition of individual time domains.
317 */
318 enum TimeDomain : uint32_t
319 {
320 /// An invalid time domain.
321 TD_INVALID = 0u,
322 /// The monotonically increasing time domain defined in nanoseconds, not increasing during system sleep.
324
325#ifdef OCEAN_BASE_TIMESTAMP_BOOTTIME_AVAILABLE
326 /// The monotonically increasing time domain defined in nanoseconds, increasing during system sleep, not available on Windows.
328#endif
329
330#ifdef OCEAN_BASE_TIMESTAMP_UPTIMERAW_AVAILABLE
331 /// The monotonically increasing time domain defined in nanoseconds, the time the system has been awake since the last time it was restarted.
333#endif
334
335#ifdef OCEAN_BASE_TIMESTAMP_VIRTUAL_COUNTER_REGISTER_AVAILABLE
337#endif
338
339#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
340 /// A custom POSIX clock id specified by the user.
341 TD_CUSTOM_POSIX
342#endif
343 };
344
345 /**
346 * Definition of an invalid value.
347 */
348 static constexpr int64_t invalidValue_ = std::numeric_limits<int64_t>::lowest();
349
350 public:
351
352 /**
353 * Creates an invalid converter object.
354 * @see isValid().
355 */
357
358 /**
359 * Default destructor.
360 */
362
363 /**
364 * Creates a new converter object for a specific time domain.
365 * @param timeDomain The time domain for which the converter will be created
366 * @param necessaryMeasurements The number of measurements necessary to determine the offset between the domain time and the unix time, with range [1, infinity)
367 */
368 explicit TimestampConverter(const TimeDomain timeDomain, const size_t necessaryMeasurements = 100);
369
370#ifdef OCEAN_BASE_TIMESTAMP_CUSTOM_POSIX_AVAILABLE
371
372 /**
373 * Creates a new converter object for a custom POSIX clock id.
374 * @param timeDomain The time domain for which the converter will be created, must be TD_CUSTOM_POSIX
375 * @param customPosixClockId The custom POSIX clock id to use for time conversion
376 * @param necessaryMeasurements The number of measurements necessary to determine the offset between the domain time and the unix time, with range [1, infinity)
377 */
378 explicit TimestampConverter(const TimeDomain timeDomain, const int customPosixClockId, const size_t necessaryMeasurements);
379#endif
380
381 /**
382 * Move constructor.
383 * @param converter The converter to be moved
384 */
385 inline TimestampConverter(TimestampConverter&& converter) noexcept;
386
387 /**
388 * Converts a timestamp defined in the converter's time domain to a unix timestamp.
389 * @param domainTimestampNs The timestamp in the converter's time domain, in nanoseconds, with range (-infinity, infinity)
390 * @return The converted unix timestamp
391 */
392 Timestamp toUnix(const int64_t domainTimestampNs);
393
394 /**
395 * Converts a timestamp defined in the converter's time domain to a unix timestamp.
396 * @param domainTimestampSeconds The timestamp in the converter's time domain, in seconds, with range (-infinity, infinity)
397 * @return The converted unix timestamp
398 */
399 Timestamp toUnix(const double domainTimestampSeconds);
400
401 /**
402 * Returns whether a given domain timestamp is within a specified range of the current domain timestamp.
403 * @param domainTimestampNs The domain timestamp to check, in nanoseconds, with range (-infinity, infinity)
404 * @param maxDistance The maximal distance between the domain timestamp and the current domain timestamp, in seconds, with range [0, infinity)
405 * @param distance Optional resulting distance between the domain timestamp and the current domain timestamp, in seconds, with range (-infinity, infinity)
406 * @return True, if so
407 */
408 bool isWithinRange(const int64_t domainTimestampNs, const double maxDistance = 1.0, double* distance = nullptr);
409
410 /**
411 * Returns the current timestamp in the time domain of this converter.
412 * @return The current timestamp in the converter's time domain, in nanoseconds, invalidValue_ in case of an error
413 */
415
416 /**
417 * Returns the time domain of this converter.
418 * @return The converter's time domain
419 */
420 inline TimeDomain timeDomain() const;
421
422 /**
423 * Returns the offset between the domain time and the unix time, in nanoseconds.
424 * Unix time = domain time + domainToUnixOffset
425 * @return The offset between the domain time and the unix time, in nanoseconds, with range (-infinity, infinity)
426 */
428
429 /**
430 * Returns the number of measurements.
431 * @return The converter's number of measurements
432 */
433 inline size_t measurements() const;
434
435 /**
436 * Returns whether this converter has been initialized with a valid time domain.
437 * @return True, if so
438 */
439 inline bool isValid() const;
440
441 /**
442 * Returns whether this converter is valid.
443 * @return True, if so
444 */
445 inline operator bool() const;
446
447 /**
448 * Move operator.
449 * @param converter The converter to be moved
450 * @return Reference to this object
451 */
453
454 /**
455 * Returns the current timestamp in a specified time domain.
456 * @param timeDomain The time domain for which the current timestamp will be returned
457 * @return The current timestamp in the specified time domain, in nanoseconds
458 */
459 static int64_t currentDomainTimestampNs(const TimeDomain timeDomain);
460
461 /**
462 * Converts a timestamp which is defined in seconds to a timestamp which is defined in nanoseconds.
463 * The timestamp is defined by (timeValue / timeDenominator) * 1s.<br>
464 * The resulting timestamp will be (newTimeValue / 1000000000) * 1s.
465 * @param timeValue The time value, in seconds, with range (-infinity, infinity)
466 * @param timeDenominator The denominator corresponding to the time value (sometimes call time scale), with range [1, infinity)
467 * @return The resulting timestamp in nanoseconds, with range (-infinity, infinity)
468 */
469 static int64_t timestampInNs(const int64_t timeValue, const int64_t timeDenominator);
470
471#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
472
473 /**
474 * Return the current timestamp in a specified POSIX clock id.
475 * @param posixClockId The POSIX clock id for which the current timestamp will be returned
476 * @return The current timestamp in the specified POSIX clock id, in nanoseconds
477 */
478 static int64_t currentTimestampNs(const int posixClockId);
479
480 protected:
481
482 /**
483 * Returns the POSIX clock id associated with a time domain.
484 * @param timeDomain The time domain for which the associated POSIX clock id will be returned
485 * @return The POSIX clock id associated with the specified time domain, -1 if no associated POSIX clock id exists
486 */
487 static int posixClockId(const TimeDomain timeDomain);
488
489#endif // OCEAN_PLATFORM_BUILD_WINDOWS
490
491 protected:
492
493 /// The time domain of this converter.
494 TimeDomain timeDomain_ = TD_INVALID;
495
496 /// The offset between the domain time and the unix time, in nanoseconds.
497 std::atomic_int64_t domainToUnixOffsetNs_ = invalidValue_;
498
499 /// The initial domain timestamp, in nanoseconds.
500 int64_t initialDomainNs_ = invalidValue_;
501
502 /// The initial unix timestamp, in nanoseconds.
503 int64_t initialUnixNs_ = invalidValue_;
504
505 /// The measured sum of the domain to unix offsets, in nanoseconds.
506 int64_t sumDomainToUnixOffsetNs_ = 0;
507
508 /// The number of measurements.
509 size_t measurements_ = 0;
510
511 /// The number of necessary measurements before the converter keeps the determined offset fixed.
512 size_t necessaryMeasurements_ = 0;
513
514 /// The optional frequency of the domain time, in Hz (in case the domain time is not defined in nanoseconds).
515 uint64_t domainFrequency_ = 0ull;
516
517 /// The converter's lock.
518 mutable Lock lock_;
519
520#ifndef OCEAN_PLATFORM_BUILD_WINDOWS
521 /// The POSIX clock id associated with the time domain.
522 int domainPosixClockId_ = -1;
523#endif
524};
525
526inline Timestamp::Timestamp(const double timestamp) :
527 value_(timestamp)
528{
529 // nothing to do here
530}
531
533{
535
536 return *this;
537}
538
539inline int64_t Timestamp::nanoseconds() const
540{
541 ocean_assert(isValid());
542
544}
545
546inline bool Timestamp::hasTimePassed(const double seconds, const Timestamp& currentTimestamp) const
547{
548 ocean_assert(seconds >= 0.0);
549 ocean_assert(currentTimestamp.isValid());
550
551 if (!isValid())
552 {
553 return true;
554 }
555
556 return double(*this) + seconds <= double(currentTimestamp);
557}
558
559inline bool Timestamp::isValid() const
560{
561 return value_ != invalidTimestampValue();
562}
563
564inline bool Timestamp::isInvalid() const
565{
566 return value_ == invalidTimestampValue();
567}
568
569inline Timestamp& Timestamp::operator=(const double timestamp)
570{
571 value_ = timestamp;
572
573 return *this;
574}
575
576inline Timestamp Timestamp::operator+(const Timestamp& right) const
577{
578 return Timestamp(value_ + right.value_);
579}
580
582{
583 value_ += right.value_;
584
585 return *this;
586}
587
588inline Timestamp Timestamp::operator+(const double seconds) const
589{
590 return Timestamp(value_ + seconds);
591}
592
593inline Timestamp& Timestamp::operator+=(const double seconds)
594{
595 value_ += seconds;
596
597 return *this;
598}
599
600inline Timestamp Timestamp::operator-(const Timestamp& right) const
601{
602 return Timestamp(value_ - right.value_);
603}
604
606{
607 value_ -= right.value_;
608
609 return *this;
610}
611
612inline Timestamp Timestamp::operator-(const double seconds) const
613{
614 return Timestamp(value_ - seconds);
615}
616
617inline Timestamp& Timestamp::operator-=(const double seconds)
618{
619 value_ -= seconds;
620
621 return *this;
622}
623
624inline bool Timestamp::operator<(const Timestamp& right) const
625{
626 return value_ < right.value_;
627}
628
629inline bool Timestamp::operator<=(const Timestamp& right) const
630{
631 return value_ <= right.value_;
632}
633
634inline bool Timestamp::operator>(const Timestamp& right) const
635{
636 return value_ > right.value_;
637}
638
639inline bool Timestamp::operator>=(const Timestamp& right) const
640{
641 return value_ >= right.value_;
642}
643
644inline bool Timestamp::operator==(const Timestamp& right) const
645{
646 return value_ == right.value_;
647}
648
649inline bool Timestamp::operator!=(const Timestamp& right) const
650{
651 return value_ != right.value_;
652}
653
654inline Timestamp::operator double() const
655{
656 return value_;
657}
658
659inline size_t Timestamp::operator()(const Timestamp& timestamp) const
660{
661 return std::hash<double>{}(double(timestamp));
662}
663
664constexpr int64_t Timestamp::seconds2milliseconds(const double seconds)
665{
666 // 1000 milliseconds = 1 second
667
668 if (seconds >= 0.0)
669 {
670 return int64_t(seconds * double(millisecondsPerSecond_) + 0.5);
671 }
672 else
673 {
674 return int64_t(seconds * double(millisecondsPerSecond_) - 0.5);
675 }
676}
677
678constexpr int64_t Timestamp::seconds2microseconds(const double seconds)
679{
680 // 1000 milliseconds = 1 second
681 // 1000 microseconds = 1 milliseconds
682
683 if (seconds >= 0.0)
684 {
685 return int64_t(seconds * double(microsecondsPerSecond_) + 0.5);
686 }
687 else
688 {
689 return int64_t(seconds * double(microsecondsPerSecond_) - 0.5);
690 }
691}
692
693constexpr int64_t Timestamp::seconds2nanoseconds(const double seconds)
694{
695 // 1000 milliseconds = 1 second
696 // 1000 microseconds = 1 milliseconds
697 // 1000 nanoseconds = 1 microseconds
698
699 if (seconds >= 0.0)
700 {
701 return int64_t(seconds * double(nanosecondsPerSecond_) + 0.5);
702 }
703 else
704 {
705 return int64_t(seconds * double(nanosecondsPerSecond_) - 0.5);
706 }
707}
708
709constexpr double Timestamp::milliseconds2seconds(const int64_t milliseconds)
710{
711 return double(milliseconds) / double(millisecondsPerSecond_);
712}
713
714constexpr double Timestamp::microseconds2seconds(const int64_t microseconds)
715{
716 return double(microseconds) / double(microsecondsPerSecond_);
717}
718
719constexpr double Timestamp::nanoseconds2seconds(const int64_t nanoseconds)
720{
721 return double(nanoseconds) / double(nanosecondsPerSecond_);
722}
723
725{
726 return -DBL_MAX;
727}
728
730{
731 *this = std::move(converter);
732}
733
738
740{
741 const ScopedLock scopedLock(lock_);
742
743 return measurements_;
744}
745
747{
749}
750
751inline TimestampConverter::operator bool() const
752{
753 return isValid();
754}
755
756}
757
758#endif // META_OCEAN_BASE_TIMESTAMP_H
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 &timestamp) 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 &currentTimestamp=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