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

Possible loss of time-zone information from KDDateTime arguments #123

Closed
rokm opened this issue Mar 29, 2018 · 2 comments
Closed

Possible loss of time-zone information from KDDateTime arguments #123

rokm opened this issue Mar 29, 2018 · 2 comments

Comments

@rokm
Copy link
Contributor

rokm commented Mar 29, 2018

As a work-around to #122, one may try to construct a KDDateTime object and explicitly set its timezone via KDDateTime::setTimeZone() method. However, this information may get lost when object is passed to a remote-method call request:

#include <KDSoapClient/KDSoapClientInterface.h>
#include <KDSoapClient/KDDateTime.h>

#include <QtCore>

int main (int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    // Current time in local time-zone
    KDDateTime currentLocal(QDateTime::currentDateTime());

    // Current time in UTC
    KDDateTime currentUtc(QDateTime::currentDateTimeUtc());
    currentUtc.setTimeZone("Z");

    // Dummy request
    KDSoapMessage request;

    KDSoapValueList data;
    data.addArgument("TimeStampL", currentLocal);
    data.addArgument("TimeStampU", currentUtc);

    request.addArgument("Data", data);

    // Call remote method
    // Set up test end-point: nc -l 8080
    KDSoapClientInterface iface("http://localhost:8080", "test");
    KDSoapMessage response = iface.call("TestRequest", request);

    return 0;
}

Running the above example (compiled with Qt5) results in the following request:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <n1:TestRequest xmlns:n1="test">
      <Data>
        <TimeStampL>2018-03-29T21:15:04.874</TimeStampL>
        <TimeStampU>2018-03-29T19:15:04.874</TimeStampU>
      </Data>
    </n1:TestRequest>
  </soap:Body>
</soap:Envelope>

As can be seen, the explicitly-set time-zone string was lost. The issue here is that KDSoapValueList::addArgument() will implicitly construct a QVariant, and in the above example, it will use the QVariant::QVariant(const QDateTime &) constructor. So the variant actually stores a QDateTime, and during serialization goes through the following case:

case QVariant::DateTime: // http://www.w3.org/TR/xmlschema-2/#dateTime

which constructs a new KDDateTime object without time-zone information.

The above issue is avoided by explicitly constructing QVariant:

    data.addArgument("TimeStampU", QVariant::fromValue(currentUtc));

and could be addressed in the library by providing an overload of KDSoapValueList::addArgument() for KDDateTime, which would take care of explicit QVariant construction.

@dfaure-kdab
Copy link
Member

The problem is that such overloads would have to be added in KDSoapValue::addArgument, KDSoapMessage::addArgument, KDSoapValue constructor.... anywhere where a value is passed as QVariant.

I think it would be better to provide a conversion operator to QVariant in KDDateTime.
Do you want to try that and submit such a patch as a merge request if it works out?

@rokm
Copy link
Contributor Author

rokm commented Mar 12, 2019

Adding a conversion operator to QVariant does indeed seem to fix the issue as well. I'll open a pull request.

rokm added a commit to rokm/KDSoap that referenced this issue Mar 12, 2019
rokm added a commit to rokm/KDSoap that referenced this issue Mar 12, 2019
This prevents implicit conversion to base QDateTime and loss of
time-zone information when passing the value as QVariant.

Fixes KDAB#123.
rokm added a commit to rokm/KDSoap that referenced this issue Mar 13, 2019
rokm added a commit to rokm/KDSoap that referenced this issue Mar 13, 2019
This prevents implicit conversion to base QDateTime and loss of
time-zone information when passing the value as QVariant.

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

No branches or pull requests

2 participants