From 79b61ae7673eb6213493e2cb202f0d70c390932d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 May 2018 17:36:08 +0200 Subject: Use a steady clock source for progress reporting Clock changes while apt is running can result in strange reports confusing (and amusing) users. Sadly, to keep the ABI for now the code is a bit more ugly than it would need to be. --- apt-pkg/contrib/progress.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/progress.cc b/apt-pkg/contrib/progress.cc index 3bccc48d4..806bd47f8 100644 --- a/apt-pkg/contrib/progress.cc +++ b/apt-pkg/contrib/progress.cc @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -115,12 +116,18 @@ bool OpProgress::CheckChange(float Interval) return false; // Check time delta - struct timeval Now; - gettimeofday(&Now,0); - decltype(Interval) const Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0; - if (Diff < Interval) + auto const Now = std::chrono::steady_clock::now().time_since_epoch(); + auto const Now_sec = std::chrono::duration_cast(Now); + auto const Now_usec = std::chrono::duration_cast(Now - Now_sec); + struct timeval NowTime = { Now_sec.count(), Now_usec.count() }; + + std::chrono::duration Delta = + std::chrono::seconds(NowTime.tv_sec - LastTime.tv_sec) + + std::chrono::microseconds(NowTime.tv_sec - LastTime.tv_usec); + + if (Delta.count() < Interval) return false; - LastTime = Now; + LastTime = NowTime; return true; } /*}}}*/ -- cgit v1.2.3