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