Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdatetime.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
5
6#ifndef QDATETIME_H
7#define QDATETIME_H
8
9#include <QtCore/qcalendar.h>
10#include <QtCore/qcompare.h>
11#include <QtCore/qlocale.h>
12#include <QtCore/qnamespace.h>
13#include <QtCore/qshareddata.h>
14#include <QtCore/qstring.h>
15
16#include <climits>
17#include <chrono>
18
19#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
20Q_FORWARD_DECLARE_CF_TYPE(CFDate);
21Q_FORWARD_DECLARE_OBJC_CLASS(NSDate);
22#endif
23
24QT_BEGIN_NAMESPACE
25
26class QTimeZone;
27class QDateTime;
28
29class Q_CORE_EXPORT QDate
30{
31 explicit constexpr QDate(qint64 julianDay) : jd(julianDay) {}
32public:
33 using difference_type = qint64;
34
35 constexpr QDate() : jd(nullJd()) {}
36 QDate(int y, int m, int d);
37 QDate(int y, int m, int d, QCalendar cal);
38// INTEGRITY incident-85878 (timezone and clock_cast are not supported)
39#if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
40 QT_POST_CXX17_API_IN_EXPORTED_CLASS
41 Q_IMPLICIT constexpr QDate(std::chrono::year_month_day date) noexcept
42 : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
43 {}
44
45 QT_POST_CXX17_API_IN_EXPORTED_CLASS
46 Q_IMPLICIT constexpr QDate(std::chrono::year_month_day_last date) noexcept
47 : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
48 {}
49
50 QT_POST_CXX17_API_IN_EXPORTED_CLASS
51 Q_IMPLICIT constexpr QDate(std::chrono::year_month_weekday date) noexcept
52 : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
53 {}
54
55 QT_POST_CXX17_API_IN_EXPORTED_CLASS
56 Q_IMPLICIT constexpr QDate(std::chrono::year_month_weekday_last date) noexcept
57 : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
58 {}
59
60 QT_POST_CXX17_API_IN_EXPORTED_CLASS
61 static constexpr QDate fromStdSysDays(const std::chrono::sys_days &days) noexcept
62 {
63 return QDate(stdSysDaysToJulianDay(days));
64 }
65
66 QT_POST_CXX17_API_IN_EXPORTED_CLASS
67 constexpr std::chrono::sys_days toStdSysDays() const noexcept
68 {
69 const qint64 days = isValid() ? jd - unixEpochJd() : 0;
70 return std::chrono::sys_days(std::chrono::days(days));
71 }
72#endif
73
74 constexpr bool isNull() const { return !isValid(); }
75 constexpr bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
76
77 // Gregorian-optimized:
78 int year() const;
79 int month() const;
80 int day() const;
81 int dayOfWeek() const;
82 int dayOfYear() const;
83 int daysInMonth() const;
84 int daysInYear() const;
85 int weekNumber(int *yearNum = nullptr) const; // ISO 8601, always Gregorian
86
87 int year(QCalendar cal) const;
88 int month(QCalendar cal) const;
89 int day(QCalendar cal) const;
90 int dayOfWeek(QCalendar cal) const;
91 int dayOfYear(QCalendar cal) const;
92 int daysInMonth(QCalendar cal) const;
93 int daysInYear(QCalendar cal) const;
94
95#if QT_DEPRECATED_SINCE(6, 9)
96 QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
97 QDateTime startOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
98 QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
99 QDateTime endOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
100#endif
101
102 QDateTime startOfDay(const QTimeZone &zone) const;
103 QDateTime endOfDay(const QTimeZone &zone) const;
104 QDateTime startOfDay() const;
105 QDateTime endOfDay() const;
106
107#if QT_CONFIG(datestring)
108 QString toString(Qt::DateFormat format = Qt::TextDate) const;
109 QString toString(const QString &format) const;
110 QString toString(const QString &format, QCalendar cal) const
111 { return toString(qToStringViewIgnoringNull(format), cal); }
112 QString toString(QStringView format) const;
113 QString toString(QStringView format, QCalendar cal) const;
114#endif
115 bool setDate(int year, int month, int day); // Gregorian-optimized
116 bool setDate(int year, int month, int day, QCalendar cal);
117
118 void getDate(int *year, int *month, int *day) const;
119
120 [[nodiscard]] QDate addDays(qint64 days) const;
121// INTEGRITY incident-85878 (timezone and clock_cast are not supported)
122#if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
123 QT_POST_CXX17_API_IN_EXPORTED_CLASS
124 [[nodiscard]] QDate addDuration(std::chrono::days days) const
125 {
126 return addDays(days.count());
127 }
128#endif
129 // Gregorian-optimized:
130 [[nodiscard]] QDate addMonths(int months) const;
131 [[nodiscard]] QDate addYears(int years) const;
132 [[nodiscard]] QDate addMonths(int months, QCalendar cal) const;
133 [[nodiscard]] QDate addYears(int years, QCalendar cal) const;
134 qint64 daysTo(QDate d) const;
135
136 static QDate currentDate();
137#if QT_CONFIG(datestring)
138 // No DateFormat accepts a two-digit year, so no need for baseYear:
139 static QDate fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
140 static QDate fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
141 { return fromString(qToStringViewIgnoringNull(string), format); }
142
143 // Accept calendar without over-ride of base year:
144 static QDate fromString(QStringView string, QStringView format, QCalendar cal)
145 { return fromString(string.toString(), format, QLocale::DefaultTwoDigitBaseYear, cal); }
146 QT_CORE_INLINE_SINCE(6, 7)
147 static QDate fromString(const QString &string, QStringView format, QCalendar cal);
148 static QDate fromString(const QString &string, const QString &format, QCalendar cal)
149 { return fromString(string, qToStringViewIgnoringNull(format), QLocale::DefaultTwoDigitBaseYear, cal); }
150
151 // Overriding base year is likely more common than overriding calendar (and
152 // likely to get more so, as the legacy base drops ever further behind us).
153 static QDate fromString(QStringView string, QStringView format,
154 int baseYear = QLocale::DefaultTwoDigitBaseYear)
155 { return fromString(string.toString(), format, baseYear); }
156 static QDate fromString(QStringView string, QStringView format,
157 int baseYear, QCalendar cal)
158 { return fromString(string.toString(), format, baseYear, cal); }
159 static QDate fromString(const QString &string, QStringView format,
160 int baseYear = QLocale::DefaultTwoDigitBaseYear);
161 static QDate fromString(const QString &string, QStringView format,
162 int baseYear, QCalendar cal);
163 static QDate fromString(const QString &string, const QString &format,
164 int baseYear = QLocale::DefaultTwoDigitBaseYear)
165 { return fromString(string, qToStringViewIgnoringNull(format), baseYear); }
166 static QDate fromString(const QString &string, const QString &format,
167 int baseYear, QCalendar cal)
168 { return fromString(string, qToStringViewIgnoringNull(format), baseYear, cal); }
169#endif
170 static bool isValid(int y, int m, int d);
171 static bool isLeapYear(int year);
172
173 static constexpr inline QDate fromJulianDay(qint64 jd_)
174 { return jd_ >= minJd() && jd_ <= maxJd() ? QDate(jd_) : QDate() ; }
175 constexpr inline qint64 toJulianDay() const { return jd; }
176
177private:
178 // using extra parentheses around min to avoid expanding it if it is a macro
179 static constexpr inline qint64 nullJd() { return (std::numeric_limits<qint64>::min)(); }
180 static constexpr inline qint64 minJd() { return Q_INT64_C(-784350574879); }
181 static constexpr inline qint64 maxJd() { return Q_INT64_C( 784354017364); }
182 static constexpr inline qint64 unixEpochJd() { return Q_INT64_C(2440588); }
183
184// INTEGRITY incident-85878 (timezone and clock_cast are not supported)
185#if __cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)
186#if !QT_CORE_REMOVED_SINCE(6, 7)
187 QT_POST_CXX17_API_IN_EXPORTED_CLASS
188#endif
189 static constexpr qint64
190 stdSysDaysToJulianDay(const std::chrono::sys_days &days) noexcept
191 {
192 const auto epochDays = days.time_since_epoch().count();
193 // minJd() and maxJd() fit into 40 bits.
194 if constexpr (sizeof(epochDays) * CHAR_BIT >= 41) {
195 constexpr auto top = maxJd() - unixEpochJd();
196 constexpr auto bottom = minJd() - unixEpochJd();
197 if (epochDays > top || epochDays < bottom)
198 return nullJd();
199 }
200 return unixEpochJd() + epochDays;
201 }
202#endif // __cpp_lib_chrono >= 201907L
203
204 qint64 jd;
205
206 friend class QDateTime;
207 friend class QDateTimeParser;
208 friend class QDateTimePrivate;
209
210 friend constexpr bool comparesEqual(const QDate &lhs, const QDate &rhs) noexcept
211 { return lhs.jd == rhs.jd; }
212 friend constexpr Qt::strong_ordering
213 compareThreeWay(const QDate &lhs, const QDate &rhs) noexcept
214 { return Qt::compareThreeWay(lhs.jd, rhs.jd); }
215 Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QDate)
216
217 friend QDate &operator++(QDate &date)
218 {
219 date = date.addDays(1);
220 return date;
221 }
222
223 friend QDate &operator--(QDate &date)
224 {
225 date = date.addDays(-1);
226 return date;
227 }
228
229 friend QDate operator++(QDate &date, int)
230 {
231 QDate old = date;
232 ++date;
233 return old;
234 }
235
236 friend QDate operator--(QDate &date, int)
237 {
238 QDate old = date;
239 --date;
240 return old;
241 }
242
243#ifndef QT_NO_DATASTREAM
244 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate);
245 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
246#endif
247};
248Q_DECLARE_TYPEINFO(QDate, Q_RELOCATABLE_TYPE);
249
250class Q_CORE_EXPORT QTime
251{
252 explicit constexpr QTime(int ms) : mds(ms)
253 {}
254public:
255 constexpr QTime(): mds(NullTime)
256 {}
257 QTime(int h, int m, int s = 0, int ms = 0);
258
259 constexpr bool isNull() const { return mds == NullTime; }
260 bool isValid() const;
261
262 int hour() const;
263 int minute() const;
264 int second() const;
265 int msec() const;
266#if QT_CONFIG(datestring)
267 QString toString(Qt::DateFormat f = Qt::TextDate) const;
268 QString toString(const QString &format) const
269 { return toString(qToStringViewIgnoringNull(format)); }
270 QString toString(QStringView format) const;
271#endif
272 bool setHMS(int h, int m, int s, int ms = 0);
273
274 [[nodiscard]] QTime addSecs(int secs) const;
275 int secsTo(QTime t) const;
276 [[nodiscard]] QTime addMSecs(int ms) const;
277 int msecsTo(QTime t) const;
278
279 static constexpr inline QTime fromMSecsSinceStartOfDay(int msecs) { return QTime(msecs); }
280 constexpr inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; }
281
282 static QTime currentTime();
283#if QT_CONFIG(datestring)
284 static QTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
285 static QTime fromString(QStringView string, QStringView format)
286 { return fromString(string.toString(), format); }
287 static QTime fromString(const QString &string, QStringView format);
288 static QTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
289 { return fromString(qToStringViewIgnoringNull(string), format); }
290 static QTime fromString(const QString &string, const QString &format)
291 { return fromString(string, qToStringViewIgnoringNull(format)); }
292#endif
293 static bool isValid(int h, int m, int s, int ms = 0);
294
295private:
296 enum TimeFlag { NullTime = -1 };
297 constexpr inline int ds() const { return mds == -1 ? 0 : mds; }
298 int mds;
299
300 friend constexpr bool comparesEqual(const QTime &lhs, const QTime &rhs) noexcept
301 { return lhs.mds == rhs.mds; }
302 friend constexpr Qt::strong_ordering
303 compareThreeWay(const QTime &lhs, const QTime &rhs) noexcept
304 { return Qt::compareThreeWay(lhs.mds, rhs.mds); }
305 Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QTime)
306
307 friend class QDateTime;
308 friend class QDateTimePrivate;
309#ifndef QT_NO_DATASTREAM
310 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime);
311 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
312#endif
313};
314Q_DECLARE_TYPEINFO(QTime, Q_RELOCATABLE_TYPE);
315
316class QDateTimePrivate;
317
318class Q_CORE_EXPORT QDateTime
319{
320 struct ShortData {
321#if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || defined(QT_BOOTSTRAPPED)
322# if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
323 qint64 status : 8;
324# endif
325 qint64 msecs : 56;
326
327# if Q_BYTE_ORDER == Q_BIG_ENDIAN
328 qint64 status : 8;
329# endif
330#else
331# if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
332 quintptr status : 8;
333# endif
334 // note: this is only 24 bits on 32-bit systems...
335 qintptr msecs : sizeof(void *) * 8 - 8;
336
337# if Q_BYTE_ORDER == Q_BIG_ENDIAN
338 quintptr status : 8;
339# endif
340#endif
341 friend constexpr bool operator==(ShortData lhs, ShortData rhs)
342 { return lhs.status == rhs.status && lhs.msecs == rhs.msecs; }
343 };
344
345 union Data {
346 // To be of any use, we need at least 60 years around 1970, which
347 // is 1,893,456,000,000 ms. That requires 41 bits to store, plus
348 // the sign bit. With the status byte, the minimum size is 50 bits.
349 static constexpr bool CanBeSmall = sizeof(ShortData) * 8 > 50;
350
351 Data() noexcept;
352 Data(const QTimeZone &);
353 Data(const Data &other) noexcept;
354 Data(Data &&other) noexcept;
355 Data &operator=(const Data &other) noexcept;
356 Data &operator=(Data &&other) noexcept { swap(other); return *this; }
357 ~Data();
358
359 void swap(Data &other) noexcept
360 { std::swap(data, other.data); }
361
362 bool isShort() const;
363 inline void invalidate();
364 void detach();
365 QTimeZone timeZone() const;
366
367 const QDateTimePrivate *operator->() const;
368 QDateTimePrivate *operator->();
369
370 QDateTimePrivate *d;
371 ShortData data;
372 };
373
374 friend class QTypeInfo<Data>;
375
376public:
377 QDateTime() noexcept;
378
379 enum class TransitionResolution {
380 Reject = 0,
381 RelativeToBefore,
382 RelativeToAfter,
383 PreferBefore,
384 PreferAfter,
385 PreferStandard,
386 PreferDaylightSaving,
387 // Closest match to behavior prior to introducing TransitionResolution:
388 LegacyBehavior = RelativeToBefore
389 };
390
391#if QT_DEPRECATED_SINCE(6, 9)
392 QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
393 QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0);
394#endif
395#if QT_CORE_REMOVED_SINCE(6, 7)
396 QDateTime(QDate date, QTime time, const QTimeZone &timeZone);
397 QDateTime(QDate date, QTime time);
398#endif
399 QDateTime(QDate date, QTime time, const QTimeZone &timeZone,
400 TransitionResolution resolve = TransitionResolution::LegacyBehavior);
401 QDateTime(QDate date, QTime time,
402 TransitionResolution resolve = TransitionResolution::LegacyBehavior);
403 QDateTime(const QDateTime &other) noexcept;
404 QDateTime(QDateTime &&other) noexcept;
405 ~QDateTime();
406
407 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDateTime)
408 QDateTime &operator=(const QDateTime &other) noexcept;
409
410 void swap(QDateTime &other) noexcept { d.swap(other.d); }
411
412 bool isNull() const;
413 bool isValid() const;
414
415 QDate date() const;
416 QTime time() const;
417 Qt::TimeSpec timeSpec() const;
418 int offsetFromUtc() const;
419 QTimeZone timeRepresentation() const;
420#if QT_CONFIG(timezone)
421 QTimeZone timeZone() const;
422#endif // timezone
423 QString timeZoneAbbreviation() const;
424 bool isDaylightTime() const;
425
426 qint64 toMSecsSinceEpoch() const;
427 qint64 toSecsSinceEpoch() const;
428
429#if QT_CORE_REMOVED_SINCE(6, 7)
430 void setDate(QDate date);
431 void setTime(QTime time);
432#endif
433 void setDate(QDate date, TransitionResolution resolve = TransitionResolution::LegacyBehavior);
434 void setTime(QTime time, TransitionResolution resolve = TransitionResolution::LegacyBehavior);
435
436#if QT_DEPRECATED_SINCE(6, 9)
437 QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
438 void setTimeSpec(Qt::TimeSpec spec);
439 QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
440 void setOffsetFromUtc(int offsetSeconds);
441#endif
442#if QT_CORE_REMOVED_SINCE(6, 7)
443 void setTimeZone(const QTimeZone &toZone);
444#endif
445 void setTimeZone(const QTimeZone &toZone,
446 TransitionResolution resolve = TransitionResolution::LegacyBehavior);
447 void setMSecsSinceEpoch(qint64 msecs);
448 void setSecsSinceEpoch(qint64 secs);
449
450#if QT_CONFIG(datestring)
451 QString toString(Qt::DateFormat format = Qt::TextDate) const;
452 QString toString(const QString &format) const;
453 QString toString(const QString &format, QCalendar cal) const
454 { return toString(qToStringViewIgnoringNull(format), cal); }
455 QString toString(QStringView format) const;
456 QString toString(QStringView format, QCalendar cal) const;
457#endif
458 [[nodiscard]] QDateTime addDays(qint64 days) const;
459 [[nodiscard]] QDateTime addMonths(int months) const;
460 [[nodiscard]] QDateTime addYears(int years) const;
461 [[nodiscard]] QDateTime addSecs(qint64 secs) const;
462 [[nodiscard]] QDateTime addMSecs(qint64 msecs) const;
463 [[nodiscard]] QDateTime addDuration(std::chrono::milliseconds msecs) const
464 {
465 return addMSecs(msecs.count());
466 }
467
468#if QT_DEPRECATED_SINCE(6, 9)
469 QT_DEPRECATED_VERSION_X_6_9("Use toTimeZone instead")
470 QDateTime toTimeSpec(Qt::TimeSpec spec) const;
471#endif
472 QDateTime toLocalTime() const;
473 QDateTime toUTC() const;
474 QDateTime toOffsetFromUtc(int offsetSeconds) const;
475 QDateTime toTimeZone(const QTimeZone &toZone) const;
476
477 qint64 daysTo(const QDateTime &) const;
478 qint64 secsTo(const QDateTime &) const;
479 qint64 msecsTo(const QDateTime &) const;
480
481 static QDateTime currentDateTime(const QTimeZone &zone);
482 static QDateTime currentDateTime();
483 static QDateTime currentDateTimeUtc();
484#if QT_CONFIG(datestring)
485 // No DateFormat accepts a two-digit year, so no need for baseYear:
486 static QDateTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
487 static QDateTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
488 { return fromString(qToStringViewIgnoringNull(string), format); }
489
490 // Accept calendar without over-ride of base year:
491 static QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
492 { return fromString(string.toString(), format, QLocale::DefaultTwoDigitBaseYear, cal); }
493 QT_CORE_INLINE_SINCE(6, 7)
494 static QDateTime fromString(const QString &string, QStringView format, QCalendar cal);
495 static QDateTime fromString(const QString &string, const QString &format, QCalendar cal)
496 { return fromString(string, qToStringViewIgnoringNull(format), QLocale::DefaultTwoDigitBaseYear, cal); }
497
498 // Overriding base year is likely more common than overriding calendar (and
499 // likely to get more so, as the legacy base drops ever further behind us).
500 static QDateTime fromString(QStringView string, QStringView format,
501 int baseYear = QLocale::DefaultTwoDigitBaseYear)
502 { return fromString(string.toString(), format, baseYear); }
503 static QDateTime fromString(QStringView string, QStringView format,
504 int baseYear, QCalendar cal)
505 { return fromString(string.toString(), format, baseYear, cal); }
506 static QDateTime fromString(const QString &string, QStringView format,
507 int baseYear = QLocale::DefaultTwoDigitBaseYear);
508 static QDateTime fromString(const QString &string, QStringView format,
509 int baseYear, QCalendar cal);
510 static QDateTime fromString(const QString &string, const QString &format,
511 int baseYear = QLocale::DefaultTwoDigitBaseYear)
512 { return fromString(string, qToStringViewIgnoringNull(format), baseYear); }
513 static QDateTime fromString(const QString &string, const QString &format,
514 int baseYear, QCalendar cal)
515 { return fromString(string, qToStringViewIgnoringNull(format), baseYear, cal); }
516#endif
517
518#if QT_DEPRECATED_SINCE(6, 9)
519 QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
520 static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
521 QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
522 static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetFromUtc = 0);
523#endif
524
525 static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
526 static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);
527 static QDateTime fromMSecsSinceEpoch(qint64 msecs);
528 static QDateTime fromSecsSinceEpoch(qint64 secs);
529
530 static qint64 currentMSecsSinceEpoch() noexcept;
531 static qint64 currentSecsSinceEpoch() noexcept;
532
533#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
534 static QDateTime fromCFDate(CFDateRef date);
535 CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED;
536 static QDateTime fromNSDate(const NSDate *date);
537 NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED;
538#endif
539
540 static QDateTime fromStdTimePoint(
541 std::chrono::time_point<
542 std::chrono::system_clock,
543 std::chrono::milliseconds
544 > time
545 );
546
547// INTEGRITY incident-85878 (timezone and clock_cast are not supported)
548#if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
549#if __cpp_concepts >= 201907L || defined(Q_QDOC)
550private:
551 // The duration type of the result of a clock_cast<system_clock>.
552 // This duration may differ from the duration of the input.
553 template <typename Clock, typename Duration>
554 using system_clock_cast_duration = decltype(
555 std::chrono::clock_cast<std::chrono::system_clock>(
556 std::declval<const std::chrono::time_point<Clock, Duration> &>()
557 ).time_since_epoch()
558 );
559
560public:
561 // Generic clock, as long as it's compatible with us (= system_clock)
562 template <typename Clock, typename Duration>
563 static QDateTime fromStdTimePoint(const std::chrono::time_point<Clock, Duration> &time)
564 requires
565 requires(const std::chrono::time_point<Clock, Duration> &t) {
566 // the clock can be converted to system_clock
567 std::chrono::clock_cast<std::chrono::system_clock>(t);
568 // after the conversion to system_clock, the duration type
569 // we get is convertible to milliseconds
570 requires std::is_convertible_v<
571 system_clock_cast_duration<Clock, Duration>,
572 std::chrono::milliseconds
573 >;
574 }
575 {
576 using namespace std::chrono;
577 const sys_time<milliseconds> sysTime = clock_cast<system_clock>(time);
578 return fromStdTimePoint(sysTime);
579 }
580#endif // __cpp_concepts
581
582 // local_time
583 QT_POST_CXX17_API_IN_EXPORTED_CLASS
584 static QDateTime fromStdTimePoint(const std::chrono::local_time<std::chrono::milliseconds> &time)
585 {
586 return fromStdLocalTime(time);
587 }
588
589 QT_POST_CXX17_API_IN_EXPORTED_CLASS
590 static QDateTime fromStdLocalTime(const std::chrono::local_time<std::chrono::milliseconds> &time)
591 {
592 QDateTime result(QDate(1970, 1, 1), QTime(0, 0, 0), TransitionResolution::LegacyBehavior);
593 return result.addMSecs(time.time_since_epoch().count());
594 }
595
596#if QT_CONFIG(timezone) && (__cpp_lib_chrono >= 201907L || defined(Q_QDOC))
597 // zoned_time. defined in qtimezone.h
598 QT_POST_CXX17_API_IN_EXPORTED_CLASS
599 static QDateTime fromStdZonedTime(const std::chrono::zoned_time<
600 std::chrono::milliseconds,
601 const std::chrono::time_zone *
602 > &time);
603#endif // QT_CONFIG(timezone)
604
605 QT_POST_CXX17_API_IN_EXPORTED_CLASS
606 std::chrono::sys_time<std::chrono::milliseconds> toStdSysMilliseconds() const
607 {
608 const std::chrono::milliseconds duration(toMSecsSinceEpoch());
609 return std::chrono::sys_time<std::chrono::milliseconds>(duration);
610 }
611
612 QT_POST_CXX17_API_IN_EXPORTED_CLASS
613 std::chrono::sys_seconds toStdSysSeconds() const
614 {
615 const std::chrono::seconds duration(toSecsSinceEpoch());
616 return std::chrono::sys_seconds(duration);
617 }
618#endif // __cpp_lib_chrono >= 201907L && !Q_OS_INTEGRITY
619
620 friend std::chrono::milliseconds operator-(const QDateTime &lhs, const QDateTime &rhs)
621 {
622 return std::chrono::milliseconds(rhs.msecsTo(lhs));
623 }
624
625 friend QDateTime operator+(const QDateTime &dateTime, std::chrono::milliseconds duration)
626 {
627 return dateTime.addMSecs(duration.count());
628 }
629
630 friend QDateTime operator+(std::chrono::milliseconds duration, const QDateTime &dateTime)
631 {
632 return dateTime + duration;
633 }
634
635 QDateTime &operator+=(std::chrono::milliseconds duration)
636 {
637 *this = addMSecs(duration.count());
638 return *this;
639 }
640
641 friend QDateTime operator-(const QDateTime &dateTime, std::chrono::milliseconds duration)
642 {
643 return dateTime.addMSecs(-duration.count());
644 }
645
646 QDateTime &operator-=(std::chrono::milliseconds duration)
647 {
648 *this = addMSecs(-duration.count());
649 return *this;
650 }
651
652 // (1<<63) ms is 292277024.6 (average Gregorian) years, counted from the start of 1970, so
653 // Last is floor(1970 + 292277024.6); no year 0, so First is floor(1970 - 1 - 292277024.6)
654 enum class YearRange : qint32 { First = -292275056, Last = +292278994 };
655
656private:
657 bool equals(const QDateTime &other) const;
658#if QT_CORE_REMOVED_SINCE(6, 7)
659 bool precedes(const QDateTime &other) const;
660#endif
661 friend class QDateTimePrivate;
662
663 Data d;
664
665 friend bool comparesEqual(const QDateTime &lhs, const QDateTime &rhs)
666 { return lhs.equals(rhs); }
667 friend Q_CORE_EXPORT Qt::weak_ordering
668 compareThreeWay(const QDateTime &lhs, const QDateTime &rhs);
669 Q_DECLARE_WEAKLY_ORDERED_NON_NOEXCEPT(QDateTime)
670
671#ifndef QT_NO_DATASTREAM
672 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
673 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
674#endif
675
676#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
677 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
678#endif
679};
682
683#ifndef QT_NO_DATASTREAM
684Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate);
685Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
686Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime);
687Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
688Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
689Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
690#endif // QT_NO_DATASTREAM
691
692#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
693Q_CORE_EXPORT QDebug operator<<(QDebug, QDate);
694Q_CORE_EXPORT QDebug operator<<(QDebug, QTime);
695Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
696#endif
697
698// QDateTime is not noexcept for now -- to be revised once
699// timezone and calendaring support is added
700Q_CORE_EXPORT size_t qHash(const QDateTime &key, size_t seed = 0);
701Q_CORE_EXPORT size_t qHash(QDate key, size_t seed = 0) noexcept;
702Q_CORE_EXPORT size_t qHash(QTime key, size_t seed = 0) noexcept;
703
704#if QT_CONFIG(datestring) && QT_CORE_INLINE_IMPL_SINCE(6, 7)
705QDate QDate::fromString(const QString &string, QStringView format, QCalendar cal)
706{
707 return fromString(string, format, QLocale::DefaultTwoDigitBaseYear, cal);
708}
709
710QDateTime QDateTime::fromString(const QString &string, QStringView format, QCalendar cal)
711{
712 return fromString(string, format, QLocale::DefaultTwoDigitBaseYear, cal);
713}
714#endif
715
716QT_END_NAMESPACE
717
718#endif // QDATETIME_H
The QApplication class manages the GUI application's control flow and main settings.
\inmodule QtCore\reentrant
Definition qdatastream.h:50
\inmodule QtCore\reentrant
Definition qdatetime.h:319
\inmodule QtCore \reentrant
Definition qdatetime.h:30
The QGroupBox widget provides a group box frame with a title.
Definition qgroupbox.h:18
The QMessageBox class provides a modal dialog for informing the user or for asking the user a questio...
Definition qmessagebox.h:23
The QPaintEngineState class provides information about the active paint engine's current state....
\inmodule QtGui
virtual QPixmap createPixmap(QSize size)
friend class QPainter
void setSystemRect(const QRect &rect)
void setActive(bool newState)
Sets the active state of the paint engine to state.
virtual void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
This is an overloaded member function, provided for convenience. It differs from the above function o...
virtual QPixmap createPixmapFromImage(QImage image, Qt::ImageConversionFlags flags=Qt::AutoColor)
friend class QWidgetPrivate
QRect systemRect() const
QPaintEngine(PaintEngineFeatures features=PaintEngineFeatures())
Creates a paint engine with the featureset specified by caps.
friend class QFontEngineQPF2
friend class QMacCGContext
virtual void drawTextItem(const QPointF &p, const QTextItem &textItem)
This function draws the text item textItem at position p.
virtual void drawLines(const QLineF *lines, int lineCount)
The default implementation splits the list of lines in lines into lineCount separate calls to drawPat...
PolygonDrawMode
\value OddEvenMode The polygon should be drawn using OddEven fill rule.
virtual void drawPoints(const QPointF *points, int pointCount)
Draws the first pointCount points in the buffer points.
virtual void drawRects(const QRectF *rects, int rectCount)
Draws the first rectCount rectangles in the buffer rects.
virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Reimplement this function to draw the part of the image specified by the sr rectangle in the given re...
QPainter * painter() const
Returns the paint engine's painter.
void fix_neg_rect(int *x, int *y, int *w, int *h)
void setPaintDevice(QPaintDevice *device)
virtual QPoint coordinateOffset() const
virtual void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
Reimplement this virtual function to draw the polygon defined by the pointCount first points in point...
void setDirty(DirtyFlags df)
void setSystemClip(const QRegion &baseClip)
QPaintEngineState * state
friend class QPainterReplayer
friend class QX11GLPlatformPixmap
PaintEngineFeatures gccaps
void clearDirty(DirtyFlags df)
bool hasFeature(PaintEngineFeatures feature) const
Returns true if the paint engine supports the specified feature; otherwise returns false.
virtual void drawEllipse(const QRectF &r)
Reimplement this function to draw the largest ellipse that can be contained within rectangle rect.
QRegion systemClip() const
bool isExtended() const
QPaintDevice * paintDevice() const
Returns the device that this engine is painting on, if painting is active; otherwise returns \nullptr...
bool testDirty(DirtyFlags df)
virtual void updateState(const QPaintEngineState &state)=0
Reimplement this function to update the state of a paint engine.
DirtyFlag
\value DirtyPen The pen is dirty and needs to be updated.
Type
\value X11 \value Windows \value MacPrinter \value CoreGraphics \macos's Quartz2D (CoreGraphics) \val...
virtual Type type() const =0
Reimplement this function to return the paint engine \l{Type}.
void drawPath(const QPainterPath &path)
Draws the given painter path using the current pen for outline and the current brush for filling.
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false.
void drawRects(const QRectF *rects, int rectCount)
Draws the first rectCount of the given rectangles using the current pen and brush.
friend class QFontEngine
Definition qpainter.h:433
void drawLines(const QLineF *lines, int lineCount)
Draws the first lineCount lines in the array lines using the current pen.
friend class QFontEngineMac
Definition qpainter.h:436
friend class QWin32PaintEngine
Definition qpainter.h:441
friend class QWin32PaintEnginePrivate
Definition qpainter.h:442
friend class QWidget
Definition qpainter.h:432
friend class QFontEngineWin
Definition qpainter.h:437
void drawEllipse(const QRectF &r)
Draws the ellipse defined by the given rectangle.
void drawPoints(const QPointF *points, int pointCount)
Draws the first pointCount points in the array points using the current pen's color.
bool isActive() const
Returns true if begin() has been called and end() has not yet been called; otherwise returns false.
\inmodule QtGui
\inmodule QtCore\reentrant
Definition qpoint.h:232
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:97
Q_DECLARE_FLAGS(InitFlags, InitFlag)
The QRadioButton widget provides a radio button with a text label.
The QStyleHintReturnMask class provides style hints that return a QRegion.
The QStyleHintReturnVariant class provides style hints that return a QVariant.
\variable QStyleOptionGraphicsItem::exposedRect
\variable QStyleOptionHeaderV2::textElideMode
\variable QStyleOptionDockWidget::title
\variable QStyleOptionMenuItem::menuItemType
\variable QStyleOptionComplex::subControls
\variable QStyleOption::palette
\variable QStyleOptionFocusRect::backgroundColor
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
\variable QStyleOptionFrame::features
The QStyleOptionHeaderV2 class is used to describe the parameters for drawing a header.
\variable QStyleOptionProgressBar::minimum
QStyleOptionMenuItemV2(const QStyleOptionMenuItemV2 &other)
Constructs a copy of the other style option.
QStyleOptionMenuItemV2()
Constructs a QStyleOptionMenuItemV2, initializing the members variables to their default values.
QStyleOptionMenuItemV2 & operator=(const QStyleOptionMenuItemV2 &)=default
Q_DECL_UNUSED_MEMBER int unused
\variable QStyleOptionMenuItemV2::mouseDown
\variable QStyleOptionToolBox::selectedPosition
\variable QStyleOptionComboBox::editable
The QStyleOption class stores the parameters used by QStyle functions.
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:30
Internal QTextItem.
\inmodule QtGui
\inmodule QtCore \reentrant
Definition qdatetime.h:251
const_iterator end() const noexcept
uint is_closing
Definition qwidget.h:82
QPalette pal
Definition qwidget.h:91
uint focus_policy
Definition qwidget.h:80
uint in_show
Definition qwidget.h:83
uint unused
Definition qwidget.h:89
uint fstrut_dirty
Definition qwidget.h:85
QRect wrect
Definition qwidget.h:93
QRect crect
Definition qwidget.h:90
uint in_destructor
Definition qwidget.h:88
uint sizehint_forced
Definition qwidget.h:81
uint in_set_window_state
Definition qwidget.h:84
Qt::WindowFlags window_flags
Definition qwidget.h:78
QFont fnt
Definition qwidget.h:92
uint window_modality
Definition qwidget.h:87
uint window_state
Definition qwidget.h:79
WId winid
Definition qwidget.h:76
uint context_menu_policy
Definition qwidget.h:86
uint widget_attributes
Definition qwidget.h:77
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QVarLengthFlatMap< QWindows11Style::Icon, QIcon, 16 > m_standardIcons
The QWindows11Style class provides a look and feel suitable for applications on Microsoft Windows 11.
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
QWindows11Style()
Constructs a QWindows11Style object.
bool eventFilter(QObject *obj, QEvent *event) override
void unpolish(QWidget *widget) override
void polish(QWidget *widget) override
void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const override
QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const override
void unpolish(QApplication *app) override
QRect subControlRect(ComplexControl control, const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const override
QWindows11Style(QWindows11StylePrivate &dd)
QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const override
void polish(QPalette &pal) override
QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *option) const override
int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const override
int styleHint(StyleHint hint, const QStyleOption *opt=nullptr, const QWidget *widget=nullptr, QStyleHintReturn *returnData=nullptr) const override
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget=nullptr) const override
QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const override
void polish(QApplication *app) override
~QWindows11Style() override
Destructor.
HBITMAP buffer(int w=0, int h=0)
static int fixedPixelMetric(QStyle::PixelMetric pm)
static HDC hdcForWidgetBackingStore(const QWidget *widget)
static QBackingStore * backingStoreForWidget(const QWidget *widget)
bool swapAlphaChannel(const QRect &rect, bool allPixels=false)
bool drawBackgroundThruNativeBuffer(QWindowsThemeData &QWindowsThemeData, qreal aditionalDevicePixelRatio, qreal correctionFactor)
static HWND winId(const QWidget *widget)
void init(bool force=false)
static bool isLineEditBaseColorSet(const QStyleOption *option, const QWidget *widget)
bool drawBackgroundDirectly(HDC dc, QWindowsThemeData &QWindowsThemeData, qreal aditionalDevicePixelRatio)
static bool isItemViewDelegateLineEdit(const QWidget *widget)
static bool useVista(bool update=false)
bool fixAlphaChannel(const QRect &rect)
void cleanup(bool force=false)
bool isTransparent(QWindowsThemeData &QWindowsThemeData)
bool hasAlphaChannel(const QRect &rect)
QRegion region(QWindowsThemeData &QWindowsThemeData)
bool drawBackground(QWindowsThemeData &QWindowsThemeData, qreal correctionFactor=1)
QFont assetFont(qreal pointSize) const
static HTHEME createTheme(int theme, const QWidget *widget)
static int pixelMetricFromSystemDp(QStyle::PixelMetric pm, const QStyleOption *option=nullptr, const QWidget *widget=nullptr)
static QString themeName(int theme)
The QWindowsVistaStyle class provides a look and feel suitable for applications on Microsoft Windows ...
QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const override
QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const override
int styleHint(StyleHint hint, const QStyleOption *opt=nullptr, const QWidget *widget=nullptr, QStyleHintReturn *returnData=nullptr) const override
SubControl hitTestComplexControl(ComplexControl control, const QStyleOptionComplex *option, const QPoint &pos, const QWidget *widget=nullptr) const override
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
void polish(QWidget *widget) override
int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const override
QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget=nullptr) const override
QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const override
\reimp
~QWindowsVistaStyle() override
Destructor.
void polish(QPalette &pal) override
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget=nullptr) const override
QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const override
QWindowsVistaStyle(QWindowsVistaStylePrivate &dd)
void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const override
void unpolish(QWidget *widget) override
WinFontIconEngine(const QString &glyph, const QFont &font)
QString string() const override
void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
Uses the given painter to paint the icon with the required mode and state into the rectangle rect.
QString key() const override
\variable QIconEngine::ScaledPixmapArgument::size
QIconEngine * clone() const override
Reimplement this method to return a clone of this icon engine.
void setScale(double scale)
Combined button and popup list for selecting options.
Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QDateTime::Data, Q_RELOCATABLE_TYPE)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
QT_REQUIRE_CONFIG(liburing)
T qstyleoption_cast(const QStyleOption *opt)
T qstyleoption_cast(QStyleOption *opt)
WINUI3Color
@ dividerStrokeDefault
@ fillControlAltSecondary
@ fillControlTertiary
@ frameColorStrongDisabled
@ textOnAccentPrimary
@ fillControlDefault
@ controlStrokeSecondary
@ frameColorStrong
@ surfaceStroke
@ fillControlDisabled
@ fillMicaAltSecondary
@ subtleHighlightColor
@ textPrimary
@ controlFillSolid
@ fillAccentSecondary
@ frameColorLight
@ subtlePressedColor
@ fillMicaAltDefault
@ textDisabled
@ fillControlSecondary
@ textSecondary
@ controlStrokeOnAccentSecondary
@ fillAccentDefault
@ textOnAccentSecondary
@ focusFrameOuterStroke
@ textOnAccentDisabled
@ fillControlAltQuarternary
@ fillControlInputActive
@ menuPanelFill
@ controlStrongFill
@ fillMicaAltTransparent
@ controlStrokePrimary
@ fillAccentDisabled
@ fillControlAltTertiary
@ fillControlAltDisabled
@ focusFrameInnerStroke
@ fillAccentTertiary
bool operator==(const ThemeMapKey &k1, const ThemeMapKey &k2)
@ UnknownAlpha
AlphaChannelType alphaType
friend size_t qHash(const ThemeMapKey &key, size_t seed=0) noexcept
ThemeMapKey(const QWindowsThemeData &data)
ThemeMapKey()=default