mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 02:42:48 +00:00
kdecore: reimplement KLocale::formatDuration()
much like the old (and now gone) KLocale::prettyFormatDuration() except with miliseconds precision instead of days Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
aa9e5b7066
commit
e413cbc41f
2 changed files with 69 additions and 19 deletions
|
@ -30,6 +30,14 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <qmath.h>
|
#include <qmath.h>
|
||||||
|
|
||||||
|
enum KLocaleDuration
|
||||||
|
{
|
||||||
|
KDurationMilisecond = 0,
|
||||||
|
KDurationSecond = 1,
|
||||||
|
KDurationMinute = 2,
|
||||||
|
KDurationHour = 3
|
||||||
|
};
|
||||||
|
|
||||||
static bool isDefaultLocale(const KLocale *locale)
|
static bool isDefaultLocale(const KLocale *locale)
|
||||||
{
|
{
|
||||||
return (locale->language() == KLocale::defaultLanguage());
|
return (locale->language() == KLocale::defaultLanguage());
|
||||||
|
@ -44,6 +52,32 @@ static QStringList s_defaultCatalogs = QStringList()
|
||||||
|
|
||||||
static const QLatin1String s_localenamec = QLatin1String("C");
|
static const QLatin1String s_localenamec = QLatin1String("C");
|
||||||
|
|
||||||
|
static QString getDuration(const KLocaleDuration which, const int duration)
|
||||||
|
{
|
||||||
|
switch (which) {
|
||||||
|
case KLocaleDuration::KDurationHour: {
|
||||||
|
return i18ncp("@item:intext", "1 hour", "%1 hours", duration);
|
||||||
|
}
|
||||||
|
case KLocaleDuration::KDurationMinute: {
|
||||||
|
return i18ncp("@item:intext", "1 minute", "%1 minutes", duration);
|
||||||
|
}
|
||||||
|
case KLocaleDuration::KDurationSecond: {
|
||||||
|
return i18ncp("@item:intext", "1 second", "%1 seconds", duration);
|
||||||
|
}
|
||||||
|
case KLocaleDuration::KDurationMilisecond: {
|
||||||
|
return i18ncp("@item:intext", "1 milisecond", "%1 miliseconds", duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Q_ASSERT(false);
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static QString getMultiDuration(const KLocaleDuration which, const int duration,
|
||||||
|
const KLocaleDuration which2, const int duration2)
|
||||||
|
{
|
||||||
|
return i18nc("@item:intext", "%1 and %2", getDuration(which, duration), getDuration(which2, duration2));
|
||||||
|
}
|
||||||
|
|
||||||
class KLocalePrivate
|
class KLocalePrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -379,28 +413,44 @@ QString KLocale::formatByteSize(double size) const
|
||||||
|
|
||||||
QString KLocale::formatDuration(unsigned long mSec) const
|
QString KLocale::formatDuration(unsigned long mSec) const
|
||||||
{
|
{
|
||||||
if (mSec >= 24*3600000) {
|
QTime durationtime;
|
||||||
return i18nc(
|
durationtime = durationtime.addMSecs(mSec);
|
||||||
"@item:intext %1 is a real number, e.g. 1.23 days", "%1 days",
|
const int hours = durationtime.hour();
|
||||||
formatNumber(mSec / (24 * 3600000.0), 2)
|
const int minutes = durationtime.minute();
|
||||||
|
const int seconds = durationtime.second();
|
||||||
|
const int miliseconds = durationtime.msec();
|
||||||
|
|
||||||
|
if (hours && minutes) {
|
||||||
|
return getMultiDuration(
|
||||||
|
KLocaleDuration::KDurationHour, hours,
|
||||||
|
KLocaleDuration::KDurationMinute, minutes
|
||||||
);
|
);
|
||||||
} else if (mSec >= 3600000) {
|
} else if (hours) {
|
||||||
return i18nc(
|
return getDuration(
|
||||||
"@item:intext %1 is a real number, e.g. 1.23 hours", "%1 hours",
|
KLocaleDuration::KDurationHour, hours
|
||||||
formatNumber(mSec / 3600000.0, 2)
|
|
||||||
);
|
);
|
||||||
} else if (mSec >= 60000) {
|
} else if (minutes && seconds) {
|
||||||
return i18nc(
|
return getMultiDuration(
|
||||||
"@item:intext %1 is a real number, e.g. 1.23 minutes", "%1 minutes",
|
KLocaleDuration::KDurationMinute, minutes,
|
||||||
formatNumber(mSec / 60000.0, 2)
|
KLocaleDuration::KDurationSecond, seconds
|
||||||
);
|
);
|
||||||
} else if (mSec >= 1000) {
|
} else if (minutes) {
|
||||||
return i18nc(
|
return getDuration(
|
||||||
"@item:intext %1 is a real number, e.g. 1.23 seconds", "%1 seconds",
|
KLocaleDuration::KDurationMinute, minutes
|
||||||
formatNumber(mSec / 1000.0, 2)
|
);
|
||||||
|
} else if (seconds && miliseconds) {
|
||||||
|
return getMultiDuration(
|
||||||
|
KLocaleDuration::KDurationSecond, seconds,
|
||||||
|
KLocaleDuration::KDurationMilisecond, miliseconds
|
||||||
|
);
|
||||||
|
} else if (seconds) {
|
||||||
|
return getDuration(
|
||||||
|
KLocaleDuration::KDurationSecond, seconds
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return i18ncp("@item:intext", "%1 millisecond", "%1 milliseconds", mSec);
|
return getDuration(
|
||||||
|
KLocaleDuration::KDurationMilisecond, miliseconds
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString KLocale::formatDate(const QDate &date, QLocale::FormatType format) const
|
QString KLocale::formatDate(const QDate &date, QLocale::FormatType format) const
|
||||||
|
|
|
@ -965,12 +965,12 @@ void JobTest::calculateRemainingSeconds()
|
||||||
unsigned int seconds = KIO::calculateRemainingSeconds( 2 * 86400 - 60, 0, 1 );
|
unsigned int seconds = KIO::calculateRemainingSeconds( 2 * 86400 - 60, 0, 1 );
|
||||||
QCOMPARE( seconds, static_cast<unsigned int>( 2 * 86400 - 60 ) );
|
QCOMPARE( seconds, static_cast<unsigned int>( 2 * 86400 - 60 ) );
|
||||||
QString text = KIO::convertSeconds( seconds );
|
QString text = KIO::convertSeconds( seconds );
|
||||||
QCOMPARE( text, i18n( "1 day 23:59:00" ) );
|
QCOMPARE( text, i18n( "23 hours and 59 minutes" ) );
|
||||||
|
|
||||||
seconds = KIO::calculateRemainingSeconds( 520, 20, 10 );
|
seconds = KIO::calculateRemainingSeconds( 520, 20, 10 );
|
||||||
QCOMPARE( seconds, static_cast<unsigned int>( 50 ) );
|
QCOMPARE( seconds, static_cast<unsigned int>( 50 ) );
|
||||||
text = KIO::convertSeconds( seconds );
|
text = KIO::convertSeconds( seconds );
|
||||||
QCOMPARE( text, i18n( "00:00:50" ) );
|
QCOMPARE( text, i18n( "50 seconds" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void JobTest::getInvalidUrl()
|
void JobTest::getInvalidUrl()
|
||||||
|
|
Loading…
Add table
Reference in a new issue