Messagelib

dateformatter.h
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 @glossary @anchor RFC2822 @anchor rfc2822 @b RFC @b 2822:
13 RFC that defines the <a href="https://tools.ietf.org/html/rfc2822">
14 Internet Message Format</a>.
15
16 @glossary @anchor ISO8601 @anchor iso8601 @b ISO @b 8601:
17 International Standards Organization (ISO) standard that defines the
18 <a href="https://en.wikipedia.org/wiki/ISO_8601">
19 international standard for date and time representations</a>.
20
21 @glossary @anchor ctime @b ctime:
22 a Unix library call which returns the local time as a human readable
23 ASCII string of the form "Sun Mar 31 02:08:35 2002".
24*/
25
26#pragma once
27
28#include "messagecore_export.h"
29#include <QDateTime>
30#include <QString>
31#include <memory>
32
33namespace MessageCore
34{
35class DateFormatterPrivate;
36
37/**
38 @brief
39 A class for abstracting date formatting.
40
41 This class deals with different kinds of date display formats.
42 The formats supported include:
43 - @b fancy "Today 02:08:35"
44 - @b ctime as with the @ref ctime function, eg. "Sun Mar 31 02:08:35 2002"
45 - @b localized according to the control center setting, eg. "2002-03-31 02:08"
46 - @b custom "whatever you like"
47*/
48class MESSAGECORE_EXPORT DateFormatter
49{
50public:
51 /**
52 The different types of date formats.
53 */
55 CTime, /**< ctime "Sun Mar 31 02:08:35 2002" */
56 Localized, /**< localized "2002-03-31 02:08" */
57 Fancy, /**< fancy "Today 02:08:35" */
58 Custom /**< custom "whatever you like" */
59 };
60
61 /**
62 Constructs a date formatter with a default #FormatType.
63
64 @param ftype is the default #FormatType to use.
65 */
66 explicit DateFormatter(FormatType ftype = DateFormatter::Fancy);
67
68 /**
69 Destroys the date formatter.
70 */
72
73 /**
74 Returns the #FormatType currently set.
75
76 @see setFormat().
77 */
78 [[nodiscard]] FormatType format() const;
79
80 /**
81 Sets the date format to @p ftype.
82
83 @param ftype is the #FormatType.
84
85 @see format().
86 */
87 void setFormat(FormatType ftype);
88
89 /**
90 Constructs a formatted date string from QDateTime @p dtime.
91
92 @param dtime is the QDateTime to use for formatting.
93 @param lang is the language, only used if #FormatType is #Localized.
94 @param shortFormat if true, create the short version of the date string,
95 only used if #FormatType is #Localized.
96
97 @return a QString containing the formatted date.
98 */
99 [[nodiscard]] QString dateString(const QDateTime &dtime, const QString &lang = QString(), bool shortFormat = true) const;
100
101 /**
102 Sets the custom format for date to string conversions to @p format.
103 This method accepts the same arguments as QDateTime::toString(), but
104 also supports the "Z" expression which is substituted with the
105 @ref RFC2822 (Section 3.3) style numeric timezone (-0500).
106
107 @param format is a QString containing the custom format.
108
109 @see QDateTime::toString(), customFormat().
110 */
111 void setCustomFormat(const QString &format);
112
113 /**
114 Returns the custom format string.
115
116 @see setCustomFormat().
117 */
118 [[nodiscard]] QString customFormat() const;
119
120 // static methods
121 /**
122 Convenience function dateString
123
124 @param ftype is the #FormatType to use.
125 @param t is the time to use for formatting.
126 @param data is either the format when #FormatType is Custom,
127 or language when #FormatType is #Localized.
128 @param shortFormat if true, create the short version of the date string,
129 only used if #FormatType is #Localized.
130
131 @return a QString containing the formatted date.
132 */
133 [[nodiscard]] static QString formatDate(DateFormatter::FormatType ftype, const QDateTime &t, const QString &data = QString(), bool shortFormat = true);
134
135 /**
136 Convenience function, same as formatDate() but returns the current time
137 formatted.
138
139 @param ftype is the #FormatType to use.
140 @param data is either the format when #FormatType is Custom,
141 or language when #FormatType is #Localized.
142 @param shortFormat if true, create the short version of the date string,
143 only used if #FormatType is #Localized.
144
145 @return a QString containing the formatted date.
146 */
147 [[nodiscard]] static QString formatCurrentDate(DateFormatter::FormatType ftype, const QString &data = QString(), bool shortFormat = true);
148
149private:
150 //@cond PRIVATE
151 Q_DISABLE_COPY(DateFormatter)
152 std::unique_ptr<DateFormatterPrivate> const d;
153 //@endcond
154};
155
156} // namespace KMime
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"
@ CTime
ctime "Sun Mar 31 02:08:35 2002"
~DateFormatter()
Destroys the date formatter.
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.