Skip to content

Commit 0904c3c

Browse files
committed
[Refactor] refactor function that forms human readable text out of a timeoffset
1 parent 53f8f22 commit 0904c3c

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -762,30 +762,7 @@ void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVer
762762
}
763763
else
764764
{
765-
// Represent time from last generated block in human readable text
766-
QString timeBehindText;
767-
const int HOUR_IN_SECONDS = 60*60;
768-
const int DAY_IN_SECONDS = 24*60*60;
769-
const int WEEK_IN_SECONDS = 7*24*60*60;
770-
const int YEAR_IN_SECONDS = 31556952; // Average length of year in Gregorian calendar
771-
if(secs < 2*DAY_IN_SECONDS)
772-
{
773-
timeBehindText = tr("%n hour(s)","",secs/HOUR_IN_SECONDS);
774-
}
775-
else if(secs < 2*WEEK_IN_SECONDS)
776-
{
777-
timeBehindText = tr("%n day(s)","",secs/DAY_IN_SECONDS);
778-
}
779-
else if(secs < YEAR_IN_SECONDS)
780-
{
781-
timeBehindText = tr("%n week(s)","",secs/WEEK_IN_SECONDS);
782-
}
783-
else
784-
{
785-
qint64 years = secs / YEAR_IN_SECONDS;
786-
qint64 remainder = secs % YEAR_IN_SECONDS;
787-
timeBehindText = tr("%1 and %2").arg(tr("%n year(s)", "", years)).arg(tr("%n week(s)","", remainder/WEEK_IN_SECONDS));
788-
}
765+
QString timeBehindText = GUIUtil::formateNiceTimeOffset(secs);
789766

790767
progressBarLabel->setVisible(true);
791768
progressBar->setFormat(tr("%1 behind").arg(timeBehindText));

src/qt/guiutil.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,4 +952,40 @@ QString formatTimeOffset(int64_t nTimeOffset)
952952
return QString(QObject::tr("%1 s")).arg(QString::number((int)nTimeOffset, 10));
953953
}
954954

955+
QString formateNiceTimeOffset(qint64 secs)
956+
{
957+
// Represent time from last generated block in human readable text
958+
QString timeBehindText;
959+
const int HOUR_IN_SECONDS = 60*60;
960+
const int DAY_IN_SECONDS = 24*60*60;
961+
const int WEEK_IN_SECONDS = 7*24*60*60;
962+
const int YEAR_IN_SECONDS = 31556952; // Average length of year in Gregorian calendar
963+
if(secs < 60)
964+
{
965+
timeBehindText = QObject::tr("%n seconds(s)","",secs);
966+
}
967+
else if(secs < 2*HOUR_IN_SECONDS)
968+
{
969+
timeBehindText = QObject::tr("%n minutes(s)","",secs/60);
970+
}
971+
else if(secs < 2*DAY_IN_SECONDS)
972+
{
973+
timeBehindText = QObject::tr("%n hour(s)","",secs/HOUR_IN_SECONDS);
974+
}
975+
else if(secs < 2*WEEK_IN_SECONDS)
976+
{
977+
timeBehindText = QObject::tr("%n day(s)","",secs/DAY_IN_SECONDS);
978+
}
979+
else if(secs < YEAR_IN_SECONDS)
980+
{
981+
timeBehindText = QObject::tr("%n week(s)","",secs/WEEK_IN_SECONDS);
982+
}
983+
else
984+
{
985+
qint64 years = secs / YEAR_IN_SECONDS;
986+
qint64 remainder = secs % YEAR_IN_SECONDS;
987+
timeBehindText = QObject::tr("%1 and %2").arg(QObject::tr("%n year(s)", "", years)).arg(QObject::tr("%n week(s)","", remainder/WEEK_IN_SECONDS));
988+
}
989+
return timeBehindText;
990+
}
955991
} // namespace GUIUtil

src/qt/guiutil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ namespace GUIUtil
200200
/* Format a CNodeCombinedStats.nTimeOffset into a user-readable string. */
201201
QString formatTimeOffset(int64_t nTimeOffset);
202202

203+
QString formateNiceTimeOffset(qint64 secs);
204+
203205
#if defined(Q_OS_MAC) && QT_VERSION >= 0x050000
204206
// workaround for Qt OSX Bug:
205207
// https://bugreports.qt-project.org/browse/QTBUG-15631

0 commit comments

Comments
 (0)