Skip to content

Commit

Permalink
Fix consistency of parsing ISO 8601 date strings.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Mar 21, 2012
1 parent d6bc3b5 commit b2722ab
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/qml/qml/qqmlstringconverters.cpp
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions 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")
}
6 changes: 6 additions & 0 deletions 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"
}
6 changes: 6 additions & 0 deletions 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")
}
6 changes: 6 additions & 0 deletions 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"
}
6 changes: 6 additions & 0 deletions 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")
}
9 changes: 9 additions & 0 deletions 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")
}
}
28 changes: 26 additions & 2 deletions tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
Expand Up @@ -71,6 +71,8 @@ class tst_qqmlecmascript : public QQmlDataTest
private slots:
void initTestCase();
void assignBasicTypes();
void assignDate_data();
void assignDate();
void idShortcutInvalidates();
void boolPropertiesEvaluateAsBool();
void methods();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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()
{
{
Expand Down

0 comments on commit b2722ab

Please sign in to comment.