diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 03c4c65383..8b8cd9d616 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -1284,6 +1284,28 @@ QDate QDate::addDays(qint64 ndays) const return fromJulianDay(r); } +/*! + \fn QDate QDate::addDuration(std::chrono::days ndays) const + + \since 6.4 + + Returns a QDate object containing a date \a ndays later than the + date of this object (or earlier if \a ndays is negative). + + Returns a null date if the current date is invalid or the new date is + out of range. + + \note Adding durations expressed in \c{std::chrono::months} or + \c{std::chrono::years} does not yield the same result obtained by using + addMonths() or addYears(). The former are fixed durations, calculated in + relation to the solar year; the latter use the Gregorian calendar definitions + of months/years. + + \note This function requires C++20. + + \sa addMonths(), addYears(), daysTo() +*/ + /*! Returns a QDate object containing a date \a nmonths later than the date of this object (or earlier if \a nmonths is negative). @@ -4605,6 +4627,26 @@ QDateTime QDateTime::addMSecs(qint64 msecs) const return dt; } +/*! + \fn QDateTime QDateTime::addDuration(std::chrono::milliseconds msecs) const + + \since 6.4 + + Returns a QDateTime object containing a datetime \a msecs milliseconds + later than the datetime of this object (or earlier if \a msecs is + negative). + + If this datetime is invalid, an invalid datetime will be returned. + + \note Adding durations expressed in \c{std::chrono::months} or + \c{std::chrono::years} does not yield the same result obtained by using + addMonths() or addYears(). The former are fixed durations, calculated in + relation to the solar year; the latter use the Gregorian calendar definitions + of months/years. + + \sa addMSecs(), msecsTo(), addDays(), addMonths(), addYears() +*/ + /*! Returns the number of days from this datetime to the \a other datetime. The number of days is counted as the number of times diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index cdda5443b3..5da5d507a5 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -162,6 +162,13 @@ public: void getDate(int *year, int *month, int *day) const; [[nodiscard]] QDate addDays(qint64 days) const; +#if __cpp_lib_chrono >= 201907L || defined(Q_QDOC) + QT_POST_CXX17_API_IN_EXPORTED_CLASS + [[nodiscard]] QDate addDuration(std::chrono::days days) const + { + return addDays(days.count()); + } +#endif // Gregorian-optimized: [[nodiscard]] QDate addMonths(int months) const; [[nodiscard]] QDate addYears(int years) const; @@ -387,6 +394,10 @@ public: [[nodiscard]] QDateTime addYears(int years) const; [[nodiscard]] QDateTime addSecs(qint64 secs) const; [[nodiscard]] QDateTime addMSecs(qint64 msecs) const; + [[nodiscard]] QDateTime addDuration(std::chrono::milliseconds msecs) const + { + return addMSecs(msecs.count()); + } QDateTime toTimeSpec(Qt::TimeSpec spec) const; inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); } diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 5d56eef559..25cf5d7c37 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -767,11 +767,19 @@ void tst_QDate::addDays() QFETCH( int, expectedDay ); QDate dt( year, month, day ); - dt = dt.addDays( amountToAdd ); + QDate dt2 = dt.addDays( amountToAdd ); - QCOMPARE( dt.year(), expectedYear ); - QCOMPARE( dt.month(), expectedMonth ); - QCOMPARE( dt.day(), expectedDay ); + QCOMPARE( dt2.year(), expectedYear ); + QCOMPARE( dt2.month(), expectedMonth ); + QCOMPARE( dt2.day(), expectedDay ); + +#if __cpp_lib_chrono >= 201907L + QDate dt3 = dt.addDuration( std::chrono::days( amountToAdd ) ); + + QCOMPARE( dt3.year(), expectedYear ); + QCOMPARE( dt3.month(), expectedMonth ); + QCOMPARE( dt3.day(), expectedDay ); +#endif } void tst_QDate::addDays_data() diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index 17f560b35c..334d308839 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -1524,16 +1524,21 @@ void tst_QDateTime::addMSecs() QFETCH(const qint64, nsecs); QFETCH(const QDateTime, result); - QDateTime test = dt.addMSecs(qint64(nsecs) * 1000); - if (!result.isValid()) { - QVERIFY(!test.isValid()); - } else { - QCOMPARE(test, result); - QCOMPARE(test.timeSpec(), dt.timeSpec()); - if (test.timeSpec() == Qt::OffsetFromUTC) - QCOMPARE(test.offsetFromUtc(), dt.offsetFromUtc()); - QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt); - } + const auto verify = [&](const QDateTime &test) { + if (!result.isValid()) { + QVERIFY(!test.isValid()); + } else { + QCOMPARE(test, result); + QCOMPARE(test.timeSpec(), dt.timeSpec()); + if (test.timeSpec() == Qt::OffsetFromUTC) + QCOMPARE(test.offsetFromUtc(), dt.offsetFromUtc()); + QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt); + } + }; + + verify(dt.addMSecs(qint64(nsecs) * 1000)); + verify(dt.addDuration(std::chrono::seconds(nsecs))); + verify(dt.addDuration(std::chrono::milliseconds(nsecs * 1000))); } void tst_QDateTime::toTimeSpec_data()