diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-04-04 03:30:47 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-04-14 21:56:56 +0200 |
commit | 41a7546789bf95b5b00e2e331896ea696b20b723 (patch) | |
tree | 3c5a2cc02fe0b5f4847b474fbf36a14620747d49 /tests/auto | |
parent | 3141a13b2a5c3cf770e072e49d43c86f376688e5 (diff) |
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>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/time/qdate/tst_qdate.cpp | 16 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp | 25 |
2 files changed, 27 insertions, 14 deletions
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() |