From 5c3ba8460a9784acb784337a2c11b5175fd7c011 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Wed, 1 May 2013 02:42:50 +0200 Subject: rewrite date/time formatting --- board.cpp | 81 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'board.cpp') diff --git a/board.cpp b/board.cpp index 8f7c0cb..329626f 100644 --- a/board.cpp +++ b/board.cpp @@ -226,41 +226,58 @@ QString Board::parseSmilies(QString text) const return text; } -QString Board::renderHumanDate(const QDateTime &dateTime, bool monthOnly) -{ - QDate date = dateTime.toLocalTime().date(); - QDate today = QDate::currentDate(); - if (date == today) { - return tr("Today"); - } else if (date.daysTo(today) == 1) { - return tr("Yesterday"); - } else if (date.daysTo(today) < 5) { - return QDate::longDayName(date.dayOfWeek(), QDate::StandaloneFormat); - } else if (monthOnly && date.daysTo(today) < date.daysInYear()) { - return QDate::longMonthName(date.month(), QDate::StandaloneFormat); - } else if (monthOnly) { - return QString::number(date.year()); - } else { - return date.toString(Qt::DefaultLocaleShortDate); - } -} - -QString Board::renderHumanDateTime(const QDateTime &dateTime) +QString Board::formatDateTime(const QDateTime &dateTime, DateTimePrecisionOptions precision) const { const QDateTime now = QDateTime::currentDateTime(); - QDateTime localDateTime = dateTime.toLocalTime(); - const int secs = localDateTime.secsTo(now); - if (secs < 1) { - return tr("Just now"); - } else if (secs < 60) { - return tr("%n second(s) ago", 0, secs); - } else if (secs < 3600) { - int mins = (secs + 10) / 60; // + 10 to round a bit - return tr("%n minute(s) ago", 0, mins); - } else if (localDateTime.daysTo(now) < 1) { - return localDateTime.time().toString(Qt::DefaultLocaleShortDate); + const QDateTime ldt = dateTime.toLocalTime(); + const int secs = ldt.secsTo(now); + const int days = ldt.daysTo(now); + + if (precision & RelativeTime) { + if (secs < 1) { + return tr("Just now"); + } else if (secs < 60) { + return tr("%n second(s) ago", 0, secs); + } else if (secs < 3600) { + int mins = (secs + 10) / 60; // + 10 to round a bit + return tr("%n minute(s) ago", 0, mins); + } + // Fall through + } + if (precision & (TodayYesterday | RelativeDate)) { + if (precision & ShowTime) { + const QString time = ldt.time().toString(Qt::DefaultLocaleShortDate); + if (days < 1) { + return tr("Today %1").arg(time); + } else if (days <= 1) { + return tr("Yesterday %1").arg(time); + } + } else { + if (days < 1) { + return tr("Today"); + } else if (days <= 1) { + return tr("Yesterday"); + } + } + } + if (precision & RelativeDate) { + if (days < 7) { + const int dayOfWeek = ldt.date().dayOfWeek(); + QString name = QDate::longDayName(dayOfWeek, QDate::StandaloneFormat); + name[0] = name[0].toUpper(); + return name; + } else if (days < 30) { + return tr("%n week(s) ago", 0, days / 7); + } else if (days < 365) { + return tr("%n month(s) ago", 0, days / 30); + } else { + return tr("%n year(s) ago", 0, days / 365); + } + } + if (precision & ShowTime) { + return ldt.toString(Qt::DefaultLocaleShortDate); } else { - return localDateTime.toString(Qt::DefaultLocaleShortDate); + return ldt.date().toString(Qt::DefaultLocaleShortDate); } } -- cgit v1.2.3