[Pkg-owncloud-commits] [owncloud-client] 211/498: Utility: improve the function to conver a duration to string

Sandro Knauß hefee-guest at moszumanska.debian.org
Tue Aug 11 14:48:51 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 7a324ff25b7b6decb1e33450fe5b745299646182
Author: Olivier Goffart <ogoffart at woboq.com>
Date:   Mon Jun 29 14:53:37 2015 +0200

    Utility: improve the function to conver a duration to string
    
     - Make it translatable
     - Make use of the %n so that it supports plurals (but this will only
       take effect if we have an english translation)
     - Only put two units.  eg:  "5 years 7 months" instead of
       "5 years 7 months 12 days 34 minutes 23 seconds"
       Even when it is "2 hours 23 minutes" the amount of second does not
       matter, especially since the estimation is likely to be boggus anyway
    
    Issues #2672 and #3097
---
 src/gui/folderstatusmodel.cpp |  4 +--
 src/gui/owncloudgui.cpp       |  5 ++-
 src/libsync/utility.cpp       | 72 +++++++++++++++++++++----------------------
 src/libsync/utility.h         |  6 ++--
 test/testutility.h            | 28 +++++++++++++++++
 5 files changed, 69 insertions(+), 46 deletions(-)

diff --git a/src/gui/folderstatusmodel.cpp b/src/gui/folderstatusmodel.cpp
index 92dcba5..8b654a1 100644
--- a/src/gui/folderstatusmodel.cpp
+++ b/src/gui/folderstatusmodel.cpp
@@ -645,7 +645,7 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
             //: Example text: "uploading foobar.png (1MB of 2MB) time left 2 minutes at a rate of 24Kb/s"
             fileProgressString = tr("%1 %2 (%3 of %4) %5 left at a rate of %6/s")
                 .arg(kindString, itemFileName, s1, s2,
-                    Utility::timeToDescriptiveString(progress.fileProgress(curItem).estimatedEta, 3, " ", true),
+                    Utility::durationToDescriptiveString(progress.fileProgress(curItem).estimatedEta),
                     Utility::octetsToString(estimatedBw) );
         } else {
             //: Example text: "uploading foobar.png (2MB of 2MB)"
@@ -672,7 +672,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::timeToDescriptiveString(progress.totalProgress().estimatedEta, 3, " ", true) );
+            .arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) );
     } else if (totalFileCount > 0) {
         // Don't attemt to estimate the time left if there is no kb to transfer.
         overallSyncString = tr("file %1 of %2") .arg(currentFile).arg(totalFileCount);
diff --git a/src/gui/owncloudgui.cpp b/src/gui/owncloudgui.cpp
index 39b6af7..bdf8dab 100644
--- a/src/gui/owncloudgui.cpp
+++ b/src/gui/owncloudgui.cpp
@@ -476,12 +476,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::timeToDescriptiveString(progress.totalProgress().estimatedEta, 2, " ",true) ) );
+            .arg( Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
     } else {
         QString totalSizeStr = Utility::octetsToString( progress.totalSize() );
         _actionStatus->setText( tr("Syncing %1 (%2 left)")
-            .arg( totalSizeStr )
-            .arg( Utility::timeToDescriptiveString(progress.totalProgress().estimatedEta, 2, " ",true) ) );
+            .arg( totalSizeStr, Utility::durationToDescriptiveString(progress.totalProgress().estimatedEta) ) );
     }
 
 
diff --git a/src/libsync/utility.cpp b/src/libsync/utility.cpp
index a9cb284..fa13c99 100644
--- a/src/libsync/utility.cpp
+++ b/src/libsync/utility.cpp
@@ -304,45 +304,43 @@ static QList<QPair<QString,quint32> > timeMapping = QList<QPair<QString,quint32>
                                                     QPair<QString,quint32>("%1h",3600) <<
                                                     QPair<QString,quint32>("%1m",60) <<
                                                     QPair<QString,quint32>("%1s",1);
-                                                    
-                                                    
-QString Utility::timeToDescriptiveString(quint64 msecs, quint8 precision, QString separator, bool specific) 
-{     
-    return timeToDescriptiveString( timeMapping , msecs, precision, separator, specific);
-}
-
-
-QString Utility::timeToDescriptiveString(QList<QPair<QString,quint32> > &timeMapping, quint64 msecs, quint8 precision, QString separator, bool specific)
-{       
-    quint64 secs = msecs / 1000;
-    QString retStr = QString(timeMapping.last().first).arg(0); // default value in case theres no actual time in msecs.
-    QList<QPair<QString,quint32> > values;
-    bool timeStartFound = false;
-   
-    for(QList<QPair<QString,quint32> >::Iterator itr = timeMapping.begin(); itr != timeMapping.end() && precision > 0; itr++) {
-        quint64 result = secs / itr->second;        
-        if(!timeStartFound) {
-            if(result == 0 ) {
-                continue;
-            }
-            retStr = "";
-            timeStartFound= true;        
-        }        
-        secs -= result * itr->second;
-        values.append(QPair<QString,quint32>(itr->first,result));
-        precision--;
+
+QString Utility::durationToDescriptiveString(quint64 msecs)
+{
+    struct Period { const char *name; quint64 msec; };
+    Q_DECL_CONSTEXPR const Period periods[] = {
+        { QT_TRANSLATE_NOOP("Utility", "%Ln year(s)") , 365*24*3600*1000L },
+        { QT_TRANSLATE_NOOP("Utility", "%Ln month(s)") , 30*24*3600*1000L },
+        { QT_TRANSLATE_NOOP("Utility", "%Ln day(s)") , 24*3600*1000L },
+        { QT_TRANSLATE_NOOP("Utility", "%Ln hour(s)") , 3600*1000L },
+        { QT_TRANSLATE_NOOP("Utility", "%Ln minute(s)") , 60*1000L },
+        { QT_TRANSLATE_NOOP("Utility", "%Ln second(s)") , 1000L },
+        { 0, 0 }
+    };
+
+    int p = 0;
+    while (periods[p].name && msecs < periods[p].msec) {
+        p++;
     }
-    
-    for(QList<QPair<QString,quint32> >::Iterator itr = values.begin(); itr < values.end(); itr++) {
-        retStr = retStr.append((specific || itr == values.end()-1) ? itr->first : "%1").arg(itr->second, (itr == values.begin() ? 1 :2 ), 10, QChar('0'));        
-        if(itr < values.end()-1) {
-            retStr.append(separator);
-        }
-        
-            
+
+    if (!periods[p].name) {
+        return QCoreApplication::translate("Utility", "0 seconds");
     }
-    
-    return retStr;
+
+    auto firstPart = QCoreApplication::translate("Utility", periods[p].name, 0, int(msecs / periods[p].msec));
+
+    if (!periods[p+1].name) {
+        return firstPart;
+    }
+
+    quint64 secondPartNum = qRound( double(msecs % periods[p].msec) / periods[p+1].msec);
+
+    if (secondPartNum == 0) {
+        return firstPart;
+    }
+
+    return QCoreApplication::translate("Utility", "%1 %2").arg(firstPart,
+            QCoreApplication::translate("Utility", periods[p+1].name, 0, secondPartNum));
 }
 
 bool Utility::hasDarkSystray()
diff --git a/src/libsync/utility.h b/src/libsync/utility.h
index 272dc89..91c05b4 100644
--- a/src/libsync/utility.h
+++ b/src/libsync/utility.h
@@ -61,13 +61,11 @@ namespace Utility
     OWNCLOUDSYNC_EXPORT qint64 qDateTimeToTime_t(const QDateTime &t);
 
     /**
-     * @brief Convert milliseconds to HMS string.
+     * @brief Convert milliseconds duration to human readable string.
      * @param quint64 msecs the milliseconds to convert to string.
-     * @param uint precision the amount of sub dviving scale to include in the result.
      * @return an HMS representation of the milliseconds value.
      */
-    OWNCLOUDSYNC_EXPORT QString timeToDescriptiveString(QList<QPair<QString,quint32> > &timeMapping, quint64 msecs, quint8 precision, QString separator, bool specific);
-    OWNCLOUDSYNC_EXPORT QString timeToDescriptiveString(quint64 msecs, quint8 precision, QString separator, bool specific);
+    OWNCLOUDSYNC_EXPORT QString durationToDescriptiveString(quint64 msecs);
 
     /**
      * @brief hasDarkSystray - determines whether the systray is dark or light.
diff --git a/test/testutility.h b/test/testutility.h
index 95a4d46..8d29dad 100644
--- a/test/testutility.h
+++ b/test/testutility.h
@@ -75,6 +75,34 @@ private slots:
         QVERIFY(toCSyncScheme("https://example.com/owncloud/") ==
                               "ownclouds://example.com/owncloud/");
     }
+
+    void testDurationToDescriptiveString()
+    {
+        QLocale::setDefault(QLocale("C"));
+        //NOTE: in order for the plural to work we would need to load the english translation
+
+        quint64 sec = 1000;
+        quint64 hour = 3600 * sec;
+
+        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))),
+                 QString("4 year(s) 5 month(s)") );
+        QCOMPARE(durationToDescriptiveString(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
+                 QString("2 day(s) 23 hour(s)") );
+
+
+    }
 };
 
 #endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list