diff options
author | Mitch Curtis <mitch.curtis@nokia.com> | 2012-08-16 15:55:31 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-21 11:05:20 +0200 |
commit | 718a2251be2d32f96befef1ec23912c1a2ce3e34 (patch) | |
tree | 8642ddb6f094fd873f20c6d45c6e9b507c3ef181 /src/corelib | |
parent | 17c89d157a0df2ed9fd0585c8ded21b04278ccf7 (diff) |
Serialise QDateTime as UTC and timeSpec() instead of private spec.
QDateTime currently serialises its private Spec enum. If a user was to
deserialise the individual components of a QDateTime separately, the
resulting timeSpec may be invalid when cast to the Qt::TimeSpec enum.
E.g.:
QDateTime dateTime(QDate(2012, 8, 14), QTime(8, 0, 0), Qt::UTC);
// ... serialise
// ... deserialise date, time, spec separately.
// spec == 2, the value of QDateTimePrivate::UTC.
// spec != UTC, will be set to LocalUnknown.
QDateTime deserialised(date, time, spec);
This patch serialises QDateTime objects in UTC and the value of
timeSpec() as opposed to QDateTimePrivate's spec. This changes the
serialisation behaviour of QDateTime for version 13 of QDataStream.
Task-number: QTBUG-4057
Change-Id: If650e7960dca7b6ab44b8233410a6369c41df73a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 293838e9a5..3324408fde 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -3666,9 +3666,19 @@ QDataStream &operator>>(QDataStream &in, QTime &time) */ QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime) { - out << dateTime.d->date << dateTime.d->time; - if (out.version() >= 7) - out << (qint8)dateTime.d->spec; + if (out.version() >= 13) { + if (dateTime.isValid()) { + QDateTime asUTC = dateTime.toUTC(); + out << asUTC.d->date << asUTC.d->time; + } else { + out << dateTime.d->date << dateTime.d->time; + } + out << (qint8)dateTime.timeSpec(); + } else { + out << dateTime.d->date << dateTime.d->time; + if (out.version() >= 7) + out << (qint8)dateTime.d->spec; + } return out; } @@ -3684,11 +3694,22 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime) { dateTime.detach(); - qint8 ts = (qint8)QDateTimePrivate::LocalUnknown; in >> dateTime.d->date >> dateTime.d->time; - if (in.version() >= 7) + + if (in.version() >= 13) { + qint8 ts = 0; in >> ts; - dateTime.d->spec = (QDateTimePrivate::Spec)ts; + if (dateTime.isValid()) { + // We always store the datetime as UTC in 13 onwards. + dateTime.d->spec = QDateTimePrivate::UTC; + dateTime = dateTime.toTimeSpec(static_cast<Qt::TimeSpec>(ts)); + } + } else { + qint8 ts = (qint8)QDateTimePrivate::LocalUnknown; + if (in.version() >= 7) + in >> ts; + dateTime.d->spec = (QDateTimePrivate::Spec)ts; + } return in; } #endif // QT_NO_DATASTREAM |