From b2722ab31e524fc223a332fc0574e4b66c716b20 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Wed, 21 Mar 2012 12:47:15 +1000 Subject: [PATCH] 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 --- src/qml/qml/qqmlstringconverters.cpp | 3 ++ .../auto/qml/qqmlecmascript/data/assignDate.2.qml | 6 ++++ .../auto/qml/qqmlecmascript/data/assignDate.3.qml | 6 ++++ .../auto/qml/qqmlecmascript/data/assignDate.4.qml | 6 ++++ .../auto/qml/qqmlecmascript/data/assignDate.5.qml | 6 ++++ .../auto/qml/qqmlecmascript/data/assignDate.6.qml | 6 ++++ tests/auto/qml/qqmlecmascript/data/assignDate.qml | 9 ++++++ .../auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 28 ++++++++++++++++++- 8 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.2.qml create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.3.qml create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.4.qml create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.5.qml create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.6.qml create mode 100644 tests/auto/qml/qqmlecmascript/data/assignDate.qml diff --git a/src/qml/qml/qqmlstringconverters.cpp b/src/qml/qml/qqmlstringconverters.cpp index 2c7f6c9..82f4f7d 100644 --- a/src/qml/qml/qqmlstringconverters.cpp +++ b/src/qml/qml/qqmlstringconverters.cpp @@ -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 index 0000000..5787018 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.2.qml @@ -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 index 0000000..32b88d0 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.3.qml @@ -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 index 0000000..16213c6 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.4.qml @@ -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 index 0000000..ff1d85f --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.5.qml @@ -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 index 0000000..859c02f --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.6.qml @@ -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 index 0000000..07a638d --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/assignDate.qml @@ -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") + } +} diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 650197e..ca4708e 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -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("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 obj(component.create()); + MyTypeObject *object = qobject_cast(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() { { -- 1.7.2.5