1/****************************************************************************
2**
3** Copyright (C) 2020 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 QCalendar;
58#if QT_CONFIG(timezone)
59class QTimeZone;
60#endif
61class QDateTime;
62
63class Q_CORE_EXPORT QDate // ### Qt 6: change to be used by value, not const &
64{
65public:
66 enum MonthNameType { // ### Qt 6: remove, along with methods using it
67 DateFormat = 0,
68 StandaloneFormat
69 };
70private:
71 explicit Q_DECL_CONSTEXPR QDate(qint64 julianDay) : jd(julianDay) {}
72public:
73 Q_DECL_CONSTEXPR QDate() : jd(nullJd()) {}
74 QDate(int y, int m, int d);
75 QDate(int y, int m, int d, QCalendar cal);
76
77 Q_DECL_CONSTEXPR bool isNull() const { return !isValid(); }
78 Q_DECL_CONSTEXPR bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
79
80 int year() const;
81 int month() const;
82 int day() const;
83 int dayOfWeek() const;
84 int dayOfYear() const;
85 int daysInMonth() const;
86 int daysInYear() const;
87 int weekNumber(int *yearNum = nullptr) const;
88
89 int year(QCalendar cal) const;
90 int month(QCalendar cal) const;
91 int day(QCalendar cal) const;
92 int dayOfWeek(QCalendar cal) const;
93 int dayOfYear(QCalendar cal) const;
94 int daysInMonth(QCalendar cal) const;
95 int daysInYear(QCalendar cal) const;
96
97 QDateTime startOfDay(Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const;
98 QDateTime endOfDay(Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const;
99#if QT_CONFIG(timezone)
100 QDateTime startOfDay(const QTimeZone &zone) const;
101 QDateTime endOfDay(const QTimeZone &zone) const;
102#endif
103
104#if QT_DEPRECATED_SINCE(5, 10) && QT_CONFIG(textdate)
105 QT_DEPRECATED_X("Use QLocale::monthName or QLocale::standaloneMonthName")
106 static QString shortMonthName(int month, MonthNameType type = DateFormat);
107 QT_DEPRECATED_X("Use QLocale::dayName or QLocale::standaloneDayName")
108 static QString shortDayName(int weekday, MonthNameType type = DateFormat);
109 QT_DEPRECATED_X("Use QLocale::monthName or QLocale::standaloneMonthName")
110 static QString longMonthName(int month, MonthNameType type = DateFormat);
111 QT_DEPRECATED_X("Use QLocale::dayName or QLocale::standaloneDayName")
112 static QString longDayName(int weekday, MonthNameType type = DateFormat);
113#endif // textdate && deprecated
114#if QT_CONFIG(datestring)
115 QString toString(Qt::DateFormat format = Qt::TextDate) const;
116#if QT_DEPRECATED_SINCE(5, 15)
117 // Only the deprecated locale-dependent formats use the calendar.
118 QT_DEPRECATED_X("Use QLocale or omit the calendar")
119 QString toString(Qt::DateFormat format, QCalendar cal) const;
120#endif
121
122#if QT_STRINGVIEW_LEVEL < 2
123 QString toString(const QString &format) const;
124 QString toString(const QString &format, QCalendar cal) const;
125#endif
126
127 QString toString(QStringView format) const;
128 QString toString(QStringView format, QCalendar cal) const;
129#endif
130#if QT_DEPRECATED_SINCE(5,0)
131 QT_DEPRECATED_X("Use setDate() instead") inline bool setYMD(int y, int m, int d)
132 { if (uint(y) <= 99) y += 1900; return setDate(y, m, d); }
133#endif
134
135 bool setDate(int year, int month, int day);
136 bool setDate(int year, int month, int day, QCalendar cal);
137
138#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
139 void getDate(int *year, int *month, int *day); // ### Qt 6: remove
140#endif // < Qt 6
141 void getDate(int *year, int *month, int *day) const;
142
143 Q_REQUIRED_RESULT QDate addDays(qint64 days) const;
144 Q_REQUIRED_RESULT QDate addMonths(int months) const;
145 Q_REQUIRED_RESULT QDate addYears(int years) const;
146 Q_REQUIRED_RESULT QDate addMonths(int months, QCalendar cal) const;
147 Q_REQUIRED_RESULT QDate addYears(int years, QCalendar cal) const;
148 qint64 daysTo(const QDate &) const; // ### Qt 6: QDate
149
150 Q_DECL_CONSTEXPR bool operator==(const QDate &other) const { return jd == other.jd; }
151 Q_DECL_CONSTEXPR bool operator!=(const QDate &other) const { return jd != other.jd; }
152 Q_DECL_CONSTEXPR bool operator< (const QDate &other) const { return jd < other.jd; }
153 Q_DECL_CONSTEXPR bool operator<=(const QDate &other) const { return jd <= other.jd; }
154 Q_DECL_CONSTEXPR bool operator> (const QDate &other) const { return jd > other.jd; }
155 Q_DECL_CONSTEXPR bool operator>=(const QDate &other) const { return jd >= other.jd; }
156
157 static QDate currentDate();
158#if QT_CONFIG(datestring)
159 static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
160 static QDate fromString(const QString &s, const QString &format);
161 static QDate fromString(const QString &s, const QString &format, QCalendar cal);
162#endif
163 static bool isValid(int y, int m, int d);
164 static bool isLeapYear(int year);
165
166 static Q_DECL_CONSTEXPR inline QDate fromJulianDay(qint64 jd_)
167 { return jd_ >= minJd() && jd_ <= maxJd() ? QDate(jd_) : QDate() ; }
168 Q_DECL_CONSTEXPR inline qint64 toJulianDay() const { return jd; }
169
170private:
171 // using extra parentheses around min to avoid expanding it if it is a macro
172 static Q_DECL_CONSTEXPR inline qint64 nullJd() { return (std::numeric_limits<qint64>::min)(); }
173 static Q_DECL_CONSTEXPR inline qint64 minJd() { return Q_INT64_C(-784350574879); }
174 static Q_DECL_CONSTEXPR inline qint64 maxJd() { return Q_INT64_C( 784354017364); }
175
176 qint64 jd;
177
178 friend class QDateTime;
179 friend class QDateTimePrivate;
180#ifndef QT_NO_DATASTREAM
181 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
182 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
183#endif
184};
185Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
186
187class Q_CORE_EXPORT QTime // ### Qt 6: change to be used by value, not const &
188{
189 explicit Q_DECL_CONSTEXPR QTime(int ms) : mds(ms)
190 {}
191public:
192 Q_DECL_CONSTEXPR QTime(): mds(NullTime)
193 {}
194 QTime(int h, int m, int s = 0, int ms = 0);
195
196 Q_DECL_CONSTEXPR bool isNull() const { return mds == NullTime; }
197 bool isValid() const;
198
199 int hour() const;
200 int minute() const;
201 int second() const;
202 int msec() const;
203#if QT_CONFIG(datestring)
204 QString toString(Qt::DateFormat f = Qt::TextDate) const;
205#if QT_STRINGVIEW_LEVEL < 2
206 QString toString(const QString &format) const;
207#endif
208 QString toString(QStringView format) const;
209#endif
210 bool setHMS(int h, int m, int s, int ms = 0);
211
212 Q_REQUIRED_RESULT QTime addSecs(int secs) const;
213 int secsTo(const QTime &) const; // ### Qt 6: plain QTime
214 Q_REQUIRED_RESULT QTime addMSecs(int ms) const;
215 int msecsTo(const QTime &) const; // ### Qt 6: plain QTime
216
217 Q_DECL_CONSTEXPR bool operator==(const QTime &other) const { return mds == other.mds; }
218 Q_DECL_CONSTEXPR bool operator!=(const QTime &other) const { return mds != other.mds; }
219 Q_DECL_CONSTEXPR bool operator< (const QTime &other) const { return mds < other.mds; }
220 Q_DECL_CONSTEXPR bool operator<=(const QTime &other) const { return mds <= other.mds; }
221 Q_DECL_CONSTEXPR bool operator> (const QTime &other) const { return mds > other.mds; }
222 Q_DECL_CONSTEXPR bool operator>=(const QTime &other) const { return mds >= other.mds; }
223
224 static Q_DECL_CONSTEXPR inline QTime fromMSecsSinceStartOfDay(int msecs) { return QTime(msecs); }
225 Q_DECL_CONSTEXPR inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; }
226
227 static QTime currentTime();
228#if QT_CONFIG(datestring)
229 static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
230 static QTime fromString(const QString &s, const QString &format);
231#endif
232 static bool isValid(int h, int m, int s, int ms = 0);
233
234#if QT_DEPRECATED_SINCE(5, 14) // ### Qt 6: remove
235 QT_DEPRECATED_X("Use QElapsedTimer instead") void start();
236 QT_DEPRECATED_X("Use QElapsedTimer instead") int restart();
237 QT_DEPRECATED_X("Use QElapsedTimer instead") int elapsed() const;
238#endif
239private:
240 enum TimeFlag { NullTime = -1 };
241 Q_DECL_CONSTEXPR inline int ds() const { return mds == -1 ? 0 : mds; }
242 int mds;
243
244 friend class QDateTime;
245 friend class QDateTimePrivate;
246#ifndef QT_NO_DATASTREAM // ### Qt 6: plain QTime
247 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
248 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
249#endif
250};
251Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
252
253class QDateTimePrivate;
254
255class Q_CORE_EXPORT QDateTime
256{
257 // ### Qt 6: revisit the optimization
258 struct ShortData {
259#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
260 quintptr status : 8;
261#endif
262 // note: this is only 24 bits on 32-bit systems...
263 qintptr msecs : sizeof(void *) * 8 - 8;
264
265#if Q_BYTE_ORDER == Q_BIG_ENDIAN
266 quintptr status : 8;
267#endif
268 };
269
270 union Data {
271 enum {
272 // To be of any use, we need at least 60 years around 1970, which
273 // is 1,893,456,000,000 ms. That requires 41 bits to store, plus
274 // the sign bit. With the status byte, the minimum size is 50 bits.
275 CanBeSmall = sizeof(ShortData) * 8 > 50
276 };
277
278 Data();
279 Data(Qt::TimeSpec);
280 Data(const Data &other);
281 Data(Data &&other);
282 Data &operator=(const Data &other);
283 ~Data();
284
285 bool isShort() const;
286 void detach();
287
288 const QDateTimePrivate *operator->() const;
289 QDateTimePrivate *operator->();
290
291 QDateTimePrivate *d;
292 ShortData data;
293 };
294
295public:
296 QDateTime() noexcept(Data::CanBeSmall);
297#if QT_DEPRECATED_SINCE(5, 15) // ### Qt 6: remove
298 QT_DEPRECATED_X("Use QDate::startOfDay()") explicit QDateTime(const QDate &);
299#endif
300 QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
301 // ### Qt 6: Merge with above with default offsetSeconds = 0
302 QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds);
303#if QT_CONFIG(timezone)
304 QDateTime(const QDate &date, const QTime &time, const QTimeZone &timeZone);
305#endif // timezone
306 QDateTime(const QDateTime &other) noexcept;
307 QDateTime(QDateTime &&other) noexcept;
308 ~QDateTime();
309
310 QDateTime &operator=(QDateTime &&other) noexcept { swap(other); return *this; }
311 QDateTime &operator=(const QDateTime &other) noexcept;
312
313 void swap(QDateTime &other) noexcept { qSwap(value1&: d.d, value2&: other.d.d); }
314
315 bool isNull() const;
316 bool isValid() const;
317
318 QDate date() const;
319 QTime time() const;
320 Qt::TimeSpec timeSpec() const;
321 int offsetFromUtc() const;
322#if QT_CONFIG(timezone)
323 QTimeZone timeZone() const;
324#endif // timezone
325 QString timeZoneAbbreviation() const;
326 bool isDaylightTime() const;
327
328 qint64 toMSecsSinceEpoch() const;
329 qint64 toSecsSinceEpoch() const;
330
331 void setDate(const QDate &date); // ### Qt 6: plain QDate
332 void setTime(const QTime &time);
333 void setTimeSpec(Qt::TimeSpec spec);
334 void setOffsetFromUtc(int offsetSeconds);
335#if QT_CONFIG(timezone)
336 void setTimeZone(const QTimeZone &toZone);
337#endif // timezone
338 void setMSecsSinceEpoch(qint64 msecs);
339 void setSecsSinceEpoch(qint64 secs);
340
341#if QT_CONFIG(datestring)
342 QString toString(Qt::DateFormat format = Qt::TextDate) const;
343#if QT_STRINGVIEW_LEVEL < 2
344 QString toString(const QString &format) const;
345 QString toString(const QString &format, QCalendar cal) const;
346#endif
347 QString toString(QStringView format) const;
348 QString toString(QStringView format, QCalendar cal) const;
349#endif
350 Q_REQUIRED_RESULT QDateTime addDays(qint64 days) const;
351 Q_REQUIRED_RESULT QDateTime addMonths(int months) const;
352 Q_REQUIRED_RESULT QDateTime addYears(int years) const;
353 Q_REQUIRED_RESULT QDateTime addSecs(qint64 secs) const;
354 Q_REQUIRED_RESULT QDateTime addMSecs(qint64 msecs) const;
355
356 QDateTime toTimeSpec(Qt::TimeSpec spec) const;
357 inline QDateTime toLocalTime() const { return toTimeSpec(spec: Qt::LocalTime); }
358 inline QDateTime toUTC() const { return toTimeSpec(spec: Qt::UTC); }
359 QDateTime toOffsetFromUtc(int offsetSeconds) const;
360#if QT_CONFIG(timezone)
361 QDateTime toTimeZone(const QTimeZone &toZone) const;
362#endif // timezone
363
364 qint64 daysTo(const QDateTime &) const;
365 qint64 secsTo(const QDateTime &) const;
366 qint64 msecsTo(const QDateTime &) const;
367
368 bool operator==(const QDateTime &other) const;
369 inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
370 bool operator<(const QDateTime &other) const;
371 inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
372 inline bool operator>(const QDateTime &other) const { return other < *this; }
373 inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
374
375#if QT_DEPRECATED_SINCE(5, 2) // ### Qt 6: remove
376 QT_DEPRECATED_X("Use setOffsetFromUtc() instead") void setUtcOffset(int seconds);
377 QT_DEPRECATED_X("Use offsetFromUtc() instead") int utcOffset() const;
378#endif // QT_DEPRECATED_SINCE
379
380 static QDateTime currentDateTime();
381 static QDateTime currentDateTimeUtc();
382#if QT_CONFIG(datestring)
383 static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
384 static QDateTime fromString(const QString &s, const QString &format);
385 static QDateTime fromString(const QString &s, const QString &format, QCalendar cal);
386#endif
387
388#if QT_DEPRECATED_SINCE(5, 8)
389 uint toTime_t() const;
390 void setTime_t(uint secsSince1Jan1970UTC);
391 static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
392 static QDateTime fromTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec,
393 int offsetFromUtc = 0);
394# if QT_CONFIG(timezone)
395 static QDateTime fromTime_t(uint secsSince1Jan1970UTC, const QTimeZone &timeZone);
396# endif
397#endif
398
399 static QDateTime fromMSecsSinceEpoch(qint64 msecs);
400 // ### Qt 6: Merge with above with default spec = Qt::LocalTime
401 static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
402 static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spe = Qt::LocalTime, int offsetFromUtc = 0);
403
404#if QT_CONFIG(timezone)
405 static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
406 static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);
407#endif
408
409 static qint64 currentMSecsSinceEpoch() noexcept;
410 static qint64 currentSecsSinceEpoch() noexcept;
411
412#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
413 static QDateTime fromCFDate(CFDateRef date);
414 CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED;
415 static QDateTime fromNSDate(const NSDate *date);
416 NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED;
417#endif
418
419 // (1<<63) ms is 292277024.6 (average Gregorian) years, counted from the start of 1970, so
420 // Last is floor(1970 + 292277024.6); no year 0, so First is floor(1970 - 1 - 292277024.6)
421 enum class YearRange : qint32 { First = -292275056, Last = +292278994 };
422
423private:
424 friend class QDateTimePrivate;
425
426 Data d;
427
428#ifndef QT_NO_DATASTREAM
429 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
430 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
431#endif
432
433#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
434 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
435#endif
436};
437Q_DECLARE_SHARED(QDateTime)
438
439#ifndef QT_NO_DATASTREAM
440Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
441Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
442Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
443Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
444Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
445Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
446#endif // QT_NO_DATASTREAM
447
448#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
449Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
450Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
451Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
452#endif
453
454// QDateTime is not noexcept for now -- to be revised once
455// timezone and calendaring support is added
456Q_CORE_EXPORT uint qHash(const QDateTime &key, uint seed = 0);
457Q_CORE_EXPORT uint qHash(const QDate &key, uint seed = 0) noexcept;
458Q_CORE_EXPORT uint qHash(const QTime &key, uint seed = 0) noexcept;
459
460QT_END_NAMESPACE
461
462#endif // QDATETIME_H
463

source code of qtbase/src/corelib/time/qdatetime.h