Skip to content

Commit c8f2817

Browse files
committed
Merge bitcoin-core#549: refactor: use std::chrono for formatDurationStr() helper
6f2593d gui, refactor: use std::chrono for formatDurationStr() helper (Jon Atack) Pull request description: Updates `formatDurationStr()` to use the `chrono` standard lib. No change in behavior. ACKs for top commit: RandyMcMillan: tACK 6f2593d shaavan: ACK 6f2593d w0xlt: tACK 6f2593d on Ubuntu 21.10 Qt 5.15.2 promag: Code review ACK 6f2593d. Tree-SHA512: 61e9afdb1db779150df338e6af08727c34f69639add465c2f7003ff775d97dce3e78e78d325bc6dea5bc13f0fce9ef1c3506d13f1661a5e083e52bba8a32ba44
2 parents cc70f65 + 6f2593d commit c8f2817

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/qt/guiutil.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -713,23 +713,18 @@ QString ConnectionTypeToQString(ConnectionType conn_type, bool prepend_direction
713713

714714
QString formatDurationStr(std::chrono::seconds dur)
715715
{
716-
const auto secs = count_seconds(dur);
717-
QStringList strList;
718-
int days = secs / 86400;
719-
int hours = (secs % 86400) / 3600;
720-
int mins = (secs % 3600) / 60;
721-
int seconds = secs % 60;
722-
723-
if (days)
724-
strList.append(QObject::tr("%1 d").arg(days));
725-
if (hours)
726-
strList.append(QObject::tr("%1 h").arg(hours));
727-
if (mins)
728-
strList.append(QObject::tr("%1 m").arg(mins));
729-
if (seconds || (!days && !hours && !mins))
730-
strList.append(QObject::tr("%1 s").arg(seconds));
731-
732-
return strList.join(" ");
716+
using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20
717+
const auto d{std::chrono::duration_cast<days>(dur)};
718+
const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)};
719+
const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)};
720+
const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)};
721+
QStringList str_list;
722+
if (auto d2{d.count()}) str_list.append(QObject::tr("%1 d").arg(d2));
723+
if (auto h2{h.count()}) str_list.append(QObject::tr("%1 h").arg(h2));
724+
if (auto m2{m.count()}) str_list.append(QObject::tr("%1 m").arg(m2));
725+
const auto s2{s.count()};
726+
if (s2 || str_list.empty()) str_list.append(QObject::tr("%1 s").arg(s2));
727+
return str_list.join(" ");
733728
}
734729

735730
QString formatServicesStr(quint64 mask)

0 commit comments

Comments
 (0)