Request to add formatInterval to klocale

John Tapsell johnflux at gmail.com
Sun Dec 10 03:04:15 GMT 2006


Hi all,

  In KLocale there is a formatNumber, formatTime, formatByteSize  etc.

I would like to add a formatInterval(unsigned long msec), which
formats a time interval.

The would be fairly clean and straight forward as so:



Index: klocale.h
===================================================================
--- klocale.h   (revision 606175)
+++ klocale.h   (working copy)
@@ -424,6 +424,16 @@
   QString formatByteSize( double size ) const;

   /**
+   * Given a number of milliseconds, converts that to a string containing
+   * the localized equivalent
+   *
+   * e.g. given formatInterval(60000), returns "1.0 minutes"
+   *
+   * @param mSec Time interval in milliseconds
+   * @return converted interval as a string - e.g. "5.5 seconds" "23.0 minutes"
+   */
+  QString formatInterval( unsigned long mSec) const;
+  /**
    * Use this to determine whether nouns are declined in
    * locale's language. This property should remain
    * read-only (no setter function)
Index: klocale.cpp
===================================================================
--- klocale.cpp (revision 606175)
+++ klocale.cpp (working copy)
@@ -1103,6 +1103,25 @@
     return s;
 }

+QString KLocale::formatInterval( unsigned long mSec) const
+{
+   QString s;
+   if( size >= 24*3600000) {
+      s = i18n( "%1 days", formatNumber( size/(24*3600000), 3));
+   } else if(size >= 3600000)
+   {
+      s = i18n( "%1 hours", formatNumber( size/3600000.0, 2));
+   } else if(size >= 60000)
+   {
+      s = i18n( "%1 minutes", formatNumber( size/60000.0, 2));
+   } else if(size >= 1000)
+   {
+      s = i18n( "%1 seconds", formatNumber( size/1000.0, 2));
+   } else
+   {
+      s = i18n( "%1 milliseconds", formatNumber(size, 0));
+   }
+}
 QString KLocale::formatDate(const QDate &pDate, bool shortFormat) const
 {
   const QString rst = shortFormat?dateFormatShort():dateFormat();


JohnFlux




More information about the kde-core-devel mailing list