summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Layt <jlayt@kde.org>2013-10-15 17:36:36 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-16 21:29:17 +0200
commit6a63554bf8226b4ffcf23e45627e6e7920671fac (patch)
tree94bea1f54c2d658041da2b35883c6234d119a420
parent24b817f20a7956470971b64436f6b8f1f9745115 (diff)
QDateTime - Fix toTimeSpec() for invalid datetimes
Check if the datetime is valid before converting it to a different time spec. If it is invalid then just change the spec to keep behavior consistent with 5.1. Task-number: QTBUG-34020 Change-Id: I6630ec1d50f810a2178ab3222bd32af018085f81 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/tools/qdatetime.cpp33
-rw-r--r--tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp21
2 files changed, 51 insertions, 3 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 9ddb96b424..dec4d9aaa4 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -3921,7 +3921,16 @@ qint64 QDateTime::msecsTo(const QDateTime &other) const
QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
{
- return fromMSecsSinceEpoch(toMSecsSinceEpoch(), spec, 0);
+ if (d->m_spec == spec && (spec == Qt::UTC || spec == Qt::LocalTime))
+ return *this;
+
+ if (!isValid()) {
+ QDateTime ret = *this;
+ ret.setTimeSpec(spec);
+ return ret;
+ }
+
+ return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), spec, 0);
}
/*!
@@ -3939,7 +3948,16 @@ QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
{
- return fromMSecsSinceEpoch(toMSecsSinceEpoch(), Qt::OffsetFromUTC, offsetSeconds);
+ if (d->m_spec == Qt::OffsetFromUTC && d->m_offsetFromUtc == offsetSeconds)
+ return *this;
+
+ if (!isValid()) {
+ QDateTime ret = *this;
+ ret.setOffsetFromUtc(offsetSeconds);
+ return ret;
+ }
+
+ return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), Qt::OffsetFromUTC, offsetSeconds);
}
#ifndef QT_BOOTSTRAPPED
@@ -3953,7 +3971,16 @@ QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
QDateTime QDateTime::toTimeZone(const QTimeZone &timeZone) const
{
- return fromMSecsSinceEpoch(toMSecsSinceEpoch(), timeZone);
+ if (d->m_spec == Qt::TimeZone && d->m_timeZone == timeZone)
+ return *this;
+
+ if (!isValid()) {
+ QDateTime ret = *this;
+ ret.setTimeZone(timeZone);
+ return ret;
+ }
+
+ return fromMSecsSinceEpoch(d->toMSecsSinceEpoch(), timeZone);
}
#endif // QT_BOOTSTRAPPED
diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp
index b8b970121c..0c560df423 100644
--- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp
+++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp
@@ -147,6 +147,8 @@ private slots:
void daylightTransitions() const;
void timeZones() const;
+ void invalid() const;
+
private:
bool europeanTimeZone;
QDate defDate() const { return QDate(1900, 1, 1); }
@@ -2899,5 +2901,24 @@ void tst_QDateTime::timeZones() const
QCOMPARE(hourAfterStd.toMSecsSinceEpoch(), dstToStdMSecs + 3600000);
}
+void tst_QDateTime::invalid() const
+{
+ QDateTime invalidDate = QDateTime(QDate(0, 0, 0), QTime(-1, -1, -1));
+ QCOMPARE(invalidDate.isValid(), false);
+ QCOMPARE(invalidDate.timeSpec(), Qt::LocalTime);
+
+ QDateTime utcDate = invalidDate.toUTC();
+ QCOMPARE(utcDate.isValid(), false);
+ QCOMPARE(utcDate.timeSpec(), Qt::UTC);
+
+ QDateTime offsetDate = invalidDate.toOffsetFromUtc(3600);
+ QCOMPARE(offsetDate.isValid(), false);
+ QCOMPARE(offsetDate.timeSpec(), Qt::OffsetFromUTC);
+
+ QDateTime tzDate = invalidDate.toTimeZone(QTimeZone("Europe/Oslo"));
+ QCOMPARE(tzDate.isValid(), false);
+ QCOMPARE(tzDate.timeSpec(), Qt::TimeZone);
+}
+
QTEST_APPLESS_MAIN(tst_QDateTime)
#include "tst_qdatetime.moc"