Search code examples
qtdate-parsingclockify

Qt QDateTime::fromString gives incorrect value with Qt::ISODate


I receive a response from the Clockify JSON API which contains dates and times in ISO8601 format.

The format of the strings looks fine to me, but when I convert them to a QDateTime with QDateTime::fromString, I get strange and incorrect values:

QString aString          = "2021-09-10T15:56:00Z";
QString aDifferentString = "2021-09-10T15:56:00";
QString theString = durationInfo.value("end").toString();
QDateTime aDateTime = QDateTime::fromString(aString, Qt::ISODate);
QDateTime anotherDateTime = QDateTime::fromString(aString, "yyyy-MM-ddTHH:mm:ssZ");
QDateTime aDifferentDateTime = QDateTime::fromString(aDifferentString, Qt::ISODate);
endTime = QDateTime::fromString(theString,Qt::ISODate);

But when I debug, I see the variables have these values after the code has run:

DateTime    Thu Jan 31 07:23:38 1974    QDateTime
aDifferentDateTime  Sat Nov 27 22:39:02 1971    QDateTime
aDifferentString    "2021-09-10T15:56:00"   QString
aString "2021-09-10T15:56:00Z"  QString
anotherDateTime Sat Nov 27 22:39:02 1971    QDateTime
endTime Thu Jan 31 07:23:38 1974    QDateTime
theString   "2021-09-10T15:56:00Z"  QString

Screenshot of debug variables view

Am I missing something here, or is the fromString function or the format spec broken? The rest of the logic in my program behaves correspondingly weird because the dates are all wrong.

I'm using Qt Creator 4.13.0

Based on Qt 5.15.0 (MSVC 2019, 64 bit)

Built on Aug 25 2020 10:06:59

From revision fff3b41833


Solution

  • The problem here is/was the display of Qt types in the debugger.

    The variables view shows erroneous values for the QDateTime variables (and possibly others). These are not the same as values printed if you pass them to, for example, qDebug().

    The lesson to be learned is to double-check if the debugger is telling you the truth.

    Why the values displayed in the debugger should be wrong, I'm not sure...