Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-05 09:27:20

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // Copyright (C) 2021 Intel Corporation.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:significant reason:default
0005 
0006 #ifndef QDATETIME_H
0007 #define QDATETIME_H
0008 
0009 #include <QtCore/qcalendar.h>
0010 #include <QtCore/qcompare.h>
0011 #include <QtCore/qlocale.h>
0012 #include <QtCore/qnamespace.h>
0013 #include <QtCore/qshareddata.h>
0014 #include <QtCore/qstring.h>
0015 
0016 #include <climits>
0017 #include <chrono>
0018 
0019 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0020 Q_FORWARD_DECLARE_CF_TYPE(CFDate);
0021 Q_FORWARD_DECLARE_OBJC_CLASS(NSDate);
0022 #endif
0023 
0024 QT_BEGIN_NAMESPACE
0025 
0026 class QTimeZone;
0027 class QDateTime;
0028 
0029 class Q_CORE_EXPORT QDate
0030 {
0031     explicit constexpr QDate(qint64 julianDay) : jd(julianDay) {}
0032 public:
0033     using difference_type = qint64;
0034 
0035     constexpr QDate() : jd(nullJd()) {}
0036     QDate(int y, int m, int d);
0037     QDate(int y, int m, int d, QCalendar cal);
0038 // INTEGRITY incident-85878 (timezone and clock_cast are not supported)
0039 #if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
0040     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0041     Q_IMPLICIT constexpr QDate(std::chrono::year_month_day date) noexcept
0042         : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
0043     {}
0044 
0045     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0046     Q_IMPLICIT constexpr QDate(std::chrono::year_month_day_last date) noexcept
0047         : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
0048     {}
0049 
0050     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0051     Q_IMPLICIT constexpr QDate(std::chrono::year_month_weekday date) noexcept
0052         : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
0053     {}
0054 
0055     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0056     Q_IMPLICIT constexpr QDate(std::chrono::year_month_weekday_last date) noexcept
0057         : jd(date.ok() ? stdSysDaysToJulianDay(date) : nullJd())
0058     {}
0059 
0060     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0061     static constexpr QDate fromStdSysDays(const std::chrono::sys_days &days) noexcept
0062     {
0063         return QDate(stdSysDaysToJulianDay(days));
0064     }
0065 
0066     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0067     constexpr std::chrono::sys_days toStdSysDays() const noexcept
0068     {
0069         const qint64 days = isValid() ? jd - unixEpochJd() : 0;
0070         return std::chrono::sys_days(std::chrono::days(days));
0071     }
0072 #endif
0073 
0074     constexpr bool isNull() const { return !isValid(); }
0075     constexpr bool isValid() const { return jd >= minJd() && jd <= maxJd(); }
0076 
0077     // Gregorian-optimized:
0078     int year() const;
0079     int month() const;
0080     int day() const;
0081     int dayOfWeek() const;
0082     int dayOfYear() const;
0083     int daysInMonth() const;
0084     int daysInYear() const;
0085     int weekNumber(int *yearNum = nullptr) const; // ISO 8601, always Gregorian
0086 
0087     int year(QCalendar cal) const;
0088     int month(QCalendar cal) const;
0089     int day(QCalendar cal) const;
0090     int dayOfWeek(QCalendar cal) const;
0091     int dayOfYear(QCalendar cal) const;
0092     int daysInMonth(QCalendar cal) const;
0093     int daysInYear(QCalendar cal) const;
0094 
0095 #if QT_DEPRECATED_SINCE(6, 9)
0096     QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
0097     QDateTime startOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
0098     QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
0099     QDateTime endOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
0100 #endif
0101 
0102     QDateTime startOfDay(const QTimeZone &zone) const;
0103     QDateTime endOfDay(const QTimeZone &zone) const;
0104     QDateTime startOfDay() const;
0105     QDateTime endOfDay() const;
0106 
0107 #if QT_CONFIG(datestring)
0108     QString toString(Qt::DateFormat format = Qt::TextDate) const;
0109     QString toString(const QString &format) const;
0110     QString toString(const QString &format, QCalendar cal) const
0111     { return toString(qToStringViewIgnoringNull(format), cal); }
0112     QString toString(QStringView format) const;
0113     QString toString(QStringView format, QCalendar cal) const;
0114 #endif
0115     bool setDate(int year, int month, int day); // Gregorian-optimized
0116     bool setDate(int year, int month, int day, QCalendar cal);
0117 
0118     void getDate(int *year, int *month, int *day) const;
0119 
0120     [[nodiscard]] QDate addDays(qint64 days) const;
0121 // INTEGRITY incident-85878 (timezone and clock_cast are not supported)
0122 #if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
0123     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0124     [[nodiscard]] QDate addDuration(std::chrono::days days) const
0125     {
0126         return addDays(days.count());
0127     }
0128 #endif
0129     // Gregorian-optimized:
0130     [[nodiscard]] QDate addMonths(int months) const;
0131     [[nodiscard]] QDate addYears(int years) const;
0132     [[nodiscard]] QDate addMonths(int months, QCalendar cal) const;
0133     [[nodiscard]] QDate addYears(int years, QCalendar cal) const;
0134     qint64 daysTo(QDate d) const;
0135 
0136     static QDate currentDate();
0137 #if QT_CONFIG(datestring)
0138     // No DateFormat accepts a two-digit year, so no need for baseYear:
0139     static QDate fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
0140     static QDate fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
0141     { return fromString(qToStringViewIgnoringNull(string), format); }
0142 
0143     // Accept calendar without over-ride of base year:
0144     static QDate fromString(QStringView string, QStringView format, QCalendar cal)
0145     { return fromString(string.toString(), format, QLocale::DefaultTwoDigitBaseYear, cal); }
0146     QT_CORE_INLINE_SINCE(6, 7)
0147     static QDate fromString(const QString &string, QStringView format, QCalendar cal);
0148     static QDate fromString(const QString &string, const QString &format, QCalendar cal)
0149     { return fromString(string, qToStringViewIgnoringNull(format), QLocale::DefaultTwoDigitBaseYear, cal); }
0150 
0151     // Overriding base year is likely more common than overriding calendar (and
0152     // likely to get more so, as the legacy base drops ever further behind us).
0153     static QDate fromString(QStringView string, QStringView format,
0154                             int baseYear = QLocale::DefaultTwoDigitBaseYear)
0155     { return fromString(string.toString(), format, baseYear); }
0156     static QDate fromString(QStringView string, QStringView format,
0157                             int baseYear, QCalendar cal)
0158     { return fromString(string.toString(), format, baseYear, cal); }
0159     static QDate fromString(const QString &string, QStringView format,
0160                             int baseYear = QLocale::DefaultTwoDigitBaseYear);
0161     static QDate fromString(const QString &string, QStringView format,
0162                             int baseYear, QCalendar cal);
0163     static QDate fromString(const QString &string, const QString &format,
0164                             int baseYear = QLocale::DefaultTwoDigitBaseYear)
0165     { return fromString(string, qToStringViewIgnoringNull(format), baseYear); }
0166     static QDate fromString(const QString &string, const QString &format,
0167                             int baseYear, QCalendar cal)
0168     { return fromString(string, qToStringViewIgnoringNull(format), baseYear, cal); }
0169 #endif
0170     static bool isValid(int y, int m, int d);
0171     static bool isLeapYear(int year);
0172 
0173     static constexpr inline QDate fromJulianDay(qint64 jd_)
0174     { return jd_ >= minJd() && jd_ <= maxJd() ? QDate(jd_) : QDate() ; }
0175     constexpr inline qint64 toJulianDay() const { return jd; }
0176 
0177 private:
0178     // using extra parentheses around min to avoid expanding it if it is a macro
0179     static constexpr inline qint64 nullJd() { return (std::numeric_limits<qint64>::min)(); }
0180     static constexpr inline qint64 minJd() { return Q_INT64_C(-784350574879); }
0181     static constexpr inline qint64 maxJd() { return Q_INT64_C( 784354017364); }
0182     static constexpr inline qint64 unixEpochJd() { return Q_INT64_C(2440588); }
0183 
0184 // INTEGRITY incident-85878 (timezone and clock_cast are not supported)
0185 #if __cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)
0186 #if !QT_CORE_REMOVED_SINCE(6, 7)
0187     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0188 #endif
0189     static constexpr qint64
0190     stdSysDaysToJulianDay(const std::chrono::sys_days &days) noexcept
0191     {
0192         const auto epochDays = days.time_since_epoch().count();
0193         // minJd() and maxJd() fit into 40 bits.
0194         if constexpr (sizeof(epochDays) * CHAR_BIT >= 41) {
0195             constexpr auto top = maxJd() - unixEpochJd();
0196             constexpr auto bottom = minJd() - unixEpochJd();
0197             if (epochDays > top || epochDays < bottom)
0198                 return nullJd();
0199         }
0200         return unixEpochJd() + epochDays;
0201     }
0202 #endif // __cpp_lib_chrono >= 201907L
0203 
0204     qint64 jd;
0205 
0206     friend class QDateTime;
0207     friend class QDateTimeParser;
0208     friend class QDateTimePrivate;
0209 
0210     friend constexpr bool comparesEqual(const QDate &lhs, const QDate &rhs) noexcept
0211     { return lhs.jd == rhs.jd; }
0212     friend constexpr Qt::strong_ordering
0213     compareThreeWay(const QDate &lhs, const QDate &rhs) noexcept
0214     { return Qt::compareThreeWay(lhs.jd, rhs.jd); }
0215     Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QDate)
0216 
0217     friend QDate &operator++(QDate &date)
0218     {
0219         date = date.addDays(1);
0220         return date;
0221     }
0222 
0223     friend QDate &operator--(QDate &date)
0224     {
0225         date = date.addDays(-1);
0226         return date;
0227     }
0228 
0229     friend QDate operator++(QDate &date, int)
0230     {
0231         QDate old = date;
0232         ++date;
0233         return old;
0234     }
0235 
0236     friend QDate operator--(QDate &date, int)
0237     {
0238         QDate old = date;
0239         --date;
0240         return old;
0241     }
0242 
0243 #ifndef QT_NO_DATASTREAM
0244     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate);
0245     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
0246 #endif
0247 };
0248 Q_DECLARE_TYPEINFO(QDate, Q_RELOCATABLE_TYPE);
0249 
0250 class Q_CORE_EXPORT QTime
0251 {
0252     explicit constexpr QTime(int ms) : mds(ms)
0253     {}
0254 public:
0255     constexpr QTime(): mds(NullTime)
0256     {}
0257     QTime(int h, int m, int s = 0, int ms = 0);
0258 
0259     constexpr bool isNull() const { return mds == NullTime; }
0260     bool isValid() const;
0261 
0262     int hour() const;
0263     int minute() const;
0264     int second() const;
0265     int msec() const;
0266 #if QT_CONFIG(datestring)
0267     QString toString(Qt::DateFormat f = Qt::TextDate) const;
0268     QString toString(const QString &format) const
0269     { return toString(qToStringViewIgnoringNull(format)); }
0270     QString toString(QStringView format) const;
0271 #endif
0272     bool setHMS(int h, int m, int s, int ms = 0);
0273 
0274     [[nodiscard]] QTime addSecs(int secs) const;
0275     int secsTo(QTime t) const;
0276     [[nodiscard]] QTime addMSecs(int ms) const;
0277     int msecsTo(QTime t) const;
0278 
0279     static constexpr inline QTime fromMSecsSinceStartOfDay(int msecs) { return QTime(msecs); }
0280     constexpr inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; }
0281 
0282     static QTime currentTime();
0283 #if QT_CONFIG(datestring)
0284     static QTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
0285     static QTime fromString(QStringView string, QStringView format)
0286     { return fromString(string.toString(), format); }
0287     static QTime fromString(const QString &string, QStringView format);
0288     static QTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
0289     { return fromString(qToStringViewIgnoringNull(string), format); }
0290     static QTime fromString(const QString &string, const QString &format)
0291     { return fromString(string, qToStringViewIgnoringNull(format)); }
0292 #endif
0293     static bool isValid(int h, int m, int s, int ms = 0);
0294 
0295 private:
0296     enum TimeFlag { NullTime = -1 };
0297     constexpr inline int ds() const { return mds == -1 ? 0 : mds; }
0298     int mds;
0299 
0300     friend constexpr bool comparesEqual(const QTime &lhs, const QTime &rhs) noexcept
0301     { return lhs.mds == rhs.mds; }
0302     friend constexpr Qt::strong_ordering
0303     compareThreeWay(const QTime &lhs, const QTime &rhs) noexcept
0304     { return Qt::compareThreeWay(lhs.mds, rhs.mds); }
0305     Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QTime)
0306 
0307     friend class QDateTime;
0308     friend class QDateTimePrivate;
0309 #ifndef QT_NO_DATASTREAM
0310     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime);
0311     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
0312 #endif
0313 };
0314 Q_DECLARE_TYPEINFO(QTime, Q_RELOCATABLE_TYPE);
0315 
0316 class QDateTimePrivate;
0317 
0318 class Q_CORE_EXPORT QDateTime
0319 {
0320     struct ShortData {
0321 #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || defined(QT_BOOTSTRAPPED)
0322 #  if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
0323         qint64 status : 8;
0324 #  endif
0325         qint64 msecs : 56;
0326 
0327 #  if Q_BYTE_ORDER == Q_BIG_ENDIAN
0328         qint64 status : 8;
0329 #  endif
0330 #else
0331 #  if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
0332         quintptr status : 8;
0333 #  endif
0334         // note: this is only 24 bits on 32-bit systems...
0335         qintptr msecs : sizeof(void *) * 8 - 8;
0336 
0337 #  if Q_BYTE_ORDER == Q_BIG_ENDIAN
0338         quintptr status : 8;
0339 #  endif
0340 #endif
0341         friend constexpr bool operator==(ShortData lhs, ShortData rhs)
0342         { return lhs.status == rhs.status && lhs.msecs == rhs.msecs; }
0343     };
0344 
0345     union Data {
0346         // To be of any use, we need at least 60 years around 1970, which
0347         // is 1,893,456,000,000 ms. That requires 41 bits to store, plus
0348         // the sign bit. With the status byte, the minimum size is 50 bits.
0349         static constexpr bool CanBeSmall = sizeof(ShortData) * 8 > 50;
0350 
0351         Data() noexcept;
0352         Data(const QTimeZone &);
0353         Data(const Data &other) noexcept;
0354         Data(Data &&other) noexcept;
0355         Data &operator=(const Data &other) noexcept;
0356         Data &operator=(Data &&other) noexcept { swap(other); return *this; }
0357         ~Data();
0358 
0359         void swap(Data &other) noexcept
0360         { std::swap(data, other.data); }
0361 
0362         bool isShort() const;
0363         inline void invalidate();
0364         void detach();
0365         QTimeZone timeZone() const;
0366 
0367         const QDateTimePrivate *operator->() const;
0368         QDateTimePrivate *operator->();
0369 
0370         QDateTimePrivate *d;
0371         ShortData data;
0372     };
0373 
0374 public:
0375     QDateTime() noexcept;
0376 
0377     enum class TransitionResolution {
0378         Reject = 0,
0379         RelativeToBefore,
0380         RelativeToAfter,
0381         PreferBefore,
0382         PreferAfter,
0383         PreferStandard,
0384         PreferDaylightSaving,
0385         // Closest match to behavior prior to introducing TransitionResolution:
0386         LegacyBehavior = RelativeToBefore
0387     };
0388 
0389 #if QT_DEPRECATED_SINCE(6, 9)
0390     QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
0391     QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0);
0392 #endif
0393 #if QT_CORE_REMOVED_SINCE(6, 7)
0394     QDateTime(QDate date, QTime time, const QTimeZone &timeZone);
0395     QDateTime(QDate date, QTime time);
0396 #endif
0397     QDateTime(QDate date, QTime time, const QTimeZone &timeZone,
0398               TransitionResolution resolve = TransitionResolution::LegacyBehavior);
0399     QDateTime(QDate date, QTime time,
0400               TransitionResolution resolve = TransitionResolution::LegacyBehavior);
0401     QDateTime(const QDateTime &other) noexcept;
0402     QDateTime(QDateTime &&other) noexcept;
0403     ~QDateTime();
0404 
0405     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QDateTime)
0406     QDateTime &operator=(const QDateTime &other) noexcept;
0407 
0408     void swap(QDateTime &other) noexcept { d.swap(other.d); }
0409 
0410     bool isNull() const;
0411     bool isValid() const;
0412 
0413     QDate date() const;
0414     QTime time() const;
0415     Qt::TimeSpec timeSpec() const;
0416     int offsetFromUtc() const;
0417     QTimeZone timeRepresentation() const;
0418 #if QT_CONFIG(timezone)
0419     QTimeZone timeZone() const;
0420 #endif // timezone
0421     QString timeZoneAbbreviation() const;
0422     bool isDaylightTime() const;
0423 
0424     qint64 toMSecsSinceEpoch() const;
0425     qint64 toSecsSinceEpoch() const;
0426 
0427 #if QT_CORE_REMOVED_SINCE(6, 7)
0428     void setDate(QDate date);
0429     void setTime(QTime time);
0430 #endif
0431     void setDate(QDate date, TransitionResolution resolve = TransitionResolution::LegacyBehavior);
0432     void setTime(QTime time, TransitionResolution resolve = TransitionResolution::LegacyBehavior);
0433 
0434 #if QT_DEPRECATED_SINCE(6, 9)
0435     QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
0436     void setTimeSpec(Qt::TimeSpec spec);
0437     QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
0438     void setOffsetFromUtc(int offsetSeconds);
0439 #endif
0440 #if QT_CORE_REMOVED_SINCE(6, 7)
0441     void setTimeZone(const QTimeZone &toZone);
0442 #endif
0443     void setTimeZone(const QTimeZone &toZone,
0444                      TransitionResolution resolve = TransitionResolution::LegacyBehavior);
0445     void setMSecsSinceEpoch(qint64 msecs);
0446     void setSecsSinceEpoch(qint64 secs);
0447 
0448 #if QT_CONFIG(datestring)
0449     QString toString(Qt::DateFormat format = Qt::TextDate) const;
0450     QString toString(const QString &format) const;
0451     QString toString(const QString &format, QCalendar cal) const
0452     { return toString(qToStringViewIgnoringNull(format), cal); }
0453     QString toString(QStringView format) const;
0454     QString toString(QStringView format, QCalendar cal) const;
0455 #endif
0456     [[nodiscard]] QDateTime addDays(qint64 days) const;
0457     [[nodiscard]] QDateTime addMonths(int months) const;
0458     [[nodiscard]] QDateTime addYears(int years) const;
0459     [[nodiscard]] QDateTime addSecs(qint64 secs) const;
0460     [[nodiscard]] QDateTime addMSecs(qint64 msecs) const;
0461     [[nodiscard]] QDateTime addDuration(std::chrono::milliseconds msecs) const
0462     {
0463         return addMSecs(msecs.count());
0464     }
0465 
0466 #if QT_DEPRECATED_SINCE(6, 9)
0467     QT_DEPRECATED_VERSION_X_6_9("Use toTimeZone instead")
0468     QDateTime toTimeSpec(Qt::TimeSpec spec) const;
0469 #endif
0470     QDateTime toLocalTime() const;
0471     QDateTime toUTC() const;
0472     QDateTime toOffsetFromUtc(int offsetSeconds) const;
0473     QDateTime toTimeZone(const QTimeZone &toZone) const;
0474 
0475     qint64 daysTo(const QDateTime &) const;
0476     qint64 secsTo(const QDateTime &) const;
0477     qint64 msecsTo(const QDateTime &) const;
0478 
0479     static QDateTime currentDateTime(const QTimeZone &zone);
0480     static QDateTime currentDateTime();
0481     static QDateTime currentDateTimeUtc();
0482 #if QT_CONFIG(datestring)
0483     // No DateFormat accepts a two-digit year, so no need for baseYear:
0484     static QDateTime fromString(QStringView string, Qt::DateFormat format = Qt::TextDate);
0485     static QDateTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
0486     { return fromString(qToStringViewIgnoringNull(string), format); }
0487 
0488     // Accept calendar without over-ride of base year:
0489     static QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
0490     { return fromString(string.toString(), format, QLocale::DefaultTwoDigitBaseYear, cal); }
0491     QT_CORE_INLINE_SINCE(6, 7)
0492     static QDateTime fromString(const QString &string, QStringView format, QCalendar cal);
0493     static QDateTime fromString(const QString &string, const QString &format, QCalendar cal)
0494     { return fromString(string, qToStringViewIgnoringNull(format), QLocale::DefaultTwoDigitBaseYear, cal); }
0495 
0496     // Overriding base year is likely more common than overriding calendar (and
0497     // likely to get more so, as the legacy base drops ever further behind us).
0498     static QDateTime fromString(QStringView string, QStringView format,
0499                                 int baseYear = QLocale::DefaultTwoDigitBaseYear)
0500     { return fromString(string.toString(), format, baseYear); }
0501     static QDateTime fromString(QStringView string, QStringView format,
0502                                 int baseYear, QCalendar cal)
0503     { return fromString(string.toString(), format, baseYear, cal); }
0504     static QDateTime fromString(const QString &string, QStringView format,
0505                                 int baseYear = QLocale::DefaultTwoDigitBaseYear);
0506     static QDateTime fromString(const QString &string, QStringView format,
0507                                 int baseYear, QCalendar cal);
0508     static QDateTime fromString(const QString &string, const QString &format,
0509                                 int baseYear = QLocale::DefaultTwoDigitBaseYear)
0510     { return fromString(string, qToStringViewIgnoringNull(format), baseYear); }
0511     static QDateTime fromString(const QString &string, const QString &format,
0512                                 int baseYear, QCalendar cal)
0513     { return fromString(string, qToStringViewIgnoringNull(format), baseYear, cal); }
0514 #endif
0515 
0516 #if QT_DEPRECATED_SINCE(6, 9)
0517     QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
0518     static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
0519     QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
0520     static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetFromUtc = 0);
0521 #endif
0522 
0523     static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
0524     static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);
0525     static QDateTime fromMSecsSinceEpoch(qint64 msecs);
0526     static QDateTime fromSecsSinceEpoch(qint64 secs);
0527 
0528     static qint64 currentMSecsSinceEpoch() noexcept;
0529     static qint64 currentSecsSinceEpoch() noexcept;
0530 
0531 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0532     static QDateTime fromCFDate(CFDateRef date);
0533     CFDateRef toCFDate() const Q_DECL_CF_RETURNS_RETAINED;
0534     static QDateTime fromNSDate(const NSDate *date);
0535     NSDate *toNSDate() const Q_DECL_NS_RETURNS_AUTORELEASED;
0536 #endif
0537 
0538     static QDateTime fromStdTimePoint(
0539         std::chrono::time_point<
0540             std::chrono::system_clock,
0541             std::chrono::milliseconds
0542         > time
0543     );
0544 
0545 // INTEGRITY incident-85878 (timezone and clock_cast are not supported)
0546 #if (__cpp_lib_chrono >= 201907L && !defined(Q_OS_INTEGRITY)) || defined(Q_QDOC)
0547 #if __cpp_concepts >= 201907L || defined(Q_QDOC)
0548 private:
0549     // The duration type of the result of a clock_cast<system_clock>.
0550     // This duration may differ from the duration of the input.
0551     template <typename Clock, typename Duration>
0552     using system_clock_cast_duration = decltype(
0553         std::chrono::clock_cast<std::chrono::system_clock>(
0554             std::declval<const std::chrono::time_point<Clock, Duration> &>()
0555         ).time_since_epoch()
0556     );
0557 
0558 public:
0559     // Generic clock, as long as it's compatible with us (= system_clock)
0560     template <typename Clock, typename Duration>
0561     static QDateTime fromStdTimePoint(const std::chrono::time_point<Clock, Duration> &time)
0562         requires
0563             requires(const std::chrono::time_point<Clock, Duration> &t) {
0564                 // the clock can be converted to system_clock
0565                 std::chrono::clock_cast<std::chrono::system_clock>(t);
0566                 // after the conversion to system_clock, the duration type
0567                 // we get is convertible to milliseconds
0568                 requires std::is_convertible_v<
0569                     system_clock_cast_duration<Clock, Duration>,
0570                     std::chrono::milliseconds
0571                 >;
0572             }
0573     {
0574         using namespace std::chrono;
0575         const sys_time<milliseconds> sysTime = clock_cast<system_clock>(time);
0576         return fromStdTimePoint(sysTime);
0577     }
0578 #endif // __cpp_concepts
0579 
0580     // local_time
0581     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0582     static QDateTime fromStdTimePoint(const std::chrono::local_time<std::chrono::milliseconds> &time)
0583     {
0584         return fromStdLocalTime(time);
0585     }
0586 
0587     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0588     static QDateTime fromStdLocalTime(const std::chrono::local_time<std::chrono::milliseconds> &time)
0589     {
0590         QDateTime result(QDate(1970, 1, 1), QTime(0, 0, 0), TransitionResolution::LegacyBehavior);
0591         return result.addMSecs(time.time_since_epoch().count());
0592     }
0593 
0594 #if QT_CONFIG(timezone) && (__cpp_lib_chrono >= 201907L || defined(Q_QDOC))
0595     // zoned_time. defined in qtimezone.h
0596     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0597     static QDateTime fromStdZonedTime(const std::chrono::zoned_time<
0598                                           std::chrono::milliseconds,
0599                                           const std::chrono::time_zone *
0600                                       > &time);
0601 #endif // QT_CONFIG(timezone)
0602 
0603     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0604     std::chrono::sys_time<std::chrono::milliseconds> toStdSysMilliseconds() const
0605     {
0606         const std::chrono::milliseconds duration(toMSecsSinceEpoch());
0607         return std::chrono::sys_time<std::chrono::milliseconds>(duration);
0608     }
0609 
0610     QT_POST_CXX17_API_IN_EXPORTED_CLASS
0611     std::chrono::sys_seconds toStdSysSeconds() const
0612     {
0613         const std::chrono::seconds duration(toSecsSinceEpoch());
0614         return std::chrono::sys_seconds(duration);
0615     }
0616 #endif // __cpp_lib_chrono >= 201907L && !Q_OS_INTEGRITY
0617 
0618     friend std::chrono::milliseconds operator-(const QDateTime &lhs, const QDateTime &rhs)
0619     {
0620         return std::chrono::milliseconds(rhs.msecsTo(lhs));
0621     }
0622 
0623     friend QDateTime operator+(const QDateTime &dateTime, std::chrono::milliseconds duration)
0624     {
0625         return dateTime.addMSecs(duration.count());
0626     }
0627 
0628     friend QDateTime operator+(std::chrono::milliseconds duration, const QDateTime &dateTime)
0629     {
0630         return dateTime + duration;
0631     }
0632 
0633     QDateTime &operator+=(std::chrono::milliseconds duration)
0634     {
0635         *this = addMSecs(duration.count());
0636         return *this;
0637     }
0638 
0639     friend QDateTime operator-(const QDateTime &dateTime, std::chrono::milliseconds duration)
0640     {
0641         return dateTime.addMSecs(-duration.count());
0642     }
0643 
0644     QDateTime &operator-=(std::chrono::milliseconds duration)
0645     {
0646         *this = addMSecs(-duration.count());
0647         return *this;
0648     }
0649 
0650     // (1<<63) ms is 292277024.6 (average Gregorian) years, counted from the start of 1970, so
0651     // Last is floor(1970 + 292277024.6); no year 0, so First is floor(1970 - 1 - 292277024.6)
0652     enum class YearRange : qint32 { First = -292275056,  Last = +292278994 };
0653 
0654 private:
0655     bool equals(const QDateTime &other) const;
0656 #if QT_CORE_REMOVED_SINCE(6, 7)
0657     bool precedes(const QDateTime &other) const;
0658 #endif
0659     friend class QDateTimePrivate;
0660 
0661     Data d;
0662 
0663     friend bool comparesEqual(const QDateTime &lhs, const QDateTime &rhs)
0664     { return lhs.equals(rhs); }
0665     friend Q_CORE_EXPORT Qt::weak_ordering
0666     compareThreeWay(const QDateTime &lhs, const QDateTime &rhs);
0667     Q_DECLARE_WEAKLY_ORDERED_NON_NOEXCEPT(QDateTime)
0668 
0669 #ifndef QT_NO_DATASTREAM
0670     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
0671     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
0672 #endif
0673 
0674 #if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
0675     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
0676 #endif
0677 };
0678 Q_DECLARE_SHARED(QDateTime)
0679 
0680 #ifndef QT_NO_DATASTREAM
0681 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate);
0682 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
0683 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QTime);
0684 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
0685 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
0686 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
0687 #endif // QT_NO_DATASTREAM
0688 
0689 #if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
0690 Q_CORE_EXPORT QDebug operator<<(QDebug, QDate);
0691 Q_CORE_EXPORT QDebug operator<<(QDebug, QTime);
0692 Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
0693 #endif
0694 
0695 // QDateTime is not noexcept for now -- to be revised once
0696 // timezone and calendaring support is added
0697 Q_CORE_EXPORT size_t qHash(const QDateTime &key, size_t seed = 0);
0698 Q_CORE_EXPORT size_t qHash(QDate key, size_t seed = 0) noexcept;
0699 Q_CORE_EXPORT size_t qHash(QTime key, size_t seed = 0) noexcept;
0700 
0701 #if QT_CONFIG(datestring) && QT_CORE_INLINE_IMPL_SINCE(6, 7)
0702 QDate QDate::fromString(const QString &string, QStringView format, QCalendar cal)
0703 {
0704     return fromString(string, format, QLocale::DefaultTwoDigitBaseYear, cal);
0705 }
0706 
0707 QDateTime QDateTime::fromString(const QString &string, QStringView format, QCalendar cal)
0708 {
0709     return fromString(string, format, QLocale::DefaultTwoDigitBaseYear, cal);
0710 }
0711 #endif
0712 
0713 QT_END_NAMESPACE
0714 
0715 #endif // QDATETIME_H