MRPT  2.0.4
Clock.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <chrono>
12 
13 namespace mrpt
14 {
15 /** Clock that is compatible with MRPT TTimeStamp representation
16  * \ingroup mrpt_core_grp
17  */
18 class Clock
19 {
20  public:
21  using rep = int64_t;
22  // 100-nanoseconds
23  using period = std::ratio<1, 10000000>;
24  using duration = std::chrono::duration<rep, period>;
25  using time_point = std::chrono::time_point<Clock>;
26 
27  static constexpr bool is_steady = std::chrono::system_clock::is_steady;
28 
29  /** Returns the current time using the currently selected Clock source.
30  * \sa setActiveClock(), mrpt::Clock::Source */
31  static time_point now() noexcept;
32  /** Create a timestamp from its double representation. \sa toDouble */
33  static time_point fromDouble(const double t) noexcept;
34  /** Converts a timestamp to a UNIX time_t-like number, with fractional part
35  * \sa fromDouble */
36  static double toDouble(const time_point t) noexcept;
37 
38  /** Options for setting the source of all timestamps across MRPT:
39  * setActiveClock(), now()
40  */
41  enum Source
42  {
43  /** Realtime: POSIX `CLOCK_REALTIME`. The highest resolution available
44  * Typically this is better than 1 microsecond. Selected by default.
45  */
46  Realtime = 0,
47 
48  /** Monotonic: POSIX `CLOCK_MONOTONIC`. Only available in Linux systems.
49  */
51  };
52 
53  /** Changes the selected clock to get time from when calling now().
54  * Default: Realtime.
55  * Ignored in non-Linux systems.
56  */
57  static void setActiveClock(const Source s);
58 
59  /** Returns the currently selected clock. */
60  static Source getActiveClock();
61 
62  /** Monotonic clock *might* drift over time with respect to Realtime.
63  * The first time a time is requested to now() with Monotonic clock enabled,
64  * MRPT internally saves the current values of both, Realtime and Monotonic
65  * clocks, then use the difference in all subsequent calls to set Monotonic
66  * timepoints in the same epoch than Realtime.
67  *
68  * By explicitly calling this static method, a user is able to force a
69  * resynchrnization between the two clocks. Potentially useful for systems
70  * that run for very long periods of time without interruption.
71  *
72  * \return The mismatch between the former and the new estimations of the
73  * epochs differences between the two clocks, in units of nanoseconds.
74  *
75  * \note This method is ignored in non-Linux systems.
76  */
77  static int64_t resetMonotonicToRealTimeEpoch() noexcept;
78 
79  /** Returns the number of nanoseconds that are added to the output of the
80  * POSIX `CLOCK_MONOTONIC` to make timestamps match the epoch of POSIX
81  * `CLOCK_REALTIME`. */
82  static uint64_t getMonotonicToRealtimeOffset();
83 };
84 } // namespace mrpt
int64_t rep
Definition: Clock.h:21
static double toDouble(const time_point t) noexcept
Converts a timestamp to a UNIX time_t-like number, with fractional part.
Definition: Clock.cpp:106
Clock that is compatible with MRPT TTimeStamp representation.
Definition: Clock.h:18
std::chrono::duration< rep, period > duration
Definition: Clock.h:24
static time_point fromDouble(const double t) noexcept
Create a timestamp from its double representation.
Definition: Clock.cpp:99
std::chrono::time_point< Clock > time_point
Definition: Clock.h:25
Source
Options for setting the source of all timestamps across MRPT: setActiveClock(), now() ...
Definition: Clock.h:41
static Source getActiveClock()
Returns the currently selected clock.
Definition: Clock.cpp:122
static time_point now() noexcept
Returns the current time using the currently selected Clock source.
Definition: Clock.cpp:94
Realtime: POSIX CLOCK_REALTIME.
Definition: Clock.h:46
static void setActiveClock(const Source s)
Changes the selected clock to get time from when calling now().
Definition: Clock.cpp:114
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Monotonic: POSIX CLOCK_MONOTONIC.
Definition: Clock.h:50
static uint64_t getMonotonicToRealtimeOffset()
Returns the number of nanoseconds that are added to the output of the POSIX CLOCK_MONOTONIC to make t...
Definition: Clock.cpp:158
static int64_t resetMonotonicToRealTimeEpoch() noexcept
Monotonic clock might drift over time with respect to Realtime.
Definition: Clock.cpp:124
static constexpr bool is_steady
Definition: Clock.h:27
std::ratio< 1, 10000000 > period
Definition: Clock.h:23



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020