Add utility function for shorter time duration strings #3403

This commit is contained in:
Christian Kamm 2016-01-21 09:53:58 +01:00
Родитель 4915bbf8f3
Коммит 41f43feecf
5 изменённых файлов: 67 добавлений и 26 удалений

Просмотреть файл

@ -915,7 +915,7 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
overallSyncString = tr("%1 of %2, file %3 of %4\nTotal time left %5")
.arg(s1, s2)
.arg(currentFile).arg(totalFileCount)
.arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) );
.arg( Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) );
} else if (totalFileCount > 0) {
// Don't attempt to estimate the time left if there is no kb to transfer.
overallSyncString = tr("file %1 of %2") .arg(currentFile).arg(totalFileCount);

Просмотреть файл

@ -641,11 +641,11 @@ void ownCloudGui::slotUpdateProgress(const QString &folder, const ProgressInfo&
quint64 totalFileCount = qMax(progress.totalFiles(), currentFile);
_actionStatus->setText( tr("Syncing %1 of %2 (%3 left)")
.arg( currentFile ).arg( totalFileCount )
.arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
.arg( Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) ) );
} else {
QString totalSizeStr = Utility::octetsToString( progress.totalSize() );
_actionStatus->setText( tr("Syncing %1 (%2 left)")
.arg( totalSizeStr, Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
.arg( totalSizeStr, Utility::durationToDescriptiveString2(progress.totalProgress().estimatedEta) ) );
}

Просмотреть файл

@ -309,9 +309,19 @@ qint64 Utility::qDateTimeToTime_t(const QDateTime& t)
return t.toMSecsSinceEpoch() / 1000;
}
QString Utility::durationToDescriptiveString(quint64 msecs)
{
struct Period { const char *name; quint64 msec; };
namespace {
struct Period
{
const char *name;
quint64 msec;
QString description(quint64 value) const
{
return QCoreApplication::translate(
"Utiliy", name, 0, QCoreApplication::UnicodeUTF8,
value);
}
};
Q_DECL_CONSTEXPR Period periods[] = {
{ QT_TRANSLATE_NOOP("Utility", "%Ln year(s)") , 365*24*3600*1000LL },
{ QT_TRANSLATE_NOOP("Utility", "%Ln month(s)") , 30*24*3600*1000LL },
@ -321,17 +331,16 @@ QString Utility::durationToDescriptiveString(quint64 msecs)
{ QT_TRANSLATE_NOOP("Utility", "%Ln second(s)") , 1000LL },
{ 0, 0 }
};
} // anonymous namespace
QString Utility::durationToDescriptiveString2(quint64 msecs)
{
int p = 0;
while (periods[p].name && msecs < periods[p].msec) {
while (periods[p+1].name && msecs < periods[p].msec) {
p++;
}
if (!periods[p].name) {
return QCoreApplication::translate("Utility", "0 seconds");
}
auto firstPart = QCoreApplication::translate("Utility", periods[p].name, 0, QCoreApplication::UnicodeUTF8, int(msecs / periods[p].msec));
auto firstPart = periods[p].description(int(msecs / periods[p].msec));
if (!periods[p+1].name) {
return firstPart;
@ -344,7 +353,18 @@ QString Utility::durationToDescriptiveString(quint64 msecs)
}
return QCoreApplication::translate("Utility", "%1 %2").arg(firstPart,
QCoreApplication::translate("Utility", periods[p+1].name, 0, QCoreApplication::UnicodeUTF8, secondPartNum));
periods[p+1].description(secondPartNum));
}
QString Utility::durationToDescriptiveString1(quint64 msecs)
{
int p = 0;
while (periods[p+1].name && msecs < periods[p].msec) {
p++;
}
quint64 amount = qRound( double(msecs) / periods[p].msec );
return periods[p].description(amount);
}
QString Utility::fileNameForGuiUse(const QString& fName)

Просмотреть файл

@ -67,8 +67,15 @@ namespace Utility
* @brief Convert milliseconds duration to human readable string.
* @param quint64 msecs the milliseconds to convert to string.
* @return an HMS representation of the milliseconds value.
*
* durationToDescriptiveString1 describes the duration in a single
* unit, like "5 minutes" or "2 days".
*
* durationToDescriptiveString2 uses two units where possible, so
* "5 minutes 43 seconds" or "1 month 3 days".
*/
OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString(quint64 msecs);
OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString1(quint64 msecs);
OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString2(quint64 msecs);
/**
* @brief hasDarkSystray - determines whether the systray is dark or light.

Просмотреть файл

@ -83,21 +83,35 @@ private slots:
QDateTime current = QDateTime::currentDateTime();
QCOMPARE(durationToDescriptiveString(0), QString("0 seconds") );
QCOMPARE(durationToDescriptiveString(5), QString("0 seconds") );
QCOMPARE(durationToDescriptiveString(1000), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString(1005), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString(56123), QString("56 second(s)") );
QCOMPARE(durationToDescriptiveString(90*sec), QString("1 minute(s) 30 second(s)") );
QCOMPARE(durationToDescriptiveString(3*hour), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString(3*hour + 20*sec), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
QCOMPARE(durationToDescriptiveString(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
QCOMPARE(durationToDescriptiveString(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
QCOMPARE(durationToDescriptiveString2(0), QString("0 second(s)") );
QCOMPARE(durationToDescriptiveString2(5), QString("0 second(s)") );
QCOMPARE(durationToDescriptiveString2(1000), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString2(1005), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString2(56123), QString("56 second(s)") );
QCOMPARE(durationToDescriptiveString2(90*sec), QString("1 minute(s) 30 second(s)") );
QCOMPARE(durationToDescriptiveString2(3*hour), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString2(3*hour + 20*sec), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString2(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
QCOMPARE(durationToDescriptiveString2(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
QString("4 year(s) 5 month(s)") );
QCOMPARE(durationToDescriptiveString(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
QString("2 day(s) 23 hour(s)") );
QCOMPARE(durationToDescriptiveString1(0), QString("0 second(s)") );
QCOMPARE(durationToDescriptiveString1(5), QString("0 second(s)") );
QCOMPARE(durationToDescriptiveString1(1000), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString1(1005), QString("1 second(s)") );
QCOMPARE(durationToDescriptiveString1(56123), QString("56 second(s)") );
QCOMPARE(durationToDescriptiveString1(90*sec), QString("2 minute(s)") );
QCOMPARE(durationToDescriptiveString1(3*hour), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString1(3*hour + 20*sec), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString1(3*hour + 70*sec), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString1(3*hour + 100*sec), QString("3 hour(s)") );
QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
QString("4 year(s)") );
QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
QString("3 day(s)") );
}