Skip to content

Commit 6f2593d

Browse files
committed
gui, refactor: use std::chrono for formatDurationStr() helper
1 parent 8fe6f5a commit 6f2593d

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)