QDate(Time): add a addDuration method

To complement the existing addSecs / MSecs / etc., add a function
that takes any compatible std::chrono::duration.

QTime also features similar functions, but it's also "unique" in that
it uses modular arithmetic (it wraps around in case of "overflow").
I'm not so sure that adding durations to a QTime object therefore
makes sense, and I'm not doing it in this patch.

[ChangeLog][QtCore][QDate] Added addDuration().
[ChangeLog][QtCore][QDateTime] Added addDuration().

Change-Id: I02aa37ff024d7f56fa976dc8f4f73523bdba8d94
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2022-04-04 03:30:47 +02:00
parent 3141a13b2a
commit 41a7546789
4 changed files with 80 additions and 14 deletions

View File

@ -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

View File

@ -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); }

View File

@ -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()

View File

@ -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()