From ae0d2a56836186dce0b009ff957ae2d04672097a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Thu, 5 Nov 2020 12:49:15 +0100 Subject: [PATCH 1/6] KFormat: Add more relative date time cases Adds to formatRelativeDateTime: * A few moments ago * Less than an hour ago --- src/lib/util/kformatprivate.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index a5bcbfa8..7a470b52 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -519,11 +519,20 @@ 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 = now.daysTo(dateTime); if (daysTo > 2 || daysTo < -2) { return m_locale.toString(dateTime, format); } + if (dateTime.secsTo(now) < 10 * 60) { + return tr("A few moments ago"); + } + + if (dateTime.secsTo(now) < 60 * 60) { + return tr("Less than an hour ago"); + } + /*: 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)); -- GitLab From cba9e1f64339a27b764e17035db798d7f93a1159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Thu, 5 Nov 2020 13:08:53 +0100 Subject: [PATCH 2/6] Add Tests --- autotests/kformattest.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/autotests/kformattest.cpp b/autotests/kformattest.cpp index 96807efd..e7d38bb1 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(); + + // 4 minutes ago + testDateTime = now.addSecs(-300); + QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("A few moments ago")); + + // 20 minutes ago + testDateTime = now.addSecs(-1200); + QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Less than an hour ago")); + testDateTime = QDateTime(QDate::currentDate().addDays(8), QTime(3, 0, 0)); QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::LongFormat), QLocale::c().toString(testDateTime, QLocale::LongFormat)); -- GitLab From 70deeaa6606a1d34e0824dc1b4298870f5ba4bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Thu, 5 Nov 2020 13:21:37 +0100 Subject: [PATCH 3/6] Use an intermediary variable --- src/lib/util/kformatprivate.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index 7a470b52..9150683d 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -525,11 +525,12 @@ QString KFormatPrivate::formatRelativeDateTime(const QDateTime &dateTime, QLocal return m_locale.toString(dateTime, format); } - if (dateTime.secsTo(now) < 10 * 60) { + const auto secsToNow = dateTime.secsTo(now); + if (secsToNow< 10 * 60) { return tr("A few moments ago"); } - if (dateTime.secsTo(now) < 60 * 60) { + if (secsToNow < 60 * 60) { return tr("Less than an hour ago"); } -- GitLab From 5be2db9d89c478da65d47ad3a340f3ee666aad7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Thu, 5 Nov 2020 20:08:58 +0100 Subject: [PATCH 4/6] Rephrase 'A few moments ago' to 'Moments ago' --- src/lib/util/kformatprivate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index 9150683d..c96fc666 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -527,7 +527,7 @@ QString KFormatPrivate::formatRelativeDateTime(const QDateTime &dateTime, QLocal const auto secsToNow = dateTime.secsTo(now); if (secsToNow< 10 * 60) { - return tr("A few moments ago"); + return tr("Moments ago"); } if (secsToNow < 60 * 60) { -- GitLab From 03bb3cd2b70c8042fa3635d22856401d4543f80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Sat, 7 Nov 2020 09:18:48 +0100 Subject: [PATCH 5/6] Have "Just Now" and "X minutes ago" --- autotests/kformattest.cpp | 12 ++++++------ src/lib/util/kformatprivate.cpp | 15 ++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/autotests/kformattest.cpp b/autotests/kformattest.cpp index e7d38bb1..4b9342c0 100644 --- a/autotests/kformattest.cpp +++ b/autotests/kformattest.cpp @@ -374,13 +374,13 @@ void KFormatTest::formatRelativeDate() QDateTime now = QDateTime::currentDateTime(); - // 4 minutes ago - testDateTime = now.addSecs(-300); - QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("A few moments ago")); + // 1 minute ago + testDateTime = now.addSecs(-1); + QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Just Now")); - // 20 minutes ago - testDateTime = now.addSecs(-1200); - QCOMPARE(format.formatRelativeDateTime(testDateTime, QLocale::ShortFormat), QStringLiteral("Less than an hour ago")); + // 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), diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index c96fc666..73ee56f4 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -520,18 +520,19 @@ QString KFormatPrivate::formatRelativeDate(const QDate &date, QLocale::FormatTyp QString KFormatPrivate::formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const { const QDateTime now = QDateTime::currentDateTime(); - const qint64 daysTo = now.daysTo(dateTime); + 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< 10 * 60) { - return tr("Moments ago"); - } - - if (secsToNow < 60 * 60) { - return tr("Less than an hour ago"); + 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 -- GitLab From fe14aff13dc36f24d612edf7e890be54473ccebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Mon, 16 Nov 2020 19:30:19 +0000 Subject: [PATCH 6/6] Apply 1 suggestion(s) to 1 file(s) --- src/lib/util/kformatprivate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util/kformatprivate.cpp b/src/lib/util/kformatprivate.cpp index 73ee56f4..5fe49e88 100644 --- a/src/lib/util/kformatprivate.cpp +++ b/src/lib/util/kformatprivate.cpp @@ -529,7 +529,7 @@ QString KFormatPrivate::formatRelativeDateTime(const QDateTime &dateTime, QLocal if (secsToNow >= 0 && secsToNow < 60 * 60) { const int minutesToNow = secsToNow / 60; if (minutesToNow <= 1) { - return tr("Just Now"); + return tr("Just now"); } else { return tr("%1 minutes ago").arg(minutesToNow); } -- GitLab