Fix roundtrip conversion of datetimes.

QDateTime.toString() is writing out milliseconds since change
15da0a5af2. Unfortunately this breaks
QDateTime::fromString() with Qt::TextDate which can't handle the new
format.

Fix by making QDateTime::fromString split up seconds and milliseconds
on a period, if any. Now
QDateTime dt = ...;
assert(QDateTime::fromString(dt.toString(), Qt::TextDate) == dt)
works again.

Change-Id: Ibfe9032e357ceaf894e33f3e33affe94f56dbf5c
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Mitch Curtis 2013-06-14 13:43:16 +02:00 committed by The Qt Project
parent 08d652ca67
commit 8a680201f1
2 changed files with 22 additions and 4 deletions

View File

@ -3409,13 +3409,29 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
return QDateTime();
}
int second = (timeParts.count() > 2) ? timeParts.at(2).toInt(&ok) : 0;
if (!ok) {
return QDateTime();
int second = 0;
int millisecond = 0;
if (timeParts.count() > 2) {
QStringList secondParts = timeParts.at(2).split(QLatin1Char('.'));
if (secondParts.size() > 2) {
return QDateTime();
}
second = secondParts.first().toInt(&ok);
if (!ok) {
return QDateTime();
}
if (secondParts.size() > 1) {
millisecond = secondParts.last().toInt(&ok);
if (!ok) {
return QDateTime();
}
}
}
QDate date(year, month, day);
QTime time(hour, minute, second);
QTime time(hour, minute, second, millisecond);
if (parts.count() == 5)
return QDateTime(date, time, Qt::LocalTime);

View File

@ -1614,6 +1614,8 @@ void tst_QDateTime::fromStringDateFormat_data()
<< Qt::TextDate << invalidDateTime();
QTest::newRow("text invalid gmt minute") << QString::fromLatin1("Thu 1. Jan 1970 00:00:00 GMT+000X")
<< Qt::TextDate << invalidDateTime();
QTest::newRow("text second fraction") << QString::fromLatin1("Mon 6. May 2013 01:02:03.456")
<< Qt::TextDate << QDateTime(QDate(2013, 5, 6), QTime(1, 2, 3, 456));
// Test Qt::ISODate format.
QTest::newRow("ISO +01:00") << QString::fromLatin1("1987-02-13T13:24:51+01:00")