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 mTodayOneSecondBeforeMidnight;
75 QString mCustomFormat;
76};
77
78}
79
80DateFormatter::DateFormatter(FormatType ftype)
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
143QByteArray DateFormatterPrivate::zone(const QDateTime &t)
144{
145 const auto secs = t.offsetFromUtc();
146 const auto hours = std::abs(secs / 3600);
147 const auto mins = std::abs((secs - hours * 3600) / 60);
148
149 QByteArray ret(6, 0);
150 qsnprintf(ret.data(), ret.size(), "%c%.2d%.2d", (secs < 0) ? '-' : '+', hours, mins);
151 ret.chop(1);
152 return ret;
153}
154
155QString DateFormatterPrivate::fancy(const QDateTime &t)
156{
157 if (!t.isValid()) {
158 return i18nc("invalid time specified", "unknown");
159 }
160
161 if (!mTodayOneSecondBeforeMidnight.isValid()) {
162 // determine time of today 23:59:59
163 mTodayOneSecondBeforeMidnight = QDateTime(QDate::currentDate(), QTime(23, 59, 59));
164 }
165
166 QDateTime old(t);
167
168 if (mTodayOneSecondBeforeMidnight >= t) {
169 const auto diff = t.secsTo(mTodayOneSecondBeforeMidnight);
170 if (diff < 7 * 24 * 60 * 60) {
171 if (diff < 24 * 60 * 60) {
172 return i18n("Today %1", QLocale().toString(old.time(), QLocale::ShortFormat));
173 }
174 if (diff < 2 * 24 * 60 * 60) {
175 return i18n("Yesterday %1", QLocale().toString(old.time(), QLocale::ShortFormat));
176 }
177 for (int i = 3; i < 8; i++) {
178 if (diff < i * 24 * 60 * 60) {
179 return i18nc("1. weekday, 2. time",
180 "%1 %2",
181 QLocale().dayName(old.date().dayOfWeek(), QLocale::LongFormat),
182 QLocale().toString(old.time(), QLocale::ShortFormat));
183 }
184 }
185 }
186 }
187
189}
190
191QString DateFormatterPrivate::localized(const QDateTime &t, bool shortFormat, const QString &lang)
192{
193 QString ret;
194
195 if (!lang.isEmpty()) {
196 ret = QLocale(lang).toString(t, (shortFormat ? QLocale::ShortFormat : QLocale::LongFormat));
197 } else {
198 ret = QLocale().toString(t, (shortFormat ? QLocale::ShortFormat : QLocale::LongFormat));
199 }
200
201 return ret;
202}
203
204QString DateFormatterPrivate::cTime(const QDateTime &t)
205{
206 return t.toString(QStringLiteral("ddd MMM dd hh:mm:ss yyyy"));
207}
208
209QString DateFormatter::formatDate(FormatType ftype, const QDateTime &t, const QString &data, bool shortFormat)
210{
211 DateFormatter f(ftype);
212 if (ftype == Custom) {
213 f.setCustomFormat(data);
214 }
215 return f.dateString(t, data, shortFormat);
216}
217
218QString DateFormatter::formatCurrentDate(FormatType ftype, const QString &data, bool shortFormat)
219{
220 DateFormatter f(ftype);
221 if (ftype == Custom) {
222 f.setCustomFormat(data);
223 }
224 return f.dateString(QDateTime::currentDateTime(), data, shortFormat);
225}
A class for abstracting date formatting.
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()
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()
QDateTime currentDateTime()
bool isValid() const const
int offsetFromUtc() const const
qint64 secsTo(const QDateTime &other) 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 Fri Jul 26 2024 11:54:19 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.