MonotonicClock.h 765 Bytes
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#ifndef MONOTONICCLOCK_H
#define MONOTONICCLOCK_H

#include <chrono>

using Timestamp = double;
using SteadyTimePoint = std::chrono::time_point<std::chrono::steady_clock>;

class MonotonicClock {
public:
    MonotonicClock()
        : started(now())
    {
    }

    static SteadyTimePoint now() { return std::chrono::steady_clock::now(); }

    Timestamp elapsed() const
    {
        return 1e-6 * std::chrono::duration_cast<std::chrono::nanoseconds>(now() - started).count();
    }

    Timestamp elapsed(const SteadyTimePoint& p) const
    {
        return 1e-6 * std::chrono::duration_cast<std::chrono::nanoseconds>(p - started).count();
    }

private:
    const SteadyTimePoint started;
};

using ReferenceClock = MonotonicClock;

#endif // MONOTONICCLOCK_H