Fix consistency of parsing ISO 8601 date strings.

Use the UTC timespec for strings with no time zone qualifier as V8 does
so we don't get different QDateTime in bindings depending on whether
the string was bound directly, or parsed by constructing a new JS
Date object.

Task-number: QTBUG-24895
Change-Id: I8f74dae418aaeeaf06df33fe58ab4e3e3fea791b
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
This commit is contained in:
Andrew den Exter 2012-03-21 12:47:15 +10:00 committed by Qt by Nokia
parent d6bc3b5907
commit b2722ab31e
8 changed files with 68 additions and 2 deletions

View File

@ -174,6 +174,9 @@ QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
{
QDateTime d = QDateTime::fromString(s, Qt::ISODate);
if (ok) *ok = d.isValid();
// V8 never parses a date string as local time. For consistency do the same here.
if (d.timeSpec() == Qt::LocalTime)
d.setTimeSpec(Qt::UTC);
return d;
}
#endif // QT_NO_DATESTRING

View File

@ -0,0 +1,6 @@
import Qt.test 1.0
MyTypeObject {
dateProperty: if(1) new Date("1982-11-25")
dateTimeProperty: if(1) new Date("2009-05-12T13:22:01")
}

View File

@ -0,0 +1,6 @@
import Qt.test 1.0
MyTypeObject {
dateProperty: if(1) "1982-11-25Z"
dateTimeProperty: if(1) "2009-05-12T13:22:01Z"
}

View File

@ -0,0 +1,6 @@
import Qt.test 1.0
MyTypeObject {
dateProperty: if(1) new Date("1982-11-25Z")
dateTimeProperty: if(1) new Date("2009-05-12T13:22:01Z")
}

View File

@ -0,0 +1,6 @@
import Qt.test 1.0
MyTypeObject {
dateProperty: if(1) "1982-11-25Z"
dateTimeProperty: if(1) "2009-05-12T15:22:01+02:00"
}

View File

@ -0,0 +1,6 @@
import Qt.test 1.0
MyTypeObject {
dateProperty: if(1) new Date("1982-11-25")
dateTimeProperty: if(1) new Date("2009-05-12T15:22:01+02:00")
}

View File

@ -0,0 +1,9 @@
import Qt.test 1.0
import QtQuick 2.0
MyTypeObject {
Component.onCompleted: {
dateProperty = new Date("1982-11-25")
dateTimeProperty = new Date("2009-05-12T13:22:01")
}
}

View File

@ -71,6 +71,8 @@ public:
private slots:
void initTestCase();
void assignBasicTypes();
void assignDate_data();
void assignDate();
void idShortcutInvalidates();
void boolPropertiesEvaluateAsBool();
void methods();
@ -278,7 +280,7 @@ void tst_qqmlecmascript::assignBasicTypes()
QCOMPARE(object->colorProperty(), QColor("red"));
QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
QCOMPARE(object->timeProperty(), QTime(11, 11, 32));
QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1)));
QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
QCOMPARE(object->pointProperty(), QPoint(99,13));
QCOMPARE(object->pointFProperty(), QPointF(-10.1, 12.3));
QCOMPARE(object->sizeProperty(), QSize(99, 13));
@ -306,7 +308,7 @@ void tst_qqmlecmascript::assignBasicTypes()
QCOMPARE(object->colorProperty(), QColor("red"));
QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
QCOMPARE(object->timeProperty(), QTime(11, 11, 32));
QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1)));
QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
QCOMPARE(object->pointProperty(), QPoint(99,13));
QCOMPARE(object->pointFProperty(), QPointF(-10.1, 12.3));
QCOMPARE(object->sizeProperty(), QSize(99, 13));
@ -321,6 +323,28 @@ void tst_qqmlecmascript::assignBasicTypes()
}
}
void tst_qqmlecmascript::assignDate_data()
{
QTest::addColumn<QUrl>("source");
QTest::newRow("Component.onComplete JS") << testFileUrl("assignDate.qml");
QTest::newRow("Binding JS") << testFileUrl("assignDate.2.qml");
QTest::newRow("Binding UTC") << testFileUrl("assignDate.3.qml");
QTest::newRow("Binding JS UTC") << testFileUrl("assignDate.4.qml");
QTest::newRow("Binding UTC+2") << testFileUrl("assignDate.5.qml");
QTest::newRow("Binding JS UTC+2 ") << testFileUrl("assignDate.6.qml");
}
void tst_qqmlecmascript::assignDate()
{
QFETCH(QUrl, source);
QQmlComponent component(&engine, source);
QScopedPointer<QObject> obj(component.create());
MyTypeObject *object = qobject_cast<MyTypeObject *>(obj.data());
QVERIFY(object != 0);
QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
}
void tst_qqmlecmascript::idShortcutInvalidates()
{
{