diff options
author | John Layt <jlayt@kde.org> | 2013-10-15 17:36:36 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-16 21:29:17 +0200 |
commit | 6a63554bf8226b4ffcf23e45627e6e7920671fac (patch) | |
tree | 94bea1f54c2d658041da2b35883c6234d119a420 /src/corelib/tools | |
parent | 24b817f20a7956470971b64436f6b8f1f9745115 (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>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 33 |
1 files changed, 30 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 |