diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-08-31 09:15:30 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-08-31 09:20:40 (GMT) |
commit | ae574f7341f4f76a1f7de570cc3d56f77781e625 (patch) | |
tree | fd85a743bfb3917380b6134ee2a0b92956e3b2ba /tests/auto/qscriptengine/tst_qscriptengine.cpp | |
parent | eb1015c7bbf135af3656110a4d112377c1209db8 (diff) | |
download | Qt-ae574f7341f4f76a1f7de570cc3d56f77781e625.zip Qt-ae574f7341f4f76a1f7de570cc3d56f77781e625.tar.gz Qt-ae574f7341f4f76a1f7de570cc3d56f77781e625.tar.bz2 |
Fix QtScript Date <--> QDateTime (local time) conversion
This has already been fixed in 4.7 (QTBUG-9770), but the change is
too big to backport.
The general idea is the same: Only operate on UTC dates internally
(since that's how JS dates are stored), and let QDateTime take care
of converting from/to local dates as necessary.
The fix itself shouldn't be merged to 4.7, but the autotests should.
Task-number: QTBUG-9770
Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'tests/auto/qscriptengine/tst_qscriptengine.cpp')
-rw-r--r-- | tests/auto/qscriptengine/tst_qscriptengine.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index bb01a42..3bb9d17 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -166,6 +166,10 @@ private slots: void qRegExpInport_data(); void qRegExpInport(); + void dateRoundtripJSQtJS(); + void dateRoundtripQtJSQt(); + void dateConversionJSQt(); + void dateConversionQtJS(); }; tst_QScriptEngine::tst_QScriptEngine() @@ -4895,5 +4899,67 @@ void tst_QScriptEngine::qRegExpInport() } } +// QScriptValue::toDateTime() returns a local time, whereas JS dates +// are always stored as UTC. QtScript must respect the current time +// zone, and correctly adjust for daylight saving time that may be in +// effect at a given date (QTBUG-9770). +void tst_QScriptEngine::dateRoundtripJSQtJS() +{ + uint secs = QDateTime(QDate(2009, 1, 1)).toUTC().toTime_t(); + QScriptEngine eng; + for (int i = 0; i < 8000; ++i) { + QScriptValue jsDate = eng.evaluate(QString::fromLatin1("new Date(%0)").arg(secs * 1000.0)); + QDateTime qtDate = jsDate.toDateTime(); + QScriptValue jsDate2 = eng.newDate(qtDate); + if (jsDate2.toNumber() != jsDate.toNumber()) + QFAIL(qPrintable(jsDate.toString())); + secs += 2*60*60; + } +} + +void tst_QScriptEngine::dateRoundtripQtJSQt() +{ + QDateTime qtDate = QDateTime(QDate(2009, 1, 1)); + QScriptEngine eng; + for (int i = 0; i < 8000; ++i) { + QScriptValue jsDate = eng.newDate(qtDate); + QDateTime qtDate2 = jsDate.toDateTime(); + if (qtDate2 != qtDate) + QFAIL(qPrintable(qtDate.toString())); + qtDate = qtDate.addSecs(2*60*60); + } +} + +void tst_QScriptEngine::dateConversionJSQt() +{ + uint secs = QDateTime(QDate(2009, 1, 1)).toUTC().toTime_t(); + QScriptEngine eng; + for (int i = 0; i < 8000; ++i) { + QScriptValue jsDate = eng.evaluate(QString::fromLatin1("new Date(%0)").arg(secs * 1000.0)); + QDateTime qtDate = jsDate.toDateTime(); + QString qtUTCDateStr = qtDate.toUTC().toString(Qt::ISODate); + QString jsUTCDateStr = jsDate.property("toISOString").call(jsDate).toString(); + jsUTCDateStr.chop(5); // get rid of milliseconds (".000Z") + if (qtUTCDateStr != jsUTCDateStr) + QFAIL(qPrintable(jsDate.toString())); + secs += 2*60*60; + } +} + +void tst_QScriptEngine::dateConversionQtJS() +{ + QDateTime qtDate = QDateTime(QDate(2009, 1, 1)); + QScriptEngine eng; + for (int i = 0; i < 8000; ++i) { + QScriptValue jsDate = eng.newDate(qtDate); + QString jsUTCDateStr = jsDate.property("toISOString").call(jsDate).toString(); + jsUTCDateStr.chop(5); // get rid of milliseconds (".000Z") + QString qtUTCDateStr = qtDate.toUTC().toString(Qt::ISODate); + if (jsUTCDateStr != qtUTCDateStr) + QFAIL(qPrintable(qtDate.toString())); + qtDate = qtDate.addSecs(2*60*60); + } +} + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" |