From 17c9421929815cd002393a600a485bd983f8b005 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Sun, 3 Nov 2024 02:27:52 +0100 Subject: [PATCH] Allow to start timer from a given time point --- src/utils/Timer.hpp | 6 +++++- tests/test_Timer.cpp | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/Timer.hpp b/src/utils/Timer.hpp index 786d7b789..76e135d8c 100644 --- a/src/utils/Timer.hpp +++ b/src/utils/Timer.hpp @@ -96,7 +96,7 @@ class Timer } Timer& operator=(const Timer&) = default; - Timer& operator=(Timer&&) = default; + Timer& operator=(Timer&&) = default; Timer(const Timer&) = default; Timer(Timer&&) = default; @@ -107,6 +107,10 @@ class Timer m_status{Status::running} {} + Timer(const std::chrono::time_point<std::chrono::high_resolution_clock>& start) + : m_start{start}, m_elapsed_sum{std::chrono::duration<double>::zero()}, m_status{Status::running} + {} + ~Timer() = default; }; diff --git a/tests/test_Timer.cpp b/tests/test_Timer.cpp index c615f9f3e..f0083a477 100644 --- a/tests/test_Timer.cpp +++ b/tests/test_Timer.cpp @@ -59,6 +59,11 @@ TEST_CASE("Timer", "[utils]") t2.reset(); REQUIRE(t2.status() == Timer::Status::paused); REQUIRE(t2.seconds() == 0); + + using namespace std::chrono_literals; + Timer t3{std::chrono::high_resolution_clock::now() - 24h}; + REQUIRE(t3.status() == Timer::Status::running); + REQUIRE(t3.seconds() > 24 * 3600); } SECTION("stop/start") -- GitLab