diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index b2cf7e6c622..675e6fea136 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -4838,6 +4838,46 @@ QDateTime QDateTime::addDays(qint64 ndays) const return dt; } +/*! + \fn QDate &QDate::operator++(QDate &date) + \since 6.11 + + The prefix \c{++} operator, adds a day to \a date and returns a reference to + the modified date object. + + \sa addDays(), operator--() +*/ + +/*! + \fn QDate QDate::operator++(QDate &date, int) + \since 6.11 + + The postfix \c{++} operator, adds a day to \a date and returns the copy of + \a date with the previous date. + + \sa addDays(), operator--() +*/ + +/*! + \fn QDate &QDate::operator--(QDate &date) + \since 6.11 + + The prefix \c{--} operator, subtracts a day from \a date and returns a + reference to the modified date object. + + \sa addDays(), operator++() +*/ + +/*! + \fn QDate QDate::operator--(QDate &date, int) + \since 6.11 + + The postfix \c{--} operator, subtracts a day from \a date and returns a + reference to the modified date object. + + \sa addDays(), operator++() +*/ + /*! Returns a QDateTime object containing a datetime \a nmonths months later than the datetime of this object (or earlier if \a nmonths diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index f2aa6092bd3..284e98dcbbb 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -29,6 +29,8 @@ class Q_CORE_EXPORT QDate { explicit constexpr QDate(qint64 julianDay) : jd(julianDay) {} public: + using difference_type = qint64; + constexpr QDate() : jd(nullJd()) {} QDate(int y, int m, int d); QDate(int y, int m, int d, QCalendar cal); @@ -211,6 +213,32 @@ private: { return Qt::compareThreeWay(lhs.jd, rhs.jd); } Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QDate) + friend inline QDate &operator++(QDate &date) noexcept + { + date = date.addDays(1); + return date; + } + + friend inline QDate &operator--(QDate &date) noexcept + { + date = date.addDays(-1); + return date; + } + + friend inline QDate operator++(QDate &date, int) noexcept + { + QDate old = date; + ++date; + return old; + } + + friend inline QDate operator--(QDate &date, int) noexcept + { + QDate old = date; + --date; + return old; + } + #ifndef QT_NO_DATASTREAM friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QDate); friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &); diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 814c95324f6..9bbf878c6c2 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -65,6 +65,8 @@ private Q_SLOTS: void julianDaysLimits(); void addDays_data(); void addDays(); + void incrementable_data() { addDays_data(); } + void incrementable(); void addMonths_data(); void addMonths(); void addYears_data(); @@ -99,6 +101,7 @@ private Q_SLOTS: #endif void roundtrip() const; void qdebug() const; + private: QDate defDate() const { return QDate(1900, 1, 1); } @@ -892,6 +895,51 @@ void tst_QDate::addDays_data() QTest::newRow( "invalid" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0; } +void tst_QDate::incrementable() +{ +#ifdef __cpp_concepts + static_assert(std::weakly_incrementable); +#endif + + QFETCH(int, year); + QFETCH(int, month); + QFETCH(int, day); + QFETCH(int, amountToAdd); + QFETCH(int, expectedYear); + QFETCH(int, expectedMonth); + QFETCH(int, expectedDay); + + const QDate dt( year, month, day); + + QDate pre = dt; + + if (amountToAdd < 0) { + for (int i = amountToAdd; i < 0; ++i) + --pre; + } else { + for (int i = 0; i < amountToAdd; ++i) + ++pre; + } + + QCOMPARE(pre.year(), expectedYear); + QCOMPARE(pre.month(), expectedMonth); + QCOMPARE(pre.day(), expectedDay); + + QDate post = dt; + + if (amountToAdd < 0) { + for (int i = amountToAdd; i < 0; ++i) + post--; + } else { + for (int i = 0; i < amountToAdd; ++i) + post++; + } + + QCOMPARE(post.year(), expectedYear); + QCOMPARE(post.month(), expectedMonth); + QCOMPARE(post.day(), expectedDay); +} + void tst_QDate::addMonths() { QFETCH( int, year );