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

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