fix(net): Float interval for network speed

Before the time difference between two measurements was always an
integer number, so for intervals < 1, you would always get 0 and for any
other non-integer interval you would get skewed results.
This commit is contained in:
patrick96 2020-10-18 18:09:13 +02:00 committed by Patrick Ziegler
parent 3895ace12a
commit 4944a5179c
1 changed files with 4 additions and 3 deletions

View File

@ -225,9 +225,10 @@ namespace net {
* Format up- and download speed
*/
string network::format_speedrate(float bytes_diff, int minwidth) const {
const auto duration = m_status.current.time - m_status.previous.time;
float time_diff = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
float speedrate = bytes_diff / (time_diff ? time_diff : 1);
// Get time difference in seconds as a float
const std::chrono::duration<float> duration = m_status.current.time - m_status.previous.time;
float time_diff = duration.count();
float speedrate = bytes_diff / time_diff;
vector<string> suffixes{"GB", "MB"};
string suffix{"KB"};