summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/progress.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2018-05-26 17:36:08 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2018-05-28 17:59:35 +0200
commit79b61ae7673eb6213493e2cb202f0d70c390932d (patch)
tree7d7c4bd7b678060224bd7aeaa1cb1ce6d649369c /apt-pkg/contrib/progress.cc
parent484babb7d00f7550cbaa592b7cb0022d38217fad (diff)
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.
Diffstat (limited to 'apt-pkg/contrib/progress.cc')
-rw-r--r--apt-pkg/contrib/progress.cc17
1 files changed, 12 insertions, 5 deletions
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 <apt-pkg/progress.h>
#include <cmath>
+#include <chrono>
#include <cstring>
#include <iostream>
#include <string>
@@ -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<std::chrono::seconds>(Now);
+ auto const Now_usec = std::chrono::duration_cast<std::chrono::microseconds>(Now - Now_sec);
+ struct timeval NowTime = { Now_sec.count(), Now_usec.count() };
+
+ std::chrono::duration<decltype(Interval)> 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;
}
/*}}}*/