summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2019-10-04 13:26:27 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2019-10-11 11:59:41 +0200
commit60deb69034e3d3ac36213ad8c36042ad57c495f7 (patch)
treef7c40ebcad857201d09c36edfb97a9242715686a /src/corelib
parent4af00753fad57989a6ae366cc3dbfc56d88508f4 (diff)
Distinguish invalid datetimes from others
A default-constructed QDateTime is invalid, but compared equal to a valid one referencing the start of 1970. This lead to date properties in QML being initialized invalid but not getting an onChange if the first value they're set to is the start of 1970. Fixing that then lead to some tests failing. Indeed, the original equality check involved using toMSecsSinceEpoch(), whose value is undefined unless the datetime is valid, without a prior check on its validity: so ensure all uses of toMSecsSinceEpoch() are guarded with isValid() checks. Reworked tst_QDateTime::toSecsSinceEpoch() to use its bool column (previously unused, after separating from toTime_t(), which uses this column for "out of time_t's range") for validity of the datetime. [ChangeLog][QtCore][QDateTime] Invalid datetimes are now treated as equal and less than all valid ones. They could previously be found equal to valid datetimes. Fixes: QTBUG-79006 Change-Id: Ie72deb8af4350a5e808144d0f6e42dc8eb3ff5ef Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/time/qdatetime.cpp40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp
index 6ed0efe77d..95a7255a04 100644
--- a/src/corelib/time/qdatetime.cpp
+++ b/src/corelib/time/qdatetime.cpp
@@ -3861,6 +3861,9 @@ int QDateTime::offsetFromUtc() const
QString QDateTime::timeZoneAbbreviation() const
{
+ if (!isValid())
+ return QString();
+
switch (getSpec(d)) {
case Qt::UTC:
return QLatin1String("UTC");
@@ -3895,6 +3898,9 @@ QString QDateTime::timeZoneAbbreviation() const
bool QDateTime::isDaylightTime() const
{
+ if (!isValid())
+ return false;
+
switch (getSpec(d)) {
case Qt::UTC:
case Qt::OffsetFromUTC:
@@ -4761,17 +4767,24 @@ QDateTime QDateTime::toTimeZone(const QTimeZone &timeZone) const
Returns \c true if this datetime is equal to the \a other datetime;
otherwise returns \c false.
+ Since 5.14, all invalid datetimes are equal to one another and differ from
+ all other datetimes.
+
\sa operator!=()
*/
bool QDateTime::operator==(const QDateTime &other) const
{
- if (getSpec(d) == Qt::LocalTime
- && getStatus(d) == getStatus(other.d)) {
+ if (!isValid())
+ return !other.isValid();
+ if (!other.isValid())
+ return false;
+
+ if (getSpec(d) == Qt::LocalTime && getStatus(d) == getStatus(other.d))
return getMSecs(d) == getMSecs(other.d);
- }
+
// Convert to UTC and compare
- return (toMSecsSinceEpoch() == other.toMSecsSinceEpoch());
+ return toMSecsSinceEpoch() == other.toMSecsSinceEpoch();
}
/*!
@@ -4780,8 +4793,9 @@ bool QDateTime::operator==(const QDateTime &other) const
Returns \c true if this datetime is different from the \a other
datetime; otherwise returns \c false.
- Two datetimes are different if either the date, the time, or the
- time zone components are different.
+ Two datetimes are different if either the date, the time, or the time zone
+ components are different. Since 5.14, any invalid datetime is less than all
+ valid datetimes.
\sa operator==()
*/
@@ -4793,12 +4807,16 @@ bool QDateTime::operator==(const QDateTime &other) const
bool QDateTime::operator<(const QDateTime &other) const
{
- if (getSpec(d) == Qt::LocalTime
- && getStatus(d) == getStatus(other.d)) {
+ if (!isValid())
+ return other.isValid();
+ if (!other.isValid())
+ return false;
+
+ if (getSpec(d) == Qt::LocalTime && getStatus(d) == getStatus(other.d))
return getMSecs(d) < getMSecs(other.d);
- }
+
// Convert to UTC and compare
- return (toMSecsSinceEpoch() < other.toMSecsSinceEpoch());
+ return toMSecsSinceEpoch() < other.toMSecsSinceEpoch();
}
/*!
@@ -5849,7 +5867,7 @@ uint qHash(const QDateTime &key, uint seed)
// QDate/QTime/spec/offset because QDateTime::operator== converts both arguments
// to the same timezone. If we don't, qHash would return different hashes for
// two QDateTimes that are equivalent once converted to the same timezone.
- return qHash(key.toMSecsSinceEpoch(), seed);
+ return key.isValid() ? qHash(key.toMSecsSinceEpoch(), seed) : seed;
}
/*! \fn uint qHash(const QDate &key, uint seed = 0)