Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] [Qt5][client][server] Use a particular DateTime format during saving model as a JSON string #5708

Closed
goodicus opened this issue Mar 25, 2020 · 4 comments · Fixed by #5763

Comments

@goodicus
Copy link

Is your feature request related to a problem? Please describe.

I'm using c++ Qt5 client and server in a network mediator application.
The app receives JSONs, collects them and resends to the Java server using a schedule.
The Java server is generated with OpenAPI also.
The problem is there is a DateTime format mismatch between Qt5 [client/server] and JAVA.

  • Java server excepts Jsons with such DateTime format:
    with
    "2017-07-08T22:35:29.771Z" - suffix Z, means UTC time
    "2017-07-08T22:35:29+02:30" - time zone
    "2017-07-08T22:35:29.771+02:30" - with a suffix and a timezone
  • Qt5 generates by default in such way:
    " 2017-07-08T22:35:29" - withnout Z and a timezone.

Describe the solution you'd like

Currently, QDateTime transforms into a string using a Qt::ISODate format.
QString toStringValue( const QDateTime& value ) {
// ISO 8601
return value.toString( Qt::ISODate );
}

It would be useful for me to set a particular DateTime format option during code generation.
Example for cpp-qt5-client:
useQDateTimeFormat="yyyy-MM-ddTHH:mm:ss.zzzZ";
Smth like this:
QString const DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.zzzZ";
QString toStringValue( const QDateTime& value ) {
return value.toString( QtDateTimeFormat );
}

Describe alternatives you've considered

Additional context

@goodicus goodicus changed the title [REQ] [Qt5][clinet][server] Use a particular DateTime format during saving model to a JSON string [REQ] [Qt5][clinet][server] Use a particular DateTime format during saving model as a JSON string Mar 25, 2020
@etherealjoy
Copy link
Contributor

etherealjoy commented Mar 28, 2020

@DevASG

useQDateTimeFormat="yyyy-MM-ddTHH:mm:ss.zzzZ";

I tried this and this does not seem to work. QDateTime::toString does not support these

"2017-07-08T22:35:29.771Z" - suffix Z, means UTC time
"2017-07-08T22:35:29+02:30" - time zone
"2017-07-08T22:35:29.771+02:30" - with a suffix and a timezone

See here
Z is not supported, instead t is supported but it only shows CET or PST or something.

It is lame. I could build a custom solution but not sure if it will be deprecated soon in next Qt version..

EDIT:
I am not sure building a custom solution is a good idea because the deserialization is not trivial.

@etherealjoy etherealjoy changed the title [REQ] [Qt5][clinet][server] Use a particular DateTime format during saving model as a JSON string [REQ] [Qt5][client][server] Use a particular DateTime format during saving model as a JSON string Mar 29, 2020
@goodicus
Copy link
Author

goodicus commented Mar 30, 2020

@etherealjoy
Let me shed light on problem details using a real example.

Currently, after serialization an OpenAPIObject with QDateTime field indise will look like "2020-03-30T08:59:31[Z|[+|-]08:59]".
This view doesn't reflect any ISO DateTime format!

A possible correct ISO::DateTime view is "2020-03-30T08:59:31.474Z" (UTC time format).
This view can be generated just after making small changes in several lines in OAIHelpers.cpp.

 namespace OpenAPI {
 
+QString const DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.zzzZ";
-
 QString toStringValue(const QString &value) {
     return value;
 }
 
 QString toStringValue(const QDateTime &value) {
     // ISO 8601
+    return value.toString(DateTimeFormat);
-    return value.toString("yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
 }
 
 QString toStringValue(const QByteArray &value) {
     return QString(value);
 }
@@ -71,11 +69,11 @@ QString toStringValue(const OAIHttpFileElement &value) {
 QJsonValue toJsonValue(const QString &value) {
     return QJsonValue(value);
 }
 
 QJsonValue toJsonValue(const QDateTime &value) {
+    return QJsonValue(value.toString(DateTimeFormat));
-    return QJsonValue(value.toString(Qt::ISODate));
 }
 
 QJsonValue toJsonValue(const QByteArray &value) {
     return QJsonValue(QString(value.toBase64()));
 }
@@ -124,11 +122,11 @@ bool fromStringValue(const QString &inStr, QString &value) {
 
 bool fromStringValue(const QString &inStr, QDateTime &value) {
     if (inStr.isEmpty()) {
         return false;
     } else {
+        auto dateTime = QDateTime::fromString(inStr, DateTimeFormat);
-        auto dateTime = QDateTime::fromString(inStr, "yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
         if (dateTime.isValid()) {
             value.setDate(dateTime.date());
             value.setTime(dateTime.time());
         } else {
             qDebug() << "DateTime is invalid";
@@ -229,11 +227,11 @@ bool fromJsonValue(QString &value, const QJsonValue &jval) {
 }
 
 bool fromJsonValue(QDateTime &value, const QJsonValue &jval) {
     bool ok = true;
     if (!jval.isUndefined() && !jval.isNull() && jval.isString()) {
+        value = QDateTime::fromString(jval.toString(), DateTimeFormat);
-        value = QDateTime::fromString(jval.toString(), Qt::ISODate);
         ok = value.isValid();
     } else {
         ok = false;
     }
     return ok;

@etherealjoy
Copy link
Contributor

etherealjoy commented Mar 30, 2020

@DevASG
Thanks for the further clarifications.

Currently, after serialization an OpenAPIObject with QDateTime field indise will look like "2020-03-
30T08:59:31[Z|[+|-]08:59]".
This view doesn't reflect any ISO DateTime format!

This one I fixed here already.
master...etherealjoy:serialization_settings

A possible correct ISO::DateTime view is "2020-03-30T08:59:31.474Z" (UTC time format).

This did not work. But I did provide a way to set the string though.

@goodicus
Copy link
Author

@etherealjoy
Looks good to me, but as I see this patch is only for cpp-qt5-client (
modules/openapi-generator/src/main/resources/cpp-qt5-client/helpers-body.mustache)
.
I think this patch must be applied to qt5-server also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants