diff --git a/autotests/kformattest.cpp b/autotests/kformattest.cpp index 96807efd6d92bc0fbce9e1a7aed6b31e005f9496..4b9342c02672e50a33d4a6f08efe2d242b7e1af6 100644 --- a/autotests/kformattest.cpp +++ b/autotests/kformattest.cpp @@ -325,6 +325,7 @@ void KFormatTest::formatRelativeDate() KFormat format(QLocale::c()); QDate testDate = QDate::currentDate(); + QCOMPARE(format.formatRelativeDate(testDate, QLocale::LongFormat), QStringLiteral("Today")); QCOMPARE(format.formatRelativeDate(testDate, QLocale::ShortFormat), QStringLiteral("Today")); QCOMPARE(format.formatRelativeDate(testDate, QLocale::NarrowFormat), QStringLiteral("Today")); @@ -371,6 +372,16 @@ void KFormatTest::formatRelativeDate() QDateTime testDateTime = QDateTime(QDate::currentDate(), QTime(3, 0, 0)); QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Today, 03:00:00")); + QDateTime now = QDateTime::currentDateTime(); + + // 1 minute ago + testDateTime = now.addSecs(-1); + QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Just Now")); + + // 5 minutes ago + testDateTime = now.addSecs(-300); + QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("5 minutes ago")); + testDateTime = QDateTime(QDate::currentDate().addDays(8), QTime(3, 0, 0)); QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat), QLocale::c().toString(testDateTime, QLocale::LongFormat)); diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index a5bcbfa82fc25564521a180664a1fa953f678742..5fe49e8883382529257e4ab116c6e528693f11d0 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -519,11 +519,22 @@ QString KFormatPrivate::formatRelativeDate(const QDate &date, QLocale::FormatTyp QString KFormatPrivate::formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const { - const qint64 daysTo = QDate::currentDate().daysTo(dateTime.date()); + const QDateTime now = QDateTime::currentDateTime(); + const qint64 daysTo = dateTime.daysTo(now); if (daysTo > 2 || daysTo < -2) { return m_locale.toString(dateTime, format); } + const auto secsToNow = dateTime.secsTo(now); + if (secsToNow >= 0 && secsToNow < 60 * 60) { + const int minutesToNow = secsToNow / 60; + if (minutesToNow <= 1) { + return tr("Just now"); + } else { + return tr("%1 minutes ago").arg(minutesToNow); + } + } + /*: relative datetime with %1 result of formatReleativeDate() and %2 the formatted time If this does not fit the grammar of your language please contact the i18n team to solve the problem */ return tr("%1, %2").arg(formatRelativeDate(dateTime.date(), format), m_locale.toString(dateTime.time(), format));