1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // Copyright (C) 2021 Intel Corporation. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef QDATETIME_H |
6 | #define QDATETIME_H |
7 | |
8 | #include <QtCore/qcalendar.h> |
9 | #include <QtCore/qnamespace.h> |
10 | #include <QtCore/qshareddata.h> |
11 | #include <QtCore/qstring.h> |
12 | |
13 | #include <limits> |
14 | #include <chrono> |
15 | |
16 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
17 | Q_FORWARD_DECLARE_CF_TYPE(CFDate); |
18 | Q_FORWARD_DECLARE_OBJC_CLASS(NSDate); |
19 | #endif |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | class QTimeZone; |
24 | class QDateTime; |
25 | |
26 | class Q_CORE_EXPORT QDate |
27 | { |
28 | explicit constexpr QDate(qint64 julianDay) : jd(julianDay) {} |
29 | public: |
30 | constexpr QDate() : jd(nullJd()) {} |
31 | QDate(int y, int m, int d); |
32 | QDate(int y, int m, int d, QCalendar cal); |
33 | #if __cpp_lib_chrono >= 201907L || defined(Q_QDOC) |
34 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
35 | Q_IMPLICIT QDate(std::chrono::year_month_day ymd) |
36 | { |
37 | if (!ymd.ok()) |
38 | jd = nullJd(); |
39 | else |
40 | *this = fromStdSysDays(ymd); |
41 | } |
42 | |
43 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
44 | Q_IMPLICIT QDate(std::chrono::year_month_day_last ymdl) |
45 | { |
46 | if (!ymdl.ok()) |
47 | jd = nullJd(); |
48 | else |
49 | *this = fromStdSysDays(ymdl); |
50 | } |
51 | |
52 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
53 | Q_IMPLICIT QDate(std::chrono::year_month_weekday ymw) |
54 | { |
55 | if (!ymw.ok()) |
56 | jd = nullJd(); |
57 | else |
58 | *this = fromStdSysDays(ymw); |
59 | } |
60 | |
61 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
62 | Q_IMPLICIT QDate(std::chrono::year_month_weekday_last ymwl) |
63 | { |
64 | if (!ymwl.ok()) |
65 | jd = nullJd(); |
66 | else |
67 | *this = fromStdSysDays(ymwl); |
68 | } |
69 | |
70 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
71 | static QDate fromStdSysDays(const std::chrono::sys_days &days) |
72 | { |
73 | const QDate epoch(unixEpochJd()); |
74 | return epoch.addDays(days.time_since_epoch().count()); |
75 | } |
76 | |
77 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
78 | std::chrono::sys_days toStdSysDays() const |
79 | { |
80 | const QDate epoch(unixEpochJd()); |
81 | return std::chrono::sys_days(std::chrono::days(epoch.daysTo(*this))); |
82 | } |
83 | #endif |
84 | |
85 | constexpr bool isNull() const { return !isValid(); } |
86 | constexpr bool isValid() const { return jd >= minJd() && jd <= maxJd(); } |
87 | |
88 | // Gregorian-optimized: |
89 | int year() const; |
90 | int month() const; |
91 | int day() const; |
92 | int dayOfWeek() const; |
93 | int dayOfYear() const; |
94 | int daysInMonth() const; |
95 | int daysInYear() const; |
96 | int weekNumber(int *yearNum = nullptr) const; // ISO 8601, always Gregorian |
97 | |
98 | int year(QCalendar cal) const; |
99 | int month(QCalendar cal) const; |
100 | int day(QCalendar cal) const; |
101 | int dayOfWeek(QCalendar cal) const; |
102 | int dayOfYear(QCalendar cal) const; |
103 | int daysInMonth(QCalendar cal) const; |
104 | int daysInYear(QCalendar cal) const; |
105 | |
106 | #if QT_DEPRECATED_SINCE(6, 9) |
107 | QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead" ) |
108 | QDateTime startOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const; |
109 | QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead" ) |
110 | QDateTime endOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const; |
111 | #endif |
112 | |
113 | QDateTime startOfDay(const QTimeZone &zone) const; |
114 | QDateTime endOfDay(const QTimeZone &zone) const; |
115 | QDateTime startOfDay() const; |
116 | QDateTime endOfDay() const; |
117 | |
118 | #if QT_CONFIG(datestring) |
119 | QString toString(Qt::DateFormat format = Qt::TextDate) const; |
120 | QString toString(const QString &format, QCalendar cal = QCalendar()) const |
121 | { return toString(format: qToStringViewIgnoringNull(s: format), cal); } |
122 | QString toString(QStringView format, QCalendar cal = QCalendar()) const; |
123 | #endif |
124 | bool setDate(int year, int month, int day); // Gregorian-optimized |
125 | bool setDate(int year, int month, int day, QCalendar cal); |
126 | |
127 | void getDate(int *year, int *month, int *day) const; |
128 | |
129 | [[nodiscard]] QDate addDays(qint64 days) const; |
130 | #if __cpp_lib_chrono >= 201907L || defined(Q_QDOC) |
131 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
132 | [[nodiscard]] QDate addDuration(std::chrono::days days) const |
133 | { |
134 | return addDays(days.count()); |
135 | } |
136 | #endif |
137 | // Gregorian-optimized: |
138 | [[nodiscard]] QDate addMonths(int months) const; |
139 | [[nodiscard]] QDate addYears(int years) const; |
140 | [[nodiscard]] QDate addMonths(int months, QCalendar cal) const; |
141 | [[nodiscard]] QDate addYears(int years, QCalendar cal) const; |
142 | qint64 daysTo(QDate d) const; |
143 | |
144 | static QDate currentDate(); |
145 | #if QT_CONFIG(datestring) |
146 | static QDate fromString(QStringView string, Qt::DateFormat format = Qt::TextDate); |
147 | static QDate fromString(QStringView string, QStringView format, QCalendar cal = QCalendar()) |
148 | { return fromString(string: string.toString(), format, cal); } |
149 | static QDate fromString(const QString &string, QStringView format, QCalendar cal = QCalendar()); |
150 | static QDate fromString(const QString &string, Qt::DateFormat format = Qt::TextDate) |
151 | { return fromString(string: qToStringViewIgnoringNull(s: string), format); } |
152 | static QDate fromString(const QString &string, const QString &format, |
153 | QCalendar cal = QCalendar()) |
154 | { return fromString(string, format: qToStringViewIgnoringNull(s: format), cal); } |
155 | #endif |
156 | static bool isValid(int y, int m, int d); |
157 | static bool isLeapYear(int year); |
158 | |
159 | static constexpr inline QDate fromJulianDay(qint64 jd_) |
160 | { return jd_ >= minJd() && jd_ <= maxJd() ? QDate(jd_) : QDate() ; } |
161 | constexpr inline qint64 toJulianDay() const { return jd; } |
162 | |
163 | private: |
164 | // using extra parentheses around min to avoid expanding it if it is a macro |
165 | static constexpr inline qint64 nullJd() { return (std::numeric_limits<qint64>::min)(); } |
166 | static constexpr inline qint64 minJd() { return Q_INT64_C(-784350574879); } |
167 | static constexpr inline qint64 maxJd() { return Q_INT64_C( 784354017364); } |
168 | static constexpr inline qint64 unixEpochJd() { return Q_INT64_C(2440588); } |
169 | |
170 | qint64 jd; |
171 | |
172 | friend class QDateTime; |
173 | friend class QDateTimePrivate; |
174 | |
175 | friend constexpr bool operator==(QDate lhs, QDate rhs) { return lhs.jd == rhs.jd; } |
176 | friend constexpr bool operator!=(QDate lhs, QDate rhs) { return lhs.jd != rhs.jd; } |
177 | friend constexpr bool operator< (QDate lhs, QDate rhs) { return lhs.jd < rhs.jd; } |
178 | friend constexpr bool operator<=(QDate lhs, QDate rhs) { return lhs.jd <= rhs.jd; } |
179 | friend constexpr bool operator> (QDate lhs, QDate rhs) { return lhs.jd > rhs.jd; } |
180 | friend constexpr bool operator>=(QDate lhs, QDate rhs) { return lhs.jd >= rhs.jd; } |
181 | #ifndef QT_NO_DATASTREAM |
182 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate); |
183 | friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &); |
184 | #endif |
185 | }; |
186 | Q_DECLARE_TYPEINFO(QDate, Q_RELOCATABLE_TYPE); |
187 | |
188 | class Q_CORE_EXPORT QTime |
189 | { |
190 | explicit constexpr QTime(int ms) : mds(ms) |
191 | {} |
192 | public: |
193 | constexpr QTime(): mds(NullTime) |
194 | {} |
195 | QTime(int h, int m, int s = 0, int ms = 0); |
196 | |
197 | constexpr bool isNull() const { return mds == NullTime; } |
198 | bool isValid() const; |
199 | |
200 | int hour() const; |
201 | int minute() const; |
202 | int second() const; |
203 | int msec() const; |
204 | #if QT_CONFIG(datestring) |
205 | QString toString(Qt::DateFormat f = Qt::TextDate) const; |
206 | QString toString(const QString &format) const |
207 | { return toString(format: qToStringViewIgnoringNull(s: format)); } |
208 | QString toString(QStringView format) const; |
209 | #endif |
210 | bool setHMS(int h, int m, int s, int ms = 0); |
211 | |
212 | [[nodiscard]] QTime addSecs(int secs) const; |
213 | int secsTo(QTime t) const; |
214 | [[nodiscard]] QTime addMSecs(int ms) const; |
215 | int msecsTo(QTime t) const; |
216 | |
217 | static constexpr inline QTime fromMSecsSinceStartOfDay(int msecs) { return QTime(msecs); } |
218 | constexpr inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; } |
219 | |
220 | static QTime currentTime(); |
221 | #if QT_CONFIG(datestring) |
222 | static QTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate); |
223 | static QTime fromString(QStringView string, QStringView format) |
224 | { return fromString(string: string.toString(), format); } |
225 | static QTime fromString(const QString &string, QStringView format); |
226 | static QTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate) |
227 | { return fromString(string: qToStringViewIgnoringNull(s: string), format); } |
228 | static QTime fromString(const QString &string, const QString &format) |
229 | { return fromString(string, format: qToStringViewIgnoringNull(s: format)); } |
230 | #endif |
231 | static bool isValid(int h, int m, int s, int ms = 0); |
232 | |
233 | private: |
234 | enum TimeFlag { NullTime = -1 }; |
235 | constexpr inline int ds() const { return mds == -1 ? 0 : mds; } |
236 | int mds; |
237 | |
238 | friend constexpr bool operator==(QTime lhs, QTime rhs) { return lhs.mds == rhs.mds; } |
239 | friend constexpr bool operator!=(QTime lhs, QTime rhs) { return lhs.mds != rhs.mds; } |
240 | friend constexpr bool operator< (QTime lhs, QTime rhs) { return lhs.mds < rhs.mds; } |
241 | friend constexpr bool operator<=(QTime lhs, QTime rhs) { return lhs.mds <= rhs.mds; } |
242 | friend constexpr bool operator> (QTime lhs, QTime rhs) { return lhs.mds > rhs.mds; } |
243 | friend constexpr bool operator>=(QTime lhs, QTime rhs) { return lhs.mds >= rhs.mds; } |
244 | |
245 | friend class QDateTime; |
246 | friend class QDateTimePrivate; |
247 | #ifndef QT_NO_DATASTREAM |
248 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime); |
249 | friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &); |
250 | #endif |
251 | }; |
252 | Q_DECLARE_TYPEINFO(QTime, Q_RELOCATABLE_TYPE); |
253 | |
254 | class QDateTimePrivate; |
255 | |
256 | class Q_CORE_EXPORT QDateTime |
257 | { |
258 | struct ShortData { |
259 | #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || defined(QT_BOOTSTRAPPED) |
260 | # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
261 | qint64 status : 8; |
262 | # endif |
263 | qint64 msecs : 56; |
264 | |
265 | # if Q_BYTE_ORDER == Q_BIG_ENDIAN |
266 | qint64 status : 8; |
267 | # endif |
268 | #else |
269 | # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
270 | quintptr status : 8; |
271 | # endif |
272 | // note: this is only 24 bits on 32-bit systems... |
273 | qintptr msecs : sizeof(void *) * 8 - 8; |
274 | |
275 | # if Q_BYTE_ORDER == Q_BIG_ENDIAN |
276 | quintptr status : 8; |
277 | # endif |
278 | #endif |
279 | friend constexpr bool operator==(const ShortData &lhs, const ShortData &rhs) |
280 | { return lhs.status == rhs.status && lhs.msecs == rhs.msecs; } |
281 | }; |
282 | |
283 | union Data { |
284 | // To be of any use, we need at least 60 years around 1970, which |
285 | // is 1,893,456,000,000 ms. That requires 41 bits to store, plus |
286 | // the sign bit. With the status byte, the minimum size is 50 bits. |
287 | static constexpr bool CanBeSmall = sizeof(ShortData) * 8 > 50; |
288 | |
289 | Data() noexcept; |
290 | Data(const QTimeZone &); |
291 | Data(const Data &other) noexcept; |
292 | Data(Data &&other) noexcept; |
293 | Data &operator=(const Data &other) noexcept; |
294 | Data &operator=(Data &&other) noexcept { swap(other); return *this; } |
295 | ~Data(); |
296 | |
297 | void swap(Data &other) noexcept |
298 | { std::swap(a&: data, b&: other.data); } |
299 | |
300 | bool isShort() const; |
301 | void detach(); |
302 | QTimeZone timeZone() const; |
303 | |
304 | const QDateTimePrivate *operator->() const; |
305 | QDateTimePrivate *operator->(); |
306 | |
307 | QDateTimePrivate *d; |
308 | ShortData data; |
309 | }; |
310 | |
311 | public: |
312 | QDateTime() noexcept; |
313 | #if QT_DEPRECATED_SINCE(6, 9) |
314 | QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead" ) |
315 | QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0); |
316 | #endif |
317 | QDateTime(QDate date, QTime time, const QTimeZone &timeZone); |
318 | QDateTime(QDate date, QTime time); |
319 | QDateTime(const QDateTime &other) noexcept; |
320 | QDateTime(QDateTime &&other) noexcept; |
321 | ~QDateTime(); |
322 | |
323 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDateTime) |
324 | QDateTime &operator=(const QDateTime &other) noexcept; |
325 | |
326 | void swap(QDateTime &other) noexcept { d.swap(other&: other.d); } |
327 | |
328 | bool isNull() const; |
329 | bool isValid() const; |
330 | |
331 | QDate date() const; |
332 | QTime time() const; |
333 | Qt::TimeSpec timeSpec() const; |
334 | int offsetFromUtc() const; |
335 | QTimeZone timeRepresentation() const; |
336 | #if QT_CONFIG(timezone) |
337 | QTimeZone timeZone() const; |
338 | #endif // timezone |
339 | QString timeZoneAbbreviation() const; |
340 | bool isDaylightTime() const; |
341 | |
342 | qint64 toMSecsSinceEpoch() const; |
343 | qint64 toSecsSinceEpoch() const; |
344 | |
345 | void setDate(QDate date); |
346 | void setTime(QTime time); |
347 | #if QT_DEPRECATED_SINCE(6, 9) |
348 | QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead" ) |
349 | void setTimeSpec(Qt::TimeSpec spec); |
350 | QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead" ) |
351 | void setOffsetFromUtc(int offsetSeconds); |
352 | #endif |
353 | void setTimeZone(const QTimeZone &toZone); |
354 | void setMSecsSinceEpoch(qint64 msecs); |
355 | void setSecsSinceEpoch(qint64 secs); |
356 | |
357 | #if QT_CONFIG(datestring) |
358 | QString toString(Qt::DateFormat format = Qt::TextDate) const; |
359 | QString toString(const QString &format, QCalendar cal = QCalendar()) const |
360 | { return toString(format: qToStringViewIgnoringNull(s: format), cal); } |
361 | QString toString(QStringView format, QCalendar cal = QCalendar()) const; |
362 | #endif |
363 | [[nodiscard]] QDateTime addDays(qint64 days) const; |
364 | [[nodiscard]] QDateTime addMonths(int months) const; |
365 | [[nodiscard]] QDateTime addYears(int years) const; |
366 | [[nodiscard]] QDateTime addSecs(qint64 secs) const; |
367 | [[nodiscard]] QDateTime addMSecs(qint64 msecs) const; |
368 | [[nodiscard]] QDateTime addDuration(std::chrono::milliseconds msecs) const |
369 | { |
370 | return addMSecs(msecs: msecs.count()); |
371 | } |
372 | |
373 | #if QT_DEPRECATED_SINCE(6, 9) |
374 | QT_DEPRECATED_VERSION_X_6_9("Use toTimeZone instead" ) |
375 | QDateTime toTimeSpec(Qt::TimeSpec spec) const; |
376 | #endif |
377 | QDateTime toLocalTime() const; |
378 | QDateTime toUTC() const; |
379 | QDateTime toOffsetFromUtc(int offsetSeconds) const; |
380 | QDateTime toTimeZone(const QTimeZone &toZone) const; |
381 | |
382 | qint64 daysTo(const QDateTime &) const; |
383 | qint64 secsTo(const QDateTime &) const; |
384 | qint64 msecsTo(const QDateTime &) const; |
385 | |
386 | static QDateTime currentDateTime(const QTimeZone &zone); |
387 | static QDateTime currentDateTime(); |
388 | static QDateTime currentDateTimeUtc(); |
389 | #if QT_CONFIG(datestring) |
390 | static QDateTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate); |
391 | static QDateTime fromString(QStringView string, QStringView format, |
392 | QCalendar cal = QCalendar()) |
393 | { return fromString(string: string.toString(), format, cal); } |
394 | static QDateTime fromString(const QString &string, QStringView format, |
395 | QCalendar cal = QCalendar()); |
396 | static QDateTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate) |
397 | { return fromString(string: qToStringViewIgnoringNull(s: string), format); } |
398 | static QDateTime fromString(const QString &string, const QString &format, |
399 | QCalendar cal = QCalendar()) |
400 | { return fromString(string, format: qToStringViewIgnoringNull(s: format), cal); } |
401 | #endif |
402 | |
403 | #if QT_DEPRECATED_SINCE(6, 9) |
404 | QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset" ) |
405 | static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0); |
406 | QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset" ) |
407 | static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetFromUtc = 0); |
408 | #endif |
409 | |
410 | static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone); |
411 | static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone); |
412 | static QDateTime fromMSecsSinceEpoch(qint64 msecs); |
413 | static QDateTime fromSecsSinceEpoch(qint64 secs); |
414 | |
415 | static qint64 currentMSecsSinceEpoch() noexcept; |
416 | static qint64 currentSecsSinceEpoch() noexcept; |
417 | |
418 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
419 | static QDateTime fromCFDate(CFDateRef date); |
420 | CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED; |
421 | static QDateTime fromNSDate(const NSDate *date); |
422 | NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED; |
423 | #endif |
424 | |
425 | #if __cpp_lib_chrono >= 201907L || defined(Q_QDOC) |
426 | #if __cpp_concepts >= 201907L || defined(Q_QDOC) |
427 | // Generic clock, as long as it's compatible with us (= system_clock) |
428 | template <typename Clock, typename Duration> |
429 | static QDateTime fromStdTimePoint(const std::chrono::time_point<Clock, Duration> &time) |
430 | requires |
431 | requires(const std::chrono::time_point<Clock, Duration> &t) { |
432 | // the clock can be converted to system_clock |
433 | std::chrono::clock_cast<std::chrono::system_clock>(t); |
434 | // the duration can be converted to milliseconds |
435 | requires std::is_convertible_v<Duration, std::chrono::milliseconds>; |
436 | } |
437 | { |
438 | const auto sysTime = std::chrono::clock_cast<std::chrono::system_clock>(time); |
439 | // clock_cast can change the duration, so convert it again to milliseconds |
440 | const auto timeInMSec = std::chrono::time_point_cast<std::chrono::milliseconds>(sysTime); |
441 | return fromMSecsSinceEpoch(timeInMSec.time_since_epoch().count(), Qt::UTC); |
442 | } |
443 | #endif // __cpp_concepts |
444 | |
445 | // local_time |
446 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
447 | static QDateTime fromStdTimePoint(const std::chrono::local_time<std::chrono::milliseconds> &time) |
448 | { |
449 | return fromStdLocalTime(time); |
450 | } |
451 | |
452 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
453 | static QDateTime fromStdLocalTime(const std::chrono::local_time<std::chrono::milliseconds> &time) |
454 | { |
455 | QDateTime result(QDate(1970, 1, 1), QTime(0, 0, 0)); |
456 | return result.addMSecs(time.time_since_epoch().count()); |
457 | } |
458 | |
459 | #if QT_CONFIG(timezone) && (__cpp_lib_chrono >= 201907L || defined(Q_QDOC)) |
460 | // zoned_time. defined in qtimezone.h |
461 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
462 | static QDateTime fromStdZonedTime(const std::chrono::zoned_time< |
463 | std::chrono::milliseconds, |
464 | const std::chrono::time_zone * |
465 | > &time); |
466 | #endif // QT_CONFIG(timezone) |
467 | |
468 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
469 | std::chrono::sys_time<std::chrono::milliseconds> toStdSysMilliseconds() const |
470 | { |
471 | const std::chrono::milliseconds duration(toMSecsSinceEpoch()); |
472 | return std::chrono::sys_time<std::chrono::milliseconds>(duration); |
473 | } |
474 | |
475 | QT_POST_CXX17_API_IN_EXPORTED_CLASS |
476 | std::chrono::sys_seconds toStdSysSeconds() const |
477 | { |
478 | const std::chrono::seconds duration(toSecsSinceEpoch()); |
479 | return std::chrono::sys_seconds(duration); |
480 | } |
481 | #endif // __cpp_lib_chrono >= 201907L |
482 | |
483 | friend std::chrono::milliseconds operator-(const QDateTime &lhs, const QDateTime &rhs) |
484 | { |
485 | return std::chrono::milliseconds(rhs.msecsTo(lhs)); |
486 | } |
487 | |
488 | friend QDateTime operator+(const QDateTime &dateTime, std::chrono::milliseconds duration) |
489 | { |
490 | return dateTime.addMSecs(msecs: duration.count()); |
491 | } |
492 | |
493 | friend QDateTime operator+(std::chrono::milliseconds duration, const QDateTime &dateTime) |
494 | { |
495 | return dateTime + duration; |
496 | } |
497 | |
498 | QDateTime &operator+=(std::chrono::milliseconds duration) |
499 | { |
500 | *this = addMSecs(msecs: duration.count()); |
501 | return *this; |
502 | } |
503 | |
504 | friend QDateTime operator-(const QDateTime &dateTime, std::chrono::milliseconds duration) |
505 | { |
506 | return dateTime.addMSecs(msecs: -duration.count()); |
507 | } |
508 | |
509 | QDateTime &operator-=(std::chrono::milliseconds duration) |
510 | { |
511 | *this = addMSecs(msecs: -duration.count()); |
512 | return *this; |
513 | } |
514 | |
515 | // (1<<63) ms is 292277024.6 (average Gregorian) years, counted from the start of 1970, so |
516 | // Last is floor(1970 + 292277024.6); no year 0, so First is floor(1970 - 1 - 292277024.6) |
517 | enum class YearRange : qint32 { First = -292275056, Last = +292278994 }; |
518 | |
519 | private: |
520 | bool equals(const QDateTime &other) const; |
521 | bool precedes(const QDateTime &other) const; |
522 | friend class QDateTimePrivate; |
523 | |
524 | Data d; |
525 | |
526 | friend bool operator==(const QDateTime &lhs, const QDateTime &rhs) { return lhs.equals(other: rhs); } |
527 | friend bool operator!=(const QDateTime &lhs, const QDateTime &rhs) { return !(lhs == rhs); } |
528 | friend bool operator<(const QDateTime &lhs, const QDateTime &rhs) { return lhs.precedes(other: rhs); } |
529 | friend bool operator<=(const QDateTime &lhs, const QDateTime &rhs) { return !(rhs < lhs); } |
530 | friend bool operator>(const QDateTime &lhs, const QDateTime &rhs) { return rhs.precedes(other: lhs); } |
531 | friend bool operator>=(const QDateTime &lhs, const QDateTime &rhs) { return !(lhs < rhs); } |
532 | |
533 | #ifndef QT_NO_DATASTREAM |
534 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &); |
535 | friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &); |
536 | #endif |
537 | |
538 | #if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring) |
539 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &); |
540 | #endif |
541 | }; |
542 | Q_DECLARE_SHARED(QDateTime) |
543 | |
544 | #ifndef QT_NO_DATASTREAM |
545 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate); |
546 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &); |
547 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime); |
548 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &); |
549 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &); |
550 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &); |
551 | #endif // QT_NO_DATASTREAM |
552 | |
553 | #if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring) |
554 | Q_CORE_EXPORT QDebug operator<<(QDebug, QDate); |
555 | Q_CORE_EXPORT QDebug operator<<(QDebug, QTime); |
556 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &); |
557 | #endif |
558 | |
559 | // QDateTime is not noexcept for now -- to be revised once |
560 | // timezone and calendaring support is added |
561 | Q_CORE_EXPORT size_t qHash(const QDateTime &key, size_t seed = 0); |
562 | Q_CORE_EXPORT size_t qHash(QDate key, size_t seed = 0) noexcept; |
563 | Q_CORE_EXPORT size_t qHash(QTime key, size_t seed = 0) noexcept; |
564 | |
565 | QT_END_NAMESPACE |
566 | |
567 | #endif // QDATETIME_H |
568 | |