diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index a642358770..bf92be2dd4 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -2842,6 +2842,9 @@ inline bool QDateTime::Data::isShort() const { bool b = quintptr(d) & QDateTimePrivate::ShortData; + // sanity check: + Q_ASSERT(b || (d->m_status & QDateTimePrivate::ShortData) == 0); + // even if CanBeSmall = false, we have short data for a default-constructed // QDateTime object. But it's unlikely. if (CanBeSmall) @@ -3658,7 +3661,7 @@ void QDateTime::setMSecsSinceEpoch(qint64 msecs) d.data.status = status; } else { d.detach(); - d->m_status = status; + d->m_status = status & ~QDateTimePrivate::ShortData; d->m_msecs = msecs; } diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index e54edb490c..33844feafe 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -667,6 +667,29 @@ void tst_QDateTime::setMSecsSinceEpoch() QCOMPARE(dt.time(), utc.time()); QCOMPARE(dt.timeSpec(), Qt::UTC); + { + QDateTime dt1 = QDateTime::fromMSecsSinceEpoch(msecs, Qt::UTC); + QCOMPARE(dt1, utc); + QCOMPARE(dt1.date(), utc.date()); + QCOMPARE(dt1.time(), utc.time()); + QCOMPARE(dt1.timeSpec(), Qt::UTC); + } + { + QDateTime dt1(utc.date(), utc.time(), Qt::UTC); + QCOMPARE(dt1, utc); + QCOMPARE(dt1.date(), utc.date()); + QCOMPARE(dt1.time(), utc.time()); + QCOMPARE(dt1.timeSpec(), Qt::UTC); + } + { + // used to fail to clear the ShortData bit, causing corruption + QDateTime dt1 = dt.addDays(0); + QCOMPARE(dt1, utc); + QCOMPARE(dt1.date(), utc.date()); + QCOMPARE(dt1.time(), utc.time()); + QCOMPARE(dt1.timeSpec(), Qt::UTC); + } + if (zoneIsCET) { QCOMPARE(dt.toLocalTime(), cet);