Bug? QDateTimeAxis
-
Hello.
I am working on Qt 6.2.4 . I have been using QML ChartView with QML DateTimeAxis on the x-axis and I am updating it in the C++ side. Today I had to perform a fix in my application regarding time zones.
I am getting a very strange behavior which I cannot explain. I am setting a QDateTime object as the max of the DateTimeAxis in C++ side and although the values are correctly set, the toString() function does not report the correct time. You can see in my code below that the toMSecsSinceEpoch() are yielding the same number. This is leading to a problem in the actual chart on the QML side as the current time is not displayed correctly.
If I use the QDateTime::currentDateTimeUtc() directly as the max() the time is being displayed in my time zone - which is again wrong as I am on CET which is UTC + 1 hour.
I have also used all of the other toString options to see if there is a problem in one of them, but they all return plus one hour than they should.
So it seems like the QDateTimeAxis::max() is not returning a correct format.My code:
// Always update the maximum value of the x-axis to the current time QDateTime myCurrentDateTime = theGeneralController->getTheCurrentDateTime(); qInfo() << QString("the to be set %1").arg(myCurrentDateTime.toString("hh:mm")); qInfo() << QString("max before set %1").arg(anAxisX->max().toString("hh:mm")); anAxisX->setMax(myCurrentDateTime); qInfo() << QString("max after set %1").arg(anAxisX->max().toString("hh:mm")); if (anAxisX->max() == myCurrentDateTime) { qInfo("The two datetime objects are equal!"); } if (myCurrentDateTime.isValid()) { qInfo() << QString("full to be set %1").arg(myCurrentDateTime.toString("dd.MM.yyyy hh:mm")); qInfo() << QString("full after set %1").arg(anAxisX->max().toString("dd.MM.yyyy hh:mm")); qInfo() << QString("full to be set %1 format text date").arg(myCurrentDateTime.toString(Qt::DateFormat::TextDate)); qInfo() << QString("full after set %1 format text date").arg(anAxisX->max().toString(Qt::DateFormat::TextDate)); qInfo() << QString("full to be set %1 format iso date").arg(myCurrentDateTime.toString(Qt::DateFormat::ISODate)); qInfo() << QString("full after set %1 format iso date").arg(anAxisX->max().toString(Qt::DateFormat::ISODate)); qInfo() << QString("full to be set %1 format iso ms date").arg(myCurrentDateTime.toString(Qt::DateFormat::ISODateWithMs)); qInfo() << QString("full after set %1 format iso ms date").arg(anAxisX->max().toString(Qt::DateFormat::ISODateWithMs)); qInfo() << QString("full to be set %1 format rfc date").arg(myCurrentDateTime.toString(Qt::DateFormat::RFC2822Date)); qInfo() << QString("full after set %1 format rfc date").arg(anAxisX->max().toString(Qt::DateFormat::RFC2822Date)); qInfo() << QString("msecondssinceepoch of the current date time %1").arg(myCurrentDateTime.toMSecsSinceEpoch()); qInfo() << QString("msecondssinceepoch of the axis date time %1").arg(anAxisX->max().toMSecsSinceEpoch()); qInfo() << QString("msecondssinceepoch of the static date time %1").arg(QDateTime::currentDateTimeUtc().toMSecsSinceEpoch()); }
And the output of the test is:
"the to be set 14:51" "max before set 01:00" "max after set 15:51" "The two datetime objects are equal! " "full to be set 31.10.2023 14:51" "full after set 31.10.2023 15:51" "full to be set Tue Oct 31 14:51:59 2023 GMT format text date" "full after set Tue Oct 31 15:51:59 2023 format text date" "full to be set 2023-10-31T14:51:59Z format iso date" "full after set 2023-10-31T15:51:59 format iso date" "full to be set 2023-10-31T14:51:59.938Z format iso ms date" "full after set 2023-10-31T15:51:59.938 format iso ms date" "full to be set 31 Oct 2023 14:51:59 +0000 format rfc date" "full after set 31 Oct 2023 15:51:59 +0100 format rfc date" "msecondssinceepoch of the current date time 1698763919938" "msecondssinceepoch of the axis date time 1698763919938" "msecondssinceepoch of the static date time 1698760327118"
Am I missing something here?
Thank you!
-
Additional information:
Setting the max like this also does not work:
anAxisX->setMax(QDateTime(myCurrentDateTime));
There seems to be a work-around. If I use the myCurrentDateTime to instantiate a new QDateTime object, then it works.
anAxisX->setMax(QDateTime::fromString(myCurrentDateTime.toString("ddd MMM d HH:mm yyyy")));
However, this is not the exact same timestamp. As you can see in the test output below the milliseconds are not the same.
"the to be set 16:35" "max before set 01:00" "max after set 16:35" "full to be set 31.10.2023 16:35" "full after set 31.10.2023 16:35" "full to be set Tue Oct 31 16:35:08 2023 GMT format text date" "full after set Tue Oct 31 16:35:00 2023 format text date" "full to be set 2023-10-31T16:35:08Z format iso date" "full after set 2023-10-31T16:35:00 format iso date" "full to be set 2023-10-31T16:35:08.954Z format iso ms date" "full after set 2023-10-31T16:35:00.000 format iso ms date" "full to be set 31 Oct 2023 16:35:08 +0000 format rfc date" "full after set 31 Oct 2023 16:35:00 +0100 format rfc date" "msecondssinceepoch of the current date time 1698770108954" "msecondssinceepoch of the axis date time 1698766500000" "msecondssinceepoch of the static date time 1698766521737"
In this case the millisecond accuracy is not crucial for my application, but this behavior is not clear to me.
-
-
-
Please read the solution in this post https://forum.qt.io/post/808545