Skip to content

Commit 127de22

Browse files
gui: add FormatPeerAge() utility helper
Co-authored-by: randymcmillan <[email protected]>
1 parent 310ba92 commit 127de22

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/qt/guiutil.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
void ForceActivation();
8080
#endif
8181

82+
using namespace std::chrono_literals;
83+
8284
namespace GUIUtil {
8385

8486
QString dateTimeStr(const QDateTime &date)
@@ -727,6 +729,16 @@ QString formatDurationStr(std::chrono::seconds dur)
727729
return str_list.join(" ");
728730
}
729731

732+
QString FormatPeerAge(std::chrono::seconds time_connected)
733+
{
734+
const auto time_now{GetTime<std::chrono::seconds>()};
735+
const auto age{time_now - time_connected};
736+
if (age >= 24h) return QObject::tr("%1 d").arg(age / 24h);
737+
if (age >= 1h) return QObject::tr("%1 h").arg(age / 1h);
738+
if (age >= 1min) return QObject::tr("%1 m").arg(age / 1min);
739+
return QObject::tr("%1 s").arg(age / 1s);
740+
}
741+
730742
QString formatServicesStr(quint64 mask)
731743
{
732744
QStringList strList;

src/qt/guiutil.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ namespace GUIUtil
223223
/** Convert seconds into a QString with days, hours, mins, secs */
224224
QString formatDurationStr(std::chrono::seconds dur);
225225

226+
/** Convert peer connection time to a QString denominated in the most relevant unit. */
227+
QString FormatPeerAge(std::chrono::seconds time_connected);
228+
226229
/** Format CNodeStats.nServices bitmask into a user-readable string */
227230
QString formatServicesStr(quint64 mask);
228231

0 commit comments

Comments
 (0)