aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsapi
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-01-12 10:00:48 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-01-17 21:58:31 +0100
commitc108a817f4ccd023c3b9d9d19427b8d65eb4e348 (patch)
tree5117c3b1f1bfd2cbc712b521d8e15a65b4d2df34 /src/qml/jsapi
parente3cb23d34b89c0f7f26d55d8e87e4390123b09cc (diff)
QJSEngine: Add efficient conversions for QDateTime and friends
The JavaScript date and time conversions are different from Qt's. Add them to coerceValue. Task-number: QTBUG-109380 Change-Id: Ic0d7dd8ff51fb8e29d80d9084d4415becaa76259 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r--src/qml/jsapi/qjsengine.cpp11
-rw-r--r--src/qml/jsapi/qjsengine.h42
2 files changed, 50 insertions, 3 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp
index 54efde52d7..add8626691 100644
--- a/src/qml/jsapi/qjsengine.cpp
+++ b/src/qml/jsapi/qjsengine.cpp
@@ -12,6 +12,7 @@
#include "private/qv4globalobject_p.h"
#include "private/qv4script_p.h"
#include "private/qv4runtime_p.h"
+#include <private/qv4dateobject_p.h>
#include <private/qqmlbuiltinfunctions_p.h>
#include <private/qqmldebugconnector_p.h>
#include <private/qv4qobjectwrapper_p.h>
@@ -953,6 +954,16 @@ QString QJSEngine::convertQObjectToString(QObject *object)
handle(), object ? object->metaObject() : nullptr, object);
}
+QString QJSEngine::convertDateTimeToString(const QDateTime &dateTime)
+{
+ return QV4::DateObject::dateTimeToString(dateTime, handle());
+}
+
+QDate QJSEngine::convertDateTimeToDate(const QDateTime &dateTime)
+{
+ return QV4::DateObject::dateTimeToDate(dateTime);
+}
+
/*! \fn template <typename T> QJSValue QJSEngine::toScriptValue(const T &value)
Creates a QJSValue with the given \a value.
diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h
index d5a8eaef15..11f1788701 100644
--- a/src/qml/jsapi/qjsengine.h
+++ b/src/qml/jsapi/qjsengine.h
@@ -9,6 +9,7 @@
#include <QtCore/qvariant.h>
#include <QtCore/qsharedpointer.h>
#include <QtCore/qobject.h>
+#include <QtCore/qtimezone.h>
#include <QtQml/qjsvalue.h>
#include <QtQml/qjsmanagedvalue.h>
#include <QtQml/qqmldebug.h>
@@ -201,9 +202,41 @@ public:
if constexpr (std::is_same_v<To, QVariant>)
return QVariant::fromValue(from);
- if constexpr (std::is_same_v<To, QString>
- && std::is_base_of_v<QObject, std::remove_const_t<std::remove_pointer_t<From>>>) {
- return convertQObjectToString(from);
+ if constexpr (std::is_same_v<To, QString>) {
+ if constexpr (std::is_base_of_v<QObject, std::remove_const_t<std::remove_pointer_t<From>>>)
+ return convertQObjectToString(from);
+ }
+
+ if constexpr (std::is_same_v<From, QDateTime>) {
+ if constexpr (std::is_same_v<To, QDate>)
+ return convertDateTimeToDate(from.toLocalTime());
+ if constexpr (std::is_same_v<To, QTime>)
+ return from.toLocalTime().time();
+ if constexpr (std::is_same_v<To, QString>)
+ return convertDateTimeToString(from.toLocalTime());
+ }
+
+ if constexpr (std::is_same_v<From, QDate>) {
+ if constexpr (std::is_same_v<To, QDateTime>)
+ return from.startOfDay(QTimeZone::UTC);
+ if constexpr (std::is_same_v<To, QTime>) {
+ // This is the current time zone offset, for better or worse
+ return coerceValue<QDateTime, QTime>(coerceValue<QDate, QDateTime>(from));
+ }
+ if constexpr (std::is_same_v<To, QString>)
+ return convertDateTimeToString(coerceValue<QDate, QDateTime>(from));
+ }
+
+ if constexpr (std::is_same_v<From, QTime>) {
+ if constexpr (std::is_same_v<To, QDate>) {
+ // Yes. April Fools' 1971. See qv4dateobject.cpp.
+ return from.isValid() ? QDate(1971, 4, 1) : QDate();
+ }
+
+ if constexpr (std::is_same_v<To, QDateTime>)
+ return QDateTime(coerceValue<QTime, QDate>(from), from, QTimeZone::LocalTime);
+ if constexpr (std::is_same_v<To, QString>)
+ return convertDateTimeToString(coerceValue<QTime, QDateTime>(from));
}
if constexpr (std::is_same_v<To, std::remove_const_t<std::remove_pointer_t<To>> const *>) {
@@ -280,7 +313,10 @@ private:
bool convertVariant(const QVariant &value, QMetaType metaType, void *ptr);
bool convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to);
+
QString convertQObjectToString(QObject *object);
+ QString convertDateTimeToString(const QDateTime &dateTime);
+ static QDate convertDateTimeToDate(const QDateTime &dateTime);
template<typename T>
friend inline T qjsvalue_cast(const QJSValue &);