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