#ifndef CORE_TIMER_H
#define CORE_TIMER_H

#include <chrono>
#include "core/time.h"

namespace dofs {

class Timer final {
public:
        Timer() {
                init();
        }

        void init() noexcept {
                _start = clock::now();
        }

        Time start() const noexcept {
                using namespace std;

                auto tp = _start.time_since_epoch();
                return Time::from_ns(
                               static_cast<Time::rep>(
                                       chrono::duration_cast<chrono::nanoseconds>(tp).count()
                               ));
        }

        Time now() const noexcept {
                using namespace std;

                auto tp = clock::now().time_since_epoch();
                return Time::from_ns(
                               static_cast<Time::rep>(
                                       chrono::duration_cast<chrono::nanoseconds>(tp).count()
                               ));
        }

        Time elapsed() const noexcept {
                using namespace std;

                auto dur = clock::now() - _start;
                return Time::from_ns(
                               static_cast<Time::rep>(
                                       chrono::duration_cast<chrono::nanoseconds>(dur).count()
                               ));
        }

private:
        using clock = std::chrono::steady_clock;
        clock::time_point _start;
};

} // namespace dofs

#endif // CORE_TIMER_H
