From 5839aed9bdf0e44398e729c8371935d9bfe8cd87 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 23 May 2023 15:59:40 +0200 Subject: [PATCH] QDateTime: use new comparison helper macros The unit-tests were already ported to the new comparison helper functions from QTestPrivate. Task-number: QTBUG-104111 Change-Id: I95fccb33433b3bbf1167545e347a271140727f23 Reviewed-by: Edward Welbourne --- src/corelib/compat/removed_api.cpp | 5 +++++ src/corelib/time/qdatetime.cpp | 25 +++++++++---------------- src/corelib/time/qdatetime.h | 13 +++++++------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 99c08f1673..f8e6fa3a51 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -692,6 +692,11 @@ void QDateTime::setTimeZone(const QTimeZone &toZone) setTimeZone(toZone, TransitionResolution::LegacyBehavior); } +bool QDateTime::precedes(const QDateTime &other) const +{ + return compareThreeWay(*this, other) < 0; +} + #include "qdatastream.h" QDataStream &QDataStream::readBytes(char *&s, uint &l) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 6b260a36ec..4fef0190b6 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -5067,26 +5067,19 @@ bool QDateTime::equals(const QDateTime &other) const \sa operator==() */ -/*! - \internal - Returns \c true if \a lhs is earlier than the \a rhs - datetime; otherwise returns \c false. - - \sa equals(), operator<() -*/ - -bool QDateTime::precedes(const QDateTime &other) const +Qt::weak_ordering compareThreeWay(const QDateTime &lhs, const QDateTime &rhs) { - if (!isValid()) - return other.isValid(); - if (!other.isValid()) - return false; + if (!lhs.isValid()) + return rhs.isValid() ? Qt::weak_ordering::less : Qt::weak_ordering::equivalent; - if (usesSameOffset(d, other.d)) - return getMSecs(d) < getMSecs(other.d); + if (!rhs.isValid()) + return Qt::weak_ordering::greater; // we know that lhs is valid here + + if (usesSameOffset(lhs.d, rhs.d)) + return Qt::compareThreeWay(getMSecs(lhs.d), getMSecs(rhs.d)); // Convert to UTC and compare - return toMSecsSinceEpoch() < other.toMSecsSinceEpoch(); + return Qt::compareThreeWay(lhs.toMSecsSinceEpoch(), rhs.toMSecsSinceEpoch()); } /*! diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index 0301a4fdde..13c6c6b8fe 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -547,17 +547,18 @@ public: private: bool equals(const QDateTime &other) const; +#if QT_CORE_REMOVED_SINCE(6, 7) bool precedes(const QDateTime &other) const; +#endif friend class QDateTimePrivate; Data d; - friend bool operator==(const QDateTime &lhs, const QDateTime &rhs) { return lhs.equals(rhs); } - friend bool operator!=(const QDateTime &lhs, const QDateTime &rhs) { return !(lhs == rhs); } - friend bool operator<(const QDateTime &lhs, const QDateTime &rhs) { return lhs.precedes(rhs); } - friend bool operator<=(const QDateTime &lhs, const QDateTime &rhs) { return !(rhs < lhs); } - friend bool operator>(const QDateTime &lhs, const QDateTime &rhs) { return rhs.precedes(lhs); } - friend bool operator>=(const QDateTime &lhs, const QDateTime &rhs) { return !(lhs < rhs); } + friend bool comparesEqual(const QDateTime &lhs, const QDateTime &rhs) + { return lhs.equals(rhs); } + friend Q_CORE_EXPORT Qt::weak_ordering + compareThreeWay(const QDateTime &lhs, const QDateTime &rhs); + Q_DECLARE_WEAKLY_ORDERED(QDateTime) #ifndef QT_NO_DATASTREAM friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);