Ocean
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 
13 #include <cfloat>
14 
15 namespace Ocean
16 {
17 
18 // Forward declaration.
19 class Timestamp;
20 
21 /**
22  * Definition of a vector holding Timestamp objects.
23  * @see Timestamp
24  * @ingroup base
25  */
26 typedef std::vector<Timestamp> Timestamps;
27 
28 /**
29  * This class implements a timestamp.
30  * The timestamp is specified by the number of seconds since 01.01.1970 00:00:00 in UTC time.<br>
31  * Depending on the underlying hardware the accuracy (update rate) of the timestamps can vary.<br>
32  * The timestamp class wraps a floating value with 64 bit precision.
33  * @ingroup base
34  */
35 class OCEAN_BASE_EXPORT Timestamp
36 {
37  public:
38 
39  /**
40  * Creates a new timestamp with invalid time.
41  */
42  Timestamp() = default;
43 
44  /**
45  * Creates a new timestamp.
46  * @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
47  */
48  explicit Timestamp(const bool toNow);
49 
50  /**
51  * Creates a new timestamp with a given value.
52  * @param timestamp Timestamp value
53  */
54  inline explicit Timestamp(const double timestamp);
55 
56  /**
57  * Sets the timestamp to the current time.
58  * The timestamp holds the seconds since 01.01.1970 00:00:00 in UTC time.
59  * @return Reference to this object
60  */
62 
63  /**
64  * Sets the timestamp to invalid.
65  * @return Reference to this object
66  */
67  inline Timestamp& toInvalid();
68 
69  /**
70  * Returns this timestamp in nanoseconds.
71  * @return The timestamp in nanoseconds, with range (-infinity, infinity)
72  */
73  inline int64_t nanoseconds() const;
74 
75  /**
76  * Returns whether a specified amount of time has passed since this timestamp.
77  * This function returns 'thisTimestamp + seconds <= currentTimestamp'.
78  * @param seconds The number of seconds defining the time to check, with range [0, infinity)
79  * @param currentTimestamp The current timestamp to use for comparison
80  * @return True, if the time has passed or if this timestamp is invalid
81  */
82  inline bool hasTimePassed(const double seconds, const Timestamp& currentTimestamp = Timestamp(true)) const;
83 
84  /**
85  * Returns whether the timestamp holds a valid time.
86  * @return True, if so
87  */
88  inline bool isValid() const;
89 
90  /**
91  * Returns whether the timestamp holds an invalid time.
92  * @return True, if so
93  */
94  inline bool isInvalid() const;
95 
96  /**
97  * Assign a new value.
98  * @param timestamp Value to assign
99  * @return Reference to this timestamp
100  */
101  inline Timestamp& operator=(const double timestamp);
102 
103  /**
104  * Adds two timestamps.
105  * @param right The right timestamp
106  * @return New timestamp
107  */
108  inline Timestamp operator+(const Timestamp& right) const;
109 
110  /**
111  * Adds and assigns two timestamps.
112  * @param right The right timestamp
113  * @return Reference to this timestamp
114  */
115  inline Timestamp& operator+=(const Timestamp& right);
116 
117  /**
118  * Adds seconds to this timestamps.
119  * @param seconds The number of seconds to add, with range (-infinity, infinity)
120  * @return New timestamp
121  */
122  inline Timestamp operator+(const double seconds) const;
123 
124  /**
125  * Adds and assigns seconds to this timestamps.
126  * @param seconds The number of seconds to add, with range (-infinity, infinity)
127  * @return Reference to this timestamp
128  */
129  inline Timestamp& operator+=(const double seconds);
130 
131  /**
132  * Subtracts two timestamps.
133  * @param right The right timestamp
134  * @return New timestamp
135  */
136  inline Timestamp operator-(const Timestamp& right) const;
137 
138  /**
139  * Subtracts and assigns two timestamps.
140  * @param right The right timestamp
141  * @return Reference to this timestamp
142  */
143  inline Timestamp& operator-=(const Timestamp& right);
144 
145  /**
146  * Subtracts seconds from this timestamp.
147  * @param seconds The number of seconds to subtract, with range (-infinity, infinity)
148  * @return New timestamp
149  */
150  inline Timestamp operator-(const double seconds) const;
151 
152  /**
153  * Subtracts and assigns seconds from this timestamp.
154  * @param seconds The number of seconds to subtract, with range (-infinity, infinity)
155  * @return Reference to this timestamp
156  */
157  inline Timestamp& operator-=(const double seconds);
158 
159  /**
160  * Returns whether the this timestamp is lesser than the right one.
161  * @param right The right timestamp
162  * @return True, if so
163  */
164  inline bool operator<(const Timestamp& right) const;
165 
166  /**
167  * Returns whether the this timestamp is lesser or equal to the right one.
168  * @param right The right timestamp
169  * @return True, if so
170  */
171  inline bool operator<=(const Timestamp& right) const;
172 
173  /**
174  * Returns whether the this timestamp is greater than the right one.
175  * @param right The right timestamp
176  * @return True, if so
177  */
178  inline bool operator>(const Timestamp& right) const;
179 
180  /**
181  * Returns whether the this timestamp is greater or equal to the right one.
182  * @param right The right timestamp
183  * @return True, if so
184  */
185  inline bool operator>=(const Timestamp& right) const;
186 
187  /**
188  * Returns whether two timestamps are identical.
189  * @param right The right timestamp
190  * @return True, if so
191  */
192  inline bool operator==(const Timestamp& right) const;
193 
194  /**
195  * Returns whether two timestamps are not identical.
196  * @param right The right timestamp
197  * @return True, if so
198  */
199  inline bool operator!=(const Timestamp& right) const;
200 
201  /**
202  * Cast operator for the timestamp value.
203  * @return Timestamp
204  */
205  explicit inline operator double() const;
206 
207  /**
208  * Hash function.
209  * @param timestamp The timestamp for which the hash value will be determined
210  * @return The resulting hash value
211  */
212  inline size_t operator()(const Timestamp& timestamp) const;
213 
214  /**
215  * Converts seconds to milliseconds.
216  * @param seconds The seconds to convert, with range (-infinity, infinity)
217  * @return The resulting milliseconds
218  */
219  static constexpr int64_t seconds2milliseconds(const double seconds);
220 
221  /**
222  * Converts seconds to microseconds.
223  * @param seconds The seconds to convert, with range (-infinity, infinity)
224  * @return The resulting microseconds
225  */
226  static constexpr int64_t seconds2microseconds(const double seconds);
227 
228  /**
229  * Converts seconds to nanoseconds.
230  * @param seconds The seconds to convert, with range (-infinity, infinity)
231  * @return The resulting nanoseconds
232  */
233  static constexpr int64_t seconds2nanoseconds(const double seconds);
234 
235  /**
236  * Converts milliseconds to seconds.
237  * @param milliseconds The milliseconds to convert, with range (-infinity, infinity)
238  * @return The resulting seconds
239  */
240  static constexpr double milliseconds2seconds(const int64_t milliseconds);
241 
242  /**
243  * Converts microseconds to seconds.
244  * @param microseconds The microseconds to convert, with range (-infinity, infinity)
245  * @return The resulting seconds
246  */
247  static constexpr double microseconds2seconds(const int64_t microseconds);
248 
249  /**
250  * Converts nanoseconds to seconds.
251  * @param nanoseconds The nanoseconds to convert, with range (-infinity, infinity)
252  * @return The resulting seconds
253  */
254  static constexpr double nanoseconds2seconds(const int64_t nanoseconds);
255 
256  protected:
257 
258  /**
259  * Returns the of an invalid timestamp.
260  * @return Invalid timestamp value
261  */
262  static constexpr double invalidTimestampValue();
263 
264  protected:
265 
266  /// Timestamp value.
267  double value_ = invalidTimestampValue();
268 };
269 
270 inline Timestamp::Timestamp(const double timestamp) :
271  value_(timestamp)
272 {
273  // nothing to do here
274 }
275 
277 {
279 
280  return *this;
281 }
282 
283 inline int64_t Timestamp::nanoseconds() const
284 {
285  ocean_assert(isValid());
286 
287  return seconds2nanoseconds(value_);
288 }
289 
290 inline bool Timestamp::hasTimePassed(const double seconds, const Timestamp& currentTimestamp) const
291 {
292  ocean_assert(seconds >= 0.0);
293  ocean_assert(currentTimestamp.isValid());
294 
295  if (!isValid())
296  {
297  return true;
298  }
299 
300  return double(*this) + seconds <= double(currentTimestamp);
301 }
302 
303 inline bool Timestamp::isValid() const
304 {
305  return value_ != invalidTimestampValue();
306 }
307 
308 inline bool Timestamp::isInvalid() const
309 {
310  return value_ == invalidTimestampValue();
311 }
312 
313 inline Timestamp& Timestamp::operator=(const double timestamp)
314 {
315  value_ = timestamp;
316 
317  return *this;
318 }
319 
320 inline Timestamp Timestamp::operator+(const Timestamp& right) const
321 {
322  return Timestamp(value_ + right.value_);
323 }
324 
326 {
327  value_ += right.value_;
328 
329  return *this;
330 }
331 
332 inline Timestamp Timestamp::operator+(const double seconds) const
333 {
334  return Timestamp(value_ + seconds);
335 }
336 
337 inline Timestamp& Timestamp::operator+=(const double seconds)
338 {
339  value_ += seconds;
340 
341  return *this;
342 }
343 
344 inline Timestamp Timestamp::operator-(const Timestamp& right) const
345 {
346  return Timestamp(value_ - right.value_);
347 }
348 
350 {
351  value_ -= right.value_;
352 
353  return *this;
354 }
355 
356 inline Timestamp Timestamp::operator-(const double seconds) const
357 {
358  return Timestamp(value_ - seconds);
359 }
360 
361 inline Timestamp& Timestamp::operator-=(const double seconds)
362 {
363  value_ -= seconds;
364 
365  return *this;
366 }
367 
368 inline bool Timestamp::operator<(const Timestamp& right) const
369 {
370  return value_ < right.value_;
371 }
372 
373 inline bool Timestamp::operator<=(const Timestamp& right) const
374 {
375  return value_ <= right.value_;
376 }
377 
378 inline bool Timestamp::operator>(const Timestamp& right) const
379 {
380  return value_ > right.value_;
381 }
382 
383 inline bool Timestamp::operator>=(const Timestamp& right) const
384 {
385  return value_ >= right.value_;
386 }
387 
388 inline bool Timestamp::operator==(const Timestamp& right) const
389 {
390  return value_ == right.value_;
391 }
392 
393 inline bool Timestamp::operator!=(const Timestamp& right) const
394 {
395  return value_ != right.value_;
396 }
397 
398 inline Timestamp::operator double() const
399 {
400  return value_;
401 }
402 
403 inline size_t Timestamp::operator()(const Timestamp& timestamp) const
404 {
405  return std::hash<double>{}(double(timestamp));
406 }
407 
408 constexpr int64_t Timestamp::seconds2milliseconds(const double seconds)
409 {
410  // 1000 milliseconds = 1 second
411 
412  if (seconds >= 0.0)
413  {
414  return int64_t(seconds * 1.0e3 + 0.5);
415  }
416  else
417  {
418  return int64_t(seconds * 1.0e3 - 0.5);
419  }
420 }
421 
422 constexpr int64_t Timestamp::seconds2microseconds(const double seconds)
423 {
424  // 1000 milliseconds = 1 second
425  // 1000 microseconds = 1 milliseconds
426 
427  if (seconds >= 0.0)
428  {
429  return int64_t(seconds * 1.0e6 + 0.5);
430  }
431  else
432  {
433  return int64_t(seconds * 1.0e6 - 0.5);
434  }
435 }
436 
437 constexpr int64_t Timestamp::seconds2nanoseconds(const double seconds)
438 {
439  // 1000 milliseconds = 1 second
440  // 1000 microseconds = 1 milliseconds
441  // 1000 nanoseconds = 1 microseconds
442 
443  if (seconds >= 0.0)
444  {
445  return int64_t(seconds * 1.0e9 + 0.5);
446  }
447  else
448  {
449  return int64_t(seconds * 1.0e9 - 0.5);
450  }
451 }
452 
453 constexpr double Timestamp::milliseconds2seconds(const int64_t milliseconds)
454 {
455  return double(milliseconds) / 1.0e3;
456 }
457 
458 constexpr double Timestamp::microseconds2seconds(const int64_t microseconds)
459 {
460  return double(microseconds) / 1.0e6;
461 }
462 
463 constexpr double Timestamp::nanoseconds2seconds(const int64_t nanoseconds)
464 {
465  return double(nanoseconds) / 1.0e9;
466 }
467 
469 {
470  return -DBL_MAX;
471 }
472 
473 }
474 
475 #endif // META_OCEAN_BASE_TIMESTAMP_H
This class implements a timestamp.
Definition: Timestamp.h:36
bool operator>=(const Timestamp &right) const
Returns whether the this timestamp is greater or equal to the right one.
Definition: Timestamp.h:383
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition: Timestamp.h:408
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition: Timestamp.h:303
size_t operator()(const Timestamp &timestamp) const
Hash function.
Definition: Timestamp.h:403
static constexpr double nanoseconds2seconds(const int64_t nanoseconds)
Converts nanoseconds to seconds.
Definition: Timestamp.h:463
bool operator!=(const Timestamp &right) const
Returns whether two timestamps are not identical.
Definition: Timestamp.h:393
Timestamp(const bool toNow)
Creates a new timestamp.
static constexpr int64_t seconds2microseconds(const double seconds)
Converts seconds to microseconds.
Definition: Timestamp.h:422
static constexpr double microseconds2seconds(const int64_t microseconds)
Converts microseconds to seconds.
Definition: Timestamp.h:458
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:290
Timestamp & toInvalid()
Sets the timestamp to invalid.
Definition: Timestamp.h:276
bool operator<=(const Timestamp &right) const
Returns whether the this timestamp is lesser or equal to the right one.
Definition: Timestamp.h:373
bool operator==(const Timestamp &right) const
Returns whether two timestamps are identical.
Definition: Timestamp.h:388
double value_
Timestamp value.
Definition: Timestamp.h:267
int64_t nanoseconds() const
Returns this timestamp in nanoseconds.
Definition: Timestamp.h:283
bool operator>(const Timestamp &right) const
Returns whether the this timestamp is greater than the right one.
Definition: Timestamp.h:378
bool operator<(const Timestamp &right) const
Returns whether the this timestamp is lesser than the right one.
Definition: Timestamp.h:368
Timestamp operator-(const Timestamp &right) const
Subtracts two timestamps.
Definition: Timestamp.h:344
Timestamp operator+(const Timestamp &right) const
Adds two timestamps.
Definition: Timestamp.h:320
Timestamp & operator+=(const Timestamp &right)
Adds and assigns two timestamps.
Definition: Timestamp.h:325
static constexpr int64_t seconds2nanoseconds(const double seconds)
Converts seconds to nanoseconds.
Definition: Timestamp.h:437
bool isInvalid() const
Returns whether the timestamp holds an invalid time.
Definition: Timestamp.h:308
Timestamp()=default
Creates a new timestamp with invalid time.
Timestamp & toNow()
Sets the timestamp to the current time.
static constexpr double invalidTimestampValue()
Returns the of an invalid timestamp.
Definition: Timestamp.h:468
static constexpr double milliseconds2seconds(const int64_t milliseconds)
Converts milliseconds to seconds.
Definition: Timestamp.h:453
Timestamp & operator=(const double timestamp)
Assign a new value.
Definition: Timestamp.h:313
Timestamp & operator-=(const Timestamp &right)
Subtracts and assigns two timestamps.
Definition: Timestamp.h:349
std::vector< Timestamp > Timestamps
Definition of a vector holding Timestamp objects.
Definition: Timestamp.h:19
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:484
AutomaticDifferentiationT< T1, TNumeric1 > operator+(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition: AutomaticDifferentiation.h:418