Skip to content

Commit f5116a7

Browse files
hebastoPastaPastaPasta
authored andcommitted
Merge bitcoin-core/gui#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
1 parent 3fa8158 commit f5116a7

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
@@ -1676,23 +1676,18 @@ QString ConnectionTypeToQString(ConnectionType conn_type)
16761676

16771677
QString formatDurationStr(std::chrono::seconds dur)
16781678
{
1679-
const auto secs = count_seconds(dur);
1680-
QStringList strList;
1681-
int days = secs / 86400;
1682-
int hours = (secs % 86400) / 3600;
1683-
int mins = (secs % 3600) / 60;
1684-
int seconds = secs % 60;
1685-
1686-
if (days)
1687-
strList.append(QObject::tr("%1 d").arg(days));
1688-
if (hours)
1689-
strList.append(QObject::tr("%1 h").arg(hours));
1690-
if (mins)
1691-
strList.append(QObject::tr("%1 m").arg(mins));
1692-
if (seconds || (!days && !hours && !mins))
1693-
strList.append(QObject::tr("%1 s").arg(seconds));
1694-
1695-
return strList.join(" ");
1679+
using days = std::chrono::duration<int, std::ratio<86400>>; // can remove this line after C++20
1680+
const auto d{std::chrono::duration_cast<days>(dur)};
1681+
const auto h{std::chrono::duration_cast<std::chrono::hours>(dur - d)};
1682+
const auto m{std::chrono::duration_cast<std::chrono::minutes>(dur - d - h)};
1683+
const auto s{std::chrono::duration_cast<std::chrono::seconds>(dur - d - h - m)};
1684+
QStringList str_list;
1685+
if (auto d2{d.count()}) str_list.append(QObject::tr("%1 d").arg(d2));
1686+
if (auto h2{h.count()}) str_list.append(QObject::tr("%1 h").arg(h2));
1687+
if (auto m2{m.count()}) str_list.append(QObject::tr("%1 m").arg(m2));
1688+
const auto s2{s.count()};
1689+
if (s2 || str_list.empty()) str_list.append(QObject::tr("%1 s").arg(s2));
1690+
return str_list.join(" ");
16961691
}
16971692

16981693
QString formatServicesStr(quint64 mask)

0 commit comments

Comments
 (0)