Messagelib

dateformatter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2001 the KMime authors.
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4/**
5 @file
6 This file is part of the API for handling @ref MIME data and
7 defines the DateFormatter class.
8
9 @brief
10 Defines the DateFormatter class.
11
12 @authors the KMime authors (see AUTHORS file)
13*/
14
15#include "dateformatter.h"
16
17#include <KLocalizedString>
18
19using namespace MessageCore;
20
21namespace MessageCore
22{
23
24class DateFormatterPrivate
25{
26public:
27 DateFormatterPrivate()
28 {
29 }
30
31 /**
32 Returns a QString containing the specified time @p t formatted
33 using the #Fancy #FormatType.
34
35 @param t is the time to use for formatting.
36 */
37 QString fancy(const QDateTime &t);
38
39 /**
40 Returns a QString containing the specified time @p t formatted
41 using the #Localized #FormatType.
42
43 @param t is the time to use for formatting.
44 @param shortFormat if true, create the short version of the date string.
45 @param lang is a QString containing the language to use.
46 */
47 static QString localized(const QDateTime &t, bool shortFormat = true, const QString &lang = QString());
48
49 /**
50 Returns a QString containing the specified time @p t formatted
51 with the ctime() function.
52
53 @param t is the time to use for formatting.
54 */
55 static QString cTime(const QDateTime &t);
56
57 /**
58 Returns a QString containing the specified time @p t formatted
59 with a previously specified custom format.
60
61 @param t time used for formatting
62 */
63 QString custom(const QDateTime &t) const;
64
65 /**
66 Returns a QString that identifies the timezone (eg."-0500")
67 of the specified time @p t.
68
69 @param t time to compute timezone from.
70 */
71 static QByteArray zone(const QDateTime &t);
72
74 QDateTime mTodayOneMillisecondBeforeMidnight;
75 QString mCustomFormat;
76};
77
78}
79
81 : d(new DateFormatterPrivate)
82{
83 d->mFormat = ftype;
84}
85
87
89{
90 return d->mFormat;
91}
92
94{
95 d->mFormat = ftype;
96}
97
98QString DateFormatter::dateString(const QDateTime &dt, const QString &lang, bool shortFormat) const
99{
100 switch (d->mFormat) {
101 case Fancy:
102 return d->fancy(dt);
103 case Localized:
104 return d->localized(dt, shortFormat, lang);
105 case CTime:
106 return d->cTime(dt);
107 case Custom:
108 return d->custom(dt);
109 }
110 return {};
111}
112
113QString DateFormatterPrivate::custom(const QDateTime &t) const
114{
115 if (mCustomFormat.isEmpty()) {
116 return {};
117 }
118
119 int z = mCustomFormat.indexOf(QLatin1Char('Z'));
120 QDateTime dt;
121 QString ret = mCustomFormat;
122
123 if (z != -1) {
124 ret.replace(z, 1, QLatin1StringView(zone(t)));
125 }
126
127 ret = t.toString(ret);
128
129 return ret;
130}
131
133{
134 d->mCustomFormat = format;
135 d->mFormat = Custom;
136}
137
139{
140 return d->mCustomFormat;
141}
142
144{
145 d->mTodayOneMillisecondBeforeMidnight = QDateTime();
146}
147
148QByteArray DateFormatterPrivate::zone(const QDateTime &t)
149{
150 const auto secs = t.offsetFromUtc();
151 const auto hours = std::abs(secs / 3600);
152 const auto mins = std::abs((secs - hours * 3600) / 60);
153
154 QByteArray ret(6, 0);
155 qsnprintf(ret.data(), ret.size(), "%c%.2d%.2d", (secs < 0) ? '-' : '+', hours, mins);
156 ret.chop(1);
157 return ret;
158}
159
160QString DateFormatterPrivate::fancy(const QDateTime &t)
161{
162 if (!t.isValid()) {
163 return i18nc("invalid time specified", "unknown");
164 }
165
166 if (!mTodayOneMillisecondBeforeMidnight.isValid()) {
167 // determine time of today 23:59:59.999
168 mTodayOneMillisecondBeforeMidnight = QDateTime(QDate::currentDate(), QTime(23, 59, 59, 999));
169 }
170
171 if (mTodayOneMillisecondBeforeMidnight >= t) {
172 const auto diffDays = t.daysTo(mTodayOneMillisecondBeforeMidnight);
173 if (diffDays < 7) {
174 if (diffDays == 0) {
175 return i18n("Today %1", QLocale().toString(t.time(), QLocale::ShortFormat));
176 }
177 if (diffDays == 1) {
178 return i18n("Yesterday %1", QLocale().toString(t.time(), QLocale::ShortFormat));
179 }
180 for (int i = 2; i < 7; i++) {
181 if (diffDays == i) {
182 return i18nc("1. weekday, 2. time",
183 "%1 %2",
184 QLocale().dayName(t.date().dayOfWeek(), QLocale::LongFormat),
186 }
187 }
188 }
189 }
190
192}
193
194QString DateFormatterPrivate::localized(const QDateTime &t, bool shortFormat, const QString &lang)
195{
196 QString ret;
197
198 if (!lang.isEmpty()) {
199 ret = QLocale(lang).toString(t, (shortFormat ? QLocale::ShortFormat : QLocale::LongFormat));
200 } else {
201 ret = QLocale().toString(t, (shortFormat ? QLocale::ShortFormat : QLocale::LongFormat));
202 }
203
204 return ret;
205}
206
207QString DateFormatterPrivate::cTime(const QDateTime &t)
208{
209 return t.toString(QStringLiteral("ddd MMM dd hh:mm:ss yyyy"));
210}
211
212QString DateFormatter::formatDate(FormatType ftype, const QDateTime &t, const QString &data, bool shortFormat)
213{
214 DateFormatter f(ftype);
215 if (ftype == Custom) {
216 f.setCustomFormat(data);
217 }
218 return f.dateString(t, data, shortFormat);
219}
220
221QString DateFormatter::formatCurrentDate(FormatType ftype, const QString &data, bool shortFormat)
222{
223 DateFormatter f(ftype);
224 if (ftype == Custom) {
225 f.setCustomFormat(data);
226 }
227 return f.dateString(QDateTime::currentDateTime(), data, shortFormat);
228}
A class for abstracting date formatting.
void invalidateReferenceDate() const
Invalidates the cached reference date used by the Fancy formatter to determine the meaning of "Today"...
FormatType
The different types of date formats.
@ Localized
localized "2002-03-31 02:08"
@ Fancy
fancy "Today 02:08:35"
@ Custom
custom "whatever you like"
@ CTime
ctime "Sun Mar 31 02:08:35 2002"
static QString formatCurrentDate(DateFormatter::FormatType ftype, const QString &data=QString(), bool shortFormat=true)
Convenience function, same as formatDate() but returns the current time formatted.
void setCustomFormat(const QString &format)
Sets the custom format for date to string conversions to format.
void setFormat(FormatType ftype)
Sets the date format to ftype.
QString customFormat() const
Returns the custom format string.
FormatType format() const
Returns the FormatType currently set.
static QString formatDate(DateFormatter::FormatType ftype, const QDateTime &t, const QString &data=QString(), bool shortFormat=true)
Convenience function dateString.
QString dateString(const QDateTime &dtime, const QString &lang=QString(), bool shortFormat=true) const
Constructs a formatted date string from QDateTime dtime.
DateFormatter(FormatType ftype=DateFormatter::Fancy)
Constructs a date formatter with a default FormatType.
~DateFormatter()
Destroys the date formatter.
This file is part of the API for handling MIME data and defines the DateFormatter class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QDate currentDate()
int dayOfWeek() const const
QDateTime currentDateTime()
QDate date() const const
qint64 daysTo(const QDateTime &other) const const
bool isValid() const const
int offsetFromUtc() const const
QTime time() const const
QString toString(QStringView format, QCalendar cal) const const
QString toString(QDate date, FormatType format) const const
void chop(qsizetype n)
QChar * data()
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.