ayatana-commits team mailing list archive
-
ayatana-commits team
-
Mailing list archive
-
Message #00153
[Branch ~agateau/plasma-indicatordisplay/trunk] Rev 74: Format time as specified in the Messaging Menu spec
------------------------------------------------------------
revno: 74
committer: Aurelien Gateau <aurelien.gateau@xxxxxxxxxxxxx>
branch nick: plasma-indicatordisplay
timestamp: Fri 2009-09-11 15:30:07 +0200
message:
Format time as specified in the Messaging Menu spec
modified:
src/timeutils.cpp
src/timeutils.h
tests/timeutilstest.cpp
tests/timeutilstest.h
--
lp:plasma-indicatordisplay
https://code.launchpad.net/~agateau/plasma-indicatordisplay/trunk
Your team ayatana-commits is subscribed to branch lp:plasma-indicatordisplay.
To unsubscribe from this branch go to https://code.launchpad.net/~agateau/plasma-indicatordisplay/trunk/+edit-subscription.
=== modified file 'src/timeutils.cpp'
--- src/timeutils.cpp 2009-09-11 10:18:06 +0000
+++ src/timeutils.cpp 2009-09-11 13:30:07 +0000
@@ -12,6 +12,8 @@
// Qt
#include <QDateTime>
+#include <QLocale>
+#include <QRegExp>
// KDE
#include <KDebug>
@@ -21,9 +23,36 @@
namespace TimeUtils
{
-QString formatDateTime(const QDateTime& dateTime, const QDateTime& now)
-{
- return KGlobal::locale()->formatTime(dateTime.time());
+QString formatDateTime(const QDateTime& dateTime, const QDateTime& _now)
+{
+ KLocale* locale = KGlobal::locale();
+ QDateTime now = _now.isValid() ? _now : QDateTime::currentDateTime();
+
+ int delta = dateTime.secsTo(now);
+ if (delta < 3600) {
+ return formatShortDuration(delta / 60);
+ }
+ if (dateTime.date() == now.date()) {
+ return locale->formatTime(dateTime.time());
+ }
+ if (dateTime.daysTo(now) < 7) {
+ return QDate::shortDayName(dateTime.date().dayOfWeek()) + " " + locale->formatTime(dateTime.time());
+ }
+
+ return dateTime.toString(TimeUtils::shortDateFormat()) + " " + locale->formatTime(dateTime.time());
+}
+
+QString formatShortDuration(int minutes)
+{
+ return i18ncp("%1 is a number of minutes", "1m", "%1m", minutes);
+}
+
+QString shortDateFormat(const QLocale* defaultLocale)
+{
+ static const QRegExp rx("[^dMy]?yy(yy)?[^dMy]?");
+ QLocale locale = defaultLocale ? *defaultLocale : QLocale::system();
+ return locale.dateFormat(QLocale::ShortFormat).replace(rx, QString());
+
}
} // namespace
=== modified file 'src/timeutils.h'
--- src/timeutils.h 2009-09-11 10:18:06 +0000
+++ src/timeutils.h 2009-09-11 13:30:07 +0000
@@ -14,6 +14,8 @@
// Qt
#include <QDateTime>
+class QLocale;
+
namespace TimeUtils
{
@@ -24,6 +26,20 @@
*/
QString formatDateTime(const QDateTime& dateTime, const QDateTime& now = QDateTime());
+/**
+ * Return a duration of a few minutes formated according to the messaging menu
+ * spec
+ * @param minutes the duration
+ */
+QString formatShortDuration(int minutes);
+
+/**
+ * Return the format string used to represent a date in a short way,
+ * without including the year
+ * @param locale the locale to use, useful for unittesting
+ */
+QString shortDateFormat(const QLocale* locale = 0);
+
} // namespace
#endif /* TIMEUTILS_H */
=== modified file 'tests/timeutilstest.cpp'
--- tests/timeutilstest.cpp 2009-09-11 10:18:06 +0000
+++ tests/timeutilstest.cpp 2009-09-11 13:30:07 +0000
@@ -12,23 +12,67 @@
#include "timeutilstest.h"
// Qt
+#include <QLocale>
#include <QtTest>
// KDE
#include <qtest_kde.h>
#include <KDebug>
+#include <KGlobal>
+#include <KLocale>
// Local
#include <timeutils.h>
QTEST_KDEMAIN(TimeUtilsTest, GUI)
+static const QDate NOW_DATE(2006, 4, 1);
+static const QTime NOW_TIME(9, 0);
+static const QDateTime NOW_DATETIME(NOW_DATE, NOW_TIME);
+
void TimeUtilsTest::testFormatDateTime()
{
+ QFETCH(QDateTime, dateTime);
+ QFETCH(QString, expected);
+
+ QString result = TimeUtils::formatDateTime(dateTime, NOW_DATETIME);
+ QCOMPARE(result, expected);
}
void TimeUtilsTest::testFormatDateTime_data()
{
+ QTest::addColumn<QDateTime>("dateTime");
+ QTest::addColumn<QString>("expected");
+
+ KLocale* locale = KGlobal::locale();
+ QDateTime dateTime;
+ QTime time;
+
+ QTest::newRow("4 minutes ago")
+ << QDateTime(NOW_DATE, NOW_TIME.addSecs(- 4 * 60))
+ << TimeUtils::formatShortDuration(4);
+
+ time = NOW_TIME.addSecs(- 2 * 3600 - 4 * 60);
+ QTest::newRow("2 hours 4 minutes ago")
+ << QDateTime(NOW_DATE, time)
+ << locale->formatTime(time);
+
+ dateTime = QDateTime(NOW_DATE.addDays(-1), NOW_TIME);
+ QTest::newRow("Yesterday")
+ << dateTime
+ << QDate::shortDayName(dateTime.date().dayOfWeek()) + " " + locale->formatTime(NOW_TIME);
+
+ dateTime = QDateTime(NOW_DATE.addDays(-7), NOW_TIME);
+ QTest::newRow("7 days ago")
+ << dateTime
+ << dateTime.toString(TimeUtils::shortDateFormat()) + " " + locale->formatTime(NOW_TIME);
+}
+
+void TimeUtilsTest::testShortDateFormat()
+{
+ QLocale cLocale = QLocale::c();
+ QString format = TimeUtils::shortDateFormat(&cLocale);
+ QCOMPARE(format, QString("d MMM"));
}
#include "timeutilstest.moc"
=== modified file 'tests/timeutilstest.h'
--- tests/timeutilstest.h 2009-09-11 10:18:06 +0000
+++ tests/timeutilstest.h 2009-09-11 13:30:07 +0000
@@ -23,6 +23,8 @@
private Q_SLOTS:
void testFormatDateTime();
void testFormatDateTime_data();
+
+ void testShortDateFormat();
};
#endif /* TIMEUTILSTEST_H */