diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-06-25 12:51:12 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-06-25 14:22:34 (GMT) |
commit | 0fa3175421a3329fe4cb3fb9f79f30807105b02f (patch) | |
tree | 4e555026bc55f38e732ce77ab6a571549d6d91a8 /src/script/api/qscriptengine.cpp | |
parent | 860094d4e2614c0926f2af0e3538412ece31f726 (diff) | |
download | Qt-0fa3175421a3329fe4cb3fb9f79f30807105b02f.zip Qt-0fa3175421a3329fe4cb3fb9f79f30807105b02f.tar.gz Qt-0fa3175421a3329fe4cb3fb9f79f30807105b02f.tar.bz2 |
Fix conversion between JavaScript Date and QDateTime
Use JavaScriptCore's conversion functions rather than our own
(incomplete) implementation.
Specifically, this means daylight saving time is finally
handled correctly on Windows.
Task-number: QTBUG-9770
Reviewed-by: Olivier Goffart
Diffstat (limited to 'src/script/api/qscriptengine.cpp')
-rw-r--r-- | src/script/api/qscriptengine.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 655026c..7bccffe 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -438,6 +438,53 @@ qsreal ToNumber(const QString &value) #endif +static const qsreal MsPerSecond = 1000.0; + +static inline int MsFromTime(qsreal t) +{ + int r = int(::fmod(t, MsPerSecond)); + return (r >= 0) ? r : r + int(MsPerSecond); +} + +/*! + \internal + Converts a JS date value (milliseconds) to a QDateTime (local time). +*/ +QDateTime MsToDateTime(JSC::ExecState *exec, qsreal t) +{ + if (qIsNaN(t)) + return QDateTime(); + JSC::GregorianDateTime tm; + JSC::msToGregorianDateTime(exec, t, /*output UTC=*/true, tm); + int ms = MsFromTime(t); + QDateTime convertedUTC = QDateTime(QDate(tm.year + 1900, tm.month + 1, tm.monthDay), + QTime(tm.hour, tm.minute, tm.second, ms), Qt::UTC); + return convertedUTC.toLocalTime(); +} + +/*! + \internal + Converts a QDateTime to a JS date value (milliseconds). +*/ +qsreal DateTimeToMs(JSC::ExecState *exec, const QDateTime &dt) +{ + if (!dt.isValid()) + return qSNaN(); + QDateTime utc = dt.toUTC(); + QDate date = utc.date(); + QTime time = utc.time(); + JSC::GregorianDateTime tm; + tm.year = date.year() - 1900; + tm.month = date.month() - 1; + tm.monthDay = date.day(); + tm.weekDay = date.dayOfWeek(); + tm.yearDay = date.dayOfYear(); + tm.hour = time.hour(); + tm.minute = time.minute(); + tm.second = time.second(); + return JSC::gregorianDateTimeToMS(exec, tm, time.msec(), /*inputIsUTC=*/true); +} + void GlobalClientData::mark(JSC::MarkStack& markStack) { engine->mark(markStack); |