Fix consistency of parsing ISO 8601 date strings.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 21 Mar 2012 02:47:15 +0000 (12:47 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 21 Mar 2012 03:05:38 +0000 (04:05 +0100)
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>

src/qml/qml/qqmlstringconverters.cpp
tests/auto/qml/qqmlecmascript/data/assignDate.2.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/data/assignDate.3.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/data/assignDate.4.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/data/assignDate.5.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/data/assignDate.6.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/data/assignDate.qml [new file with mode: 0644]
tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp

index 2c7f6c9..82f4f7d 100644 (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
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.2.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.2.qml
new file mode 100644 (file)
index 0000000..5787018
--- /dev/null
@@ -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")
+}
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.3.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.3.qml
new file mode 100644 (file)
index 0000000..32b88d0
--- /dev/null
@@ -0,0 +1,6 @@
+import Qt.test 1.0
+
+MyTypeObject {
+    dateProperty: if(1) "1982-11-25Z"
+    dateTimeProperty: if(1) "2009-05-12T13:22:01Z"
+}
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.4.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.4.qml
new file mode 100644 (file)
index 0000000..16213c6
--- /dev/null
@@ -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")
+}
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.5.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.5.qml
new file mode 100644 (file)
index 0000000..ff1d85f
--- /dev/null
@@ -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"
+}
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.6.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.6.qml
new file mode 100644 (file)
index 0000000..859c02f
--- /dev/null
@@ -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")
+}
diff --git a/tests/auto/qml/qqmlecmascript/data/assignDate.qml b/tests/auto/qml/qqmlecmascript/data/assignDate.qml
new file mode 100644 (file)
index 0000000..07a638d
--- /dev/null
@@ -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")
+    }
+}
index 650197e..ca4708e 100644 (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()
 {
     {