Commit 847345d2 authored by Volker Krause's avatar Volker Krause
Browse files

Add a method to safely copy a QDateTime in JavaScript

We must not go through a JS Date object for this, as that loses the very
relevant timezone information.
parent d5e3540e
......@@ -25,6 +25,7 @@
#include <QJsonArray>
#include <QJsonObject>
#include <QLocale>
#include <QMetaProperty>
#include <QRegularExpression>
#include <QUrl>
......@@ -257,6 +258,22 @@ QJSValue JsApi::JsonLd::toGeoCoordinates(const QString &mapUrl)
return {};
QJSValue JsApi::JsonLd::readQDateTime(const QVariant &obj, const QString &propName) const
const auto mo = QMetaType::metaObjectForType(obj.userType());
if (!mo) {
return {};
const auto propIdx = mo->indexOfProperty(propName.toUtf8().constData());
if (propIdx < 0) {
qWarning() << "Unknown property name:" << mo->className() << propName;
return {};
const auto prop = mo->property(propIdx);
const auto dt = prop.readOnGadget(obj.constData());
return toJson(dt);
void JsApi::JsonLd::setContextDate(const QDateTime& dt)
m_contextDate = dt;
......@@ -89,6 +89,15 @@ public:
Q_INVOKABLE QJSValue toGeoCoordinates(const QString &mapUrl);
/** Read a QDateTime property and return a JSON-LD serialization of it.
* This is a workaround for JS destroying timezone information when getting in touch with a QDateTime
* object. With this method it is safe to read a QDateTime property e.g. from a Qt gadget or QObject
* without the risk of losing information.
* @param obj The object to read from.
* @param propName The name of the property to read.
Q_INVOKABLE QJSValue readQDateTime(const QVariant &obj, const QString &propName) const;
///@cond internal
void setContextDate(const QDateTime &dt);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment