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 <edward.welbourne@qt.io>
This commit is contained in:
Ivan Solovev 2023-05-23 15:59:40 +02:00
parent ff04c24184
commit 5839aed9bd
3 changed files with 21 additions and 22 deletions

View File

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

View File

@ -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());
}
/*!

View File

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