From c81fd071e618740fac9351fdc0e47d654d65a156 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 25 Sep 2012 14:03:41 +0200 Subject: [PATCH] Detect local time changes. --- lib/base/application.cpp | 16 ++++++++++++++++ lib/base/timer.cpp | 13 +++++++++++++ lib/base/timer.h | 1 + 3 files changed, 30 insertions(+) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index fc36f4605..0838ea918 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -99,6 +99,8 @@ void Application::RunEventLoop(void) double nextProfile = 0; #endif /* _DEBUG */ + double lastLoop = Utility::GetTime(); + while (!m_ShuttingDown) { Object::ClearHeldObjects(); @@ -123,6 +125,20 @@ void Application::RunEventLoop(void) nextProfile = Utility::GetTime() + 15.0; } #endif /* _DEBUG */ + + double now = Utility::GetTime(); + + if (now < lastLoop) { + /* We moved backwards in time - cool. */ + double lostTime = lastLoop - now; + stringstream msgbuf; + msgbuf << "We moved backwards in time: " << lostTime + << " seconds"; + Logger::Write(LogInformation, "base", msgbuf.str()); + Timer::AdjustTimers(-lostTime); + } + + lastLoop = now; } } diff --git a/lib/base/timer.cpp b/lib/base/timer.cpp index ce2436908..faa8cf1eb 100644 --- a/lib/base/timer.cpp +++ b/lib/base/timer.cpp @@ -155,3 +155,16 @@ void Timer::Reschedule(double next) { m_Next = next; } + +/** + * Adjusts all timers by adding the specified amount of time to their + * next scheduled timestamp. + * + * @param adjustment The adjustment. + */ +void Timer::AdjustTimers(double adjustment) +{ + BOOST_FOREACH(Timer::Ptr timer, m_Timers) { + timer->m_Next += adjustment; + } +} diff --git a/lib/base/timer.h b/lib/base/timer.h index 2cde7360e..ab32c624c 100644 --- a/lib/base/timer.h +++ b/lib/base/timer.h @@ -43,6 +43,7 @@ public: double GetInterval(void) const; static double ProcessTimers(void); + static void AdjustTimers(double adjustment); void Start(void); void Stop(void);