1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QDATETIME_H
42#define QDATETIME_H
43
44#include <QtCore/qstring.h>
45#include <QtCore/qnamespace.h>
46#include <QtCore/qshareddata.h>
47
48#include <limits>
49
50#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
51Q_FORWARD_DECLARE_CF_TYPE(CFDate);
52Q_FORWARD_DECLARE_OBJC_CLASS(NSDate);
53#endif
54
55QT_BEGIN_NAMESPACE
56
57class QTimeZone;
58
59class Q_CORE_EXPORT QDate
60{
61public:
62 enum MonthNameType { // ### Qt 6: remove, along with methods using it
63 DateFormat = 0,
64 StandaloneFormat
65 };
66private:
67 explicit Q_DECL_CONSTEXPR QDate(qint64 julianDay) : jd(julianDay) {}
68public:
69 Q_DECL_CONSTEXPR QDate() : jd(nullJd()) {}
70 QDate(int y, int m, int d);
71
72 Q_DECL_CONSTEXPR bool isNull() const { return !isValid(); }
73 Q_DECL_CONSTEXPR bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
74
75 int year() const;
76 int month() const;
77 int day() const;
78 int dayOfWeek() const;
79 int dayOfYear() const;
80 int daysInMonth() const;
81 int daysInYear() const;
82 int weekNumber(int *yearNum = nullptr) const;
83
84#if QT_DEPRECATED_SINCE(5, 10) && QT_CONFIG(textdate)
85 QT_DEPRECATED_X("Use QLocale::monthName or QLocale::standaloneMonthName")
86 static QString shortMonthName(int month, MonthNameType type = DateFormat);
87 QT_DEPRECATED_X("Use QLocale::dayName or QLocale::standaloneDayName")
88 static QString shortDayName(int weekday, MonthNameType type = DateFormat);
89 QT_DEPRECATED_X("Use QLocale::monthName or QLocale::standaloneMonthName")
90 static QString longMonthName(int month, MonthNameType type = DateFormat);
91 QT_DEPRECATED_X("Use QLocale::dayName or QLocale::standaloneDayName")
92 static QString longDayName(int weekday, MonthNameType type = DateFormat);
93#endif // textdate && deprecated
94#if QT_CONFIG(datestring)
95 QString toString(Qt::DateFormat f = Qt::TextDate) const;
96#if QT_STRINGVIEW_LEVEL < 2
97 QString toString(const QString &format) const;
98#endif
99 QString toString(QStringView format) const;
100#endif
101#if QT_DEPRECATED_SINCE(5,0)
102QT_DEPRECATED inline bool setYMD(int y, int m, int d)
103{ if (uint(y) <= 99) y += 1900; return setDate(y, m, d); }
104#endif
105
106 bool setDate(int year, int month, int day);
107
108#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
109 void getDate(int *year, int *month, int *day); // ### Qt 6: remove
110#endif // < Qt 6
111 void getDate(int *year, int *month, int *day) const;
112
113 Q_REQUIRED_RESULT QDate addDays(qint64 days) const;
114 Q_REQUIRED_RESULT QDate addMonths(int months) const;
115 Q_REQUIRED_RESULT QDate addYears(int years) const;
116 qint64 daysTo(const QDate &) const;
117
118 Q_DECL_CONSTEXPR bool operator==(const QDate &other) const { return jd == other.jd; }
119 Q_DECL_CONSTEXPR bool operator!=(const QDate &other) const { return jd != other.jd; }
120 Q_DECL_CONSTEXPR bool operator< (const QDate &other) const { return jd < other.jd; }
121 Q_DECL_CONSTEXPR bool operator<=(const QDate &other) const { return jd <= other.jd; }
122 Q_DECL_CONSTEXPR bool operator> (const QDate &other) const { return jd > other.jd; }
123 Q_DECL_CONSTEXPR bool operator>=(const QDate &other) const { return jd >= other.jd; }
124
125 static QDate currentDate();
126#if QT_CONFIG(datestring)
127 static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
128 static QDate fromString(const QString &s, const QString &format);
129#endif
130 static bool isValid(int y, int m, int d);
131 static bool isLeapYear(int year);
132
133 static Q_DECL_CONSTEXPR inline QDate fromJulianDay(qint64 jd_)
134 { return jd_ >= minJd() && jd_ <= maxJd() ? QDate(jd_) : QDate() ; }
135 Q_DECL_CONSTEXPR inline qint64 toJulianDay() const { return jd; }
136
137private:
138 // using extra parentheses around min to avoid expanding it if it is a macro
139 static Q_DECL_CONSTEXPR inline qint64 nullJd() { return (std::numeric_limits<qint64>::min)(); }
140 static Q_DECL_CONSTEXPR inline qint64 minJd() { return Q_INT64_C(-784350574879); }
141 static Q_DECL_CONSTEXPR inline qint64 maxJd() { return Q_INT64_C( 784354017364); }
142
143 qint64 jd;
144
145 friend class QDateTime;
146 friend class QDateTimePrivate;
147#ifndef QT_NO_DATASTREAM
148 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
149 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
150#endif
151};
152Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
153
154class Q_CORE_EXPORT QTime
155{
156 explicit Q_DECL_CONSTEXPR QTime(int ms) : mds(ms)
157 {}
158public:
159 Q_DECL_CONSTEXPR QTime(): mds(NullTime)
160 {}
161 QTime(int h, int m, int s = 0, int ms = 0);
162
163 Q_DECL_CONSTEXPR bool isNull() const { return mds == NullTime; }
164 bool isValid() const;
165
166 int hour() const;
167 int minute() const;
168 int second() const;
169 int msec() const;
170#if QT_CONFIG(datestring)
171 QString toString(Qt::DateFormat f = Qt::TextDate) const;
172#if QT_STRINGVIEW_LEVEL < 2
173 QString toString(const QString &format) const;
174#endif
175 QString toString(QStringView format) const;
176#endif
177 bool setHMS(int h, int m, int s, int ms = 0);
178
179 Q_REQUIRED_RESULT QTime addSecs(int secs) const;
180 int secsTo(const QTime &) const;
181 Q_REQUIRED_RESULT QTime addMSecs(int ms) const;
182 int msecsTo(const QTime &) const;
183
184 Q_DECL_CONSTEXPR bool operator==(const QTime &other) const { return mds == other.mds; }
185 Q_DECL_CONSTEXPR bool operator!=(const QTime &other) const { return mds != other.mds; }
186 Q_DECL_CONSTEXPR bool operator< (const QTime &other) const { return mds < other.mds; }
187 Q_DECL_CONSTEXPR bool operator<=(const QTime &other) const { return mds <= other.mds; }
188 Q_DECL_CONSTEXPR bool operator> (const QTime &other) const { return mds > other.mds; }
189 Q_DECL_CONSTEXPR bool operator>=(const QTime &other) const { return mds >= other.mds; }
190
191 static Q_DECL_CONSTEXPR inline QTime fromMSecsSinceStartOfDay(int msecs) { return QTime(msecs); }
192 Q_DECL_CONSTEXPR inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; }
193
194 static QTime currentTime();
195#if QT_CONFIG(datestring)
196 static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
197 static QTime fromString(const QString &s, const QString &format);
198#endif
199 static bool isValid(int h, int m, int s, int ms = 0);
200
201 void start();
202 int restart();
203 int elapsed() const;
204private:
205 enum TimeFlag { NullTime = -1 };
206 Q_DECL_CONSTEXPR inline int ds() const { return mds == -1 ? 0 : mds; }
207 int mds;
208
209 friend class QDateTime;
210 friend class QDateTimePrivate;
211#ifndef QT_NO_DATASTREAM
212 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
213 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
214#endif
215};
216Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
217
218class QDateTimePrivate;
219
220class Q_CORE_EXPORT QDateTime
221{
222 // ### Qt 6: revisit the optimization
223 struct ShortData {
224#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
225 quintptr status : 8;
226#endif
227 // note: this is only 24 bits on 32-bit systems...
228 qintptr msecs : sizeof(void *) * 8 - 8;
229
230#if Q_BYTE_ORDER == Q_BIG_ENDIAN
231 quintptr status : 8;
232#endif
233 };
234
235 union Data {
236 enum {
237 // To be of any use, we need at least 60 years around 1970, which
238 // is 1,893,456,000,000 ms. That requires 41 bits to store, plus
239 // the sign bit. With the status byte, the minimum size is 50 bits.
240 CanBeSmall = sizeof(ShortData) * 8 > 50
241 };
242
243 Data();
244 Data(Qt::TimeSpec);
245 Data(const Data &other);
246 Data(Data &&other);
247 Data &operator=(const Data &other);
248 ~Data();
249
250 bool isShort() const;
251 void detach();
252
253 const QDateTimePrivate *operator->() const;
254 QDateTimePrivate *operator->();
255
256 QDateTimePrivate *d;
257 ShortData data;
258 };
259
260public:
261 QDateTime() Q_DECL_NOEXCEPT_EXPR(Data::CanBeSmall);
262 explicit QDateTime(const QDate &);
263 QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
264 // ### Qt 6: Merge with above with default offsetSeconds = 0
265 QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds);
266#if QT_CONFIG(timezone)
267 QDateTime(const QDate &date, const QTime &time, const QTimeZone &timeZone);
268#endif // timezone
269 QDateTime(const QDateTime &other) Q_DECL_NOTHROW;
270 QDateTime(QDateTime &&other) Q_DECL_NOTHROW;
271 ~QDateTime();
272
273#ifdef Q_COMPILER_RVALUE_REFS
274 QDateTime &operator=(QDateTime &&other) Q_DECL_NOTHROW { swap(other); return *this; }
275#endif
276 QDateTime &operator=(const QDateTime &other) Q_DECL_NOTHROW;
277
278 void swap(QDateTime &other) Q_DECL_NOTHROW { qSwap(d.d, other.d.d); }
279
280 bool isNull() const;
281 bool isValid() const;
282
283 QDate date() const;
284 QTime time() const;
285 Qt::TimeSpec timeSpec() const;
286 int offsetFromUtc() const;
287#if QT_CONFIG(timezone)
288 QTimeZone timeZone() const;
289#endif // timezone
290 QString timeZoneAbbreviation() const;
291 bool isDaylightTime() const;
292
293 qint64 toMSecsSinceEpoch() const;
294 qint64 toSecsSinceEpoch() const;
295
296 void setDate(const QDate &date);
297 void setTime(const QTime &time);
298 void setTimeSpec(Qt::TimeSpec spec);
299 void setOffsetFromUtc(int offsetSeconds);
300#if QT_CONFIG(timezone)
301 void setTimeZone(const QTimeZone &toZone);
302#endif // timezone
303 void setMSecsSinceEpoch(qint64 msecs);
304 void setSecsSinceEpoch(qint64 secs);
305
306#if QT_CONFIG(datestring)
307 QString toString(Qt::DateFormat f = Qt::TextDate) const;
308#if QT_STRINGVIEW_LEVEL < 2
309 QString toString(const QString &format) const;
310#endif
311 QString toString(QStringView format) const;
312#endif
313 Q_REQUIRED_RESULT QDateTime addDays(qint64 days) const;
314 Q_REQUIRED_RESULT QDateTime addMonths(int months) const;
315 Q_REQUIRED_RESULT QDateTime addYears(int years) const;
316 Q_REQUIRED_RESULT QDateTime addSecs(qint64 secs) const;
317 Q_REQUIRED_RESULT QDateTime addMSecs(qint64 msecs) const;
318
319 QDateTime toTimeSpec(Qt::TimeSpec spec) const;
320 inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
321 inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
322 QDateTime toOffsetFromUtc(int offsetSeconds) const;
323#if QT_CONFIG(timezone)
324 QDateTime toTimeZone(const QTimeZone &toZone) const;
325#endif // timezone
326
327 qint64 daysTo(const QDateTime &) const;
328 qint64 secsTo(const QDateTime &) const;
329 qint64 msecsTo(const QDateTime &) const;
330
331 bool operator==(const QDateTime &other) const;
332 inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
333 bool operator<(const QDateTime &other) const;
334 inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
335 inline bool operator>(const QDateTime &other) const { return other < *this; }
336 inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
337
338#if QT_DEPRECATED_SINCE(5, 2)
339 QT_DEPRECATED void setUtcOffset(int seconds);
340 QT_DEPRECATED int utcOffset() const;
341#endif // QT_DEPRECATED_SINCE
342
343 static QDateTime currentDateTime();
344 static QDateTime currentDateTimeUtc();
345#if QT_CONFIG(datestring)
346 static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
347 static QDateTime fromString(const QString &s, const QString &format);
348#endif
349
350#if QT_DEPRECATED_SINCE(5, 8)
351 uint toTime_t() const;
352 void setTime_t(uint secsSince1Jan1970UTC);
353 static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
354 static QDateTime fromTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec,
355 int offsetFromUtc = 0);
356 static QDateTime fromTime_t(uint secsSince1Jan1970UTC, const QTimeZone &timeZone);
357#endif
358
359 static QDateTime fromMSecsSinceEpoch(qint64 msecs);
360 // ### Qt 6: Merge with above with default spec = Qt::LocalTime
361 static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
362 static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spe = Qt::LocalTime, int offsetFromUtc = 0);
363
364#if QT_CONFIG(timezone)
365 static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
366 static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);
367#endif
368
369 static qint64 currentMSecsSinceEpoch() Q_DECL_NOTHROW;
370 static qint64 currentSecsSinceEpoch() Q_DECL_NOTHROW;
371
372#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
373 static QDateTime fromCFDate(CFDateRef date);
374 CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED;
375 static QDateTime fromNSDate(const NSDate *date);
376 NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED;
377#endif
378
379private:
380 friend class QDateTimePrivate;
381
382 Data d;
383
384#ifndef QT_NO_DATASTREAM
385 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
386 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
387#endif
388
389#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
390 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
391#endif
392};
393Q_DECLARE_SHARED(QDateTime)
394
395#ifndef QT_NO_DATASTREAM
396Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
397Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
398Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
399Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
400Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
401Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
402#endif // QT_NO_DATASTREAM
403
404#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
405Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
406Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
407Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
408#endif
409
410// QDateTime is not noexcept for now -- to be revised once
411// timezone and calendaring support is added
412Q_CORE_EXPORT uint qHash(const QDateTime &key, uint seed = 0);
413Q_CORE_EXPORT uint qHash(const QDate &key, uint seed = 0) Q_DECL_NOTHROW;
414Q_CORE_EXPORT uint qHash(const QTime &key, uint seed = 0) Q_DECL_NOTHROW;
415
416QT_END_NAMESPACE
417
418#endif // QDATETIME_H
419