From b5a758dea257a87e8ed0ba11dd3df65a70a2a863 Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Sun, 2 Aug 2015 21:54:55 +0200 Subject: [PATCH] Smart date formatting --- src/datautils.cpp | 22 ++++++++++++++++++++++ src/datautils.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/datautils.cpp b/src/datautils.cpp index 36811b0..dbaf3af 100644 --- a/src/datautils.cpp +++ b/src/datautils.cpp @@ -79,3 +79,25 @@ uint DataUtils::parseIsoPeriod(const QString &isoPeriod) { uint period = ((days * 24 + hours) * 60 + minutes) * 60 + seconds; return period; } + +QString DataUtils::formatDateTime(const QDateTime &dt) { + const qint64 seconds = dt.secsTo(QDateTime::currentDateTime()); + QString s; + int f = 60; + if (seconds < f) { + s = qApp->tr("Just now"); + } else if (seconds < (f *= 60)) { + s = qApp->tr("%n minute(s) ago", "", seconds / 60); + } else if (seconds < (f *= 24)) { + s = qApp->tr("%n hour(s) ago", "", seconds / (60*60)); + } else if (seconds < (f *= 7)) { + s = qApp->tr("%n day(s) ago", "", seconds / (60*60*24)); + } else if (seconds < (f = 60*60*24*30)) { + s = qApp->tr("%n weeks(s) ago", "", seconds / (60*60*24*7)); + } else if (seconds < (f = 60*60*24*365)) { + s = qApp->tr("%n month(s) ago", "", seconds / (60*60*24*30)); + } else { + s = dt.toString(Qt::DefaultLocaleShortDate); + } + return s; +} diff --git a/src/datautils.h b/src/datautils.h index e5a8de0..bce1ca8 100644 --- a/src/datautils.h +++ b/src/datautils.h @@ -10,6 +10,7 @@ public: static QString regioneCode(const QLocale &locale); static QString systemRegioneCode(); static uint parseIsoPeriod(const QString &isoPeriod); + static QString formatDateTime(const QDateTime &dt); private: DataUtils() { } -- 2.39.5