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