class mrpt::Clock

C++11-clock that is compatible with MRPT TTimeStamp representation.

State-aware methods in this class are thread-safe.

#include <mrpt/core/Clock.h>

class Clock
    // typedefs

    typedef int64_t rep;
    typedef std::ratio<1, 10000000> period;
    typedef std::chrono::duration<rep, period> duration;
    typedef std::chrono::time_point<Clock> time_point;

    // enums

    enum Source;


    static constexpr bool is_steady = std::chrono::system_clock::is_steady;


    static time_point now();
    static time_point fromDouble(const double t);
    static double toDouble(const time_point t);
    static void setActiveClock(const Source s);
    static Source getActiveClock();
    static int64_t resetMonotonicToRealTimeEpoch();
    static uint64_t getMonotonicToRealtimeOffset();
    static void setSimulatedTime(const time_point& t);


static time_point now()

Returns the current time using the currently selected Clock source.

See also:

setActiveClock(), mrpt::Clock::Source

static time_point fromDouble(const double t)

Create a timestamp from its double representation.

See also:


static double toDouble(const time_point t)

Converts a timestamp to a UNIX time_t-like number, with fractional part.

See also:


static void setActiveClock(const Source s)

Changes the selected clock to get time from when calling now().

Default: Realtime.

Monotonic is only available on Linux systems. RealTime and Simulated are available on any platform.

It is strongly recommended to call setSimulatedTime() before setting the clock source to Simulated to ensure that any subsequent call to now(), perhaps in a parallel thread, does not return an undefined time_point value.

static Source getActiveClock()

Returns the currently selected clock.

static int64_t resetMonotonicToRealTimeEpoch()

Monotonic clock might drift over time with respect to Realtime.

The first time a time is requested to now() with Monotonic clock enabled, MRPT internally saves the current values of both, Realtime and Monotonic clocks, then use the difference in all subsequent calls to set Monotonic timepoints in the same epoch than Realtime.

By explicitly calling this static method, a user is able to force a resynchrnization between the two clocks. Potentially useful for systems that run for very long periods of time without interruption.

This method is ignored in non-Linux systems.


The mismatch between the former and the new estimations of the epochs differences between the two clocks, in units of nanoseconds.

static uint64_t getMonotonicToRealtimeOffset()

Returns the number of nanoseconds that are added to the output of the POSIX CLOCK_MONOTONIC to make timestamps match the epoch of POSIX CLOCK_REALTIME.

static void setSimulatedTime(const time_point& t)

When setActiveClock() is set to Simulated, sets the simulated time that will be returned in subsequent calls to now().

[New in MRPT 2.1.1]