diff options
author | Jon Atack <jon@atack.com> | 2022-02-14 21:02:50 +0100 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2022-02-15 00:48:02 +0100 |
commit | 6f2593dc23565abaa3d176595cba6e07883f512e (patch) | |
tree | 381b893eb8cbbeebdfceb886bcd6f58a4bfbc74e /src/qt/guiutil.cpp | |
parent | 8fe6f5a6fbcd8083d916cb630f35f8f5980d6825 (diff) | |
download | bitcoin-6f2593dc23565abaa3d176595cba6e07883f512e.tar.xz |
gui, refactor: use std::chrono for formatDurationStr() helper
Diffstat (limited to 'src/qt/guiutil.cpp')
-rw-r--r-- | src/qt/guiutil.cpp | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index dc73bcd911..9796565bc0 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -713,23 +713,18 @@ QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction QString formatDurationStr(std::chrono::seconds dur) { - const auto secs = count_seconds(dur); - QStringList strList; - int days = secs / 86400; - int hours = (secs % 86400) / 3600; - int mins = (secs % 3600) / 60; - int seconds = secs % 60; - - if (days) - strList.append(QObject::tr("%1 d").arg(days)); - if (hours) - strList.append(QObject::tr("%1 h").arg(hours)); - if (mins) - strList.append(QObject::tr("%1 m").arg(mins)); - if (seconds || (!days && !hours && !mins)) - strList.append(QObject::tr("%1 s").arg(seconds)); - - return strList.join(" "); + using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20 + const auto d{std::chrono::duration_cast<days>(dur)}; + const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)}; + const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)}; + const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)}; + QStringList str_list; + if (auto d2{d.count()}) str_list.append(QObject::tr("%1 d").arg(d2)); + if (auto h2{h.count()}) str_list.append(QObject::tr("%1 h").arg(h2)); + if (auto m2{m.count()}) str_list.append(QObject::tr("%1 m").arg(m2)); + const auto s2{s.count()}; + if (s2 || str_list.empty()) str_list.append(QObject::tr("%1 s").arg(s2)); + return str_list.join(" "); } QString formatServicesStr(quint64 mask) |