PDA

View Full Version : QT 5.2 bug of incorrect calling of QDateTime::fromString() ?



abshaev
4th September 2016, 17:45
Hi everybody!

I cannot understand the reason of my problem connected with calling of function QDateTime::fromString.
The result of it is always "invalid" independently on converting format and datetime string.

When i calling opposite function .toString everything is OK:

QDateTime local(QDateTime::currentDateTime());
QString format = "yyyy-MM-dd hh:mm:ss";
QString S = local.toString(format);

Result: S = "2016-09-04 13:35:29"

But if I call it with the same settings it fails:

QDateTime dateTime = QDateTime::fromString(S, format);

Result: dateTime = (invalid)

Is it a bug of QT, if yes how to improve it ?

I use:
- Linux Ubuntu 15.04 64 bit
- Qt Creator 3.0.0
Based on Qt 5.2.0 (GCC 4.6.1, 64 bit)
Built on Dec 10 2013 at 11:45:55
From revision 27d10d8dcd

anda_skoa
4th September 2016, 18:03
This works for me


#include <QDateTime>
#include <QDebug>

int main()
{
QDateTime d = QDateTime::currentDateTime();

const QString format = "yyyy-MM-dd hh:mm:ss";
const QString formatted = d.toString(format);
qDebug() << "d=" << d << ", formatted=" << formatted;

QDateTime d2 = QDateTime::fromString(formatted, format);
qDebug() << "d2=" << d2;

return 0;
}

Output is


d= QDateTime(2016-09-04 18:51:28.549 CEST Qt::TimeSpec(LocalTime)) , formatted= "2016-09-04 18:51:28"
d2= QDateTime(2016-09-04 18:51:28.000 CEST Qt::TimeSpec(LocalTime))

Qt 5.5.1, Debian/Unstable

Cheers,
_

abshaev
4th September 2016, 18:30
It is interesting, your code outputs the correct result on my desktop, but the value of datetime variable d2 is still "invalid" !

Debugging starts
&"warning: GDB: Failed to set controlling terminal: Inappropriate ioctl for device\n"
d= QDateTime("2016-09-04 20:19:13.029 MSK Qt::LocalTime") , formatted= "2016-09-04 20:19:13"
d2= QDateTime("2016-09-04 20:19:13.000 MSK Qt::LocalTime")

Lesiok
4th September 2016, 19:05
It is interesting, your code outputs the correct result on my desktop, but the value of datetime variable d2 is still "invalid" !

Debugging starts
&"warning: GDB: Failed to set controlling terminal: Inappropriate ioctl for device\n"
d= QDateTime("2016-09-04 20:19:13.029 MSK Qt::LocalTime") , formatted= "2016-09-04 20:19:13"
d2= QDateTime("2016-09-04 20:19:13.000 MSK Qt::LocalTime")This code does exactly what to do. Format string does not contain expressions for milliseconds. Try this :

const QString format = "yyyy-MM-dd hh:mm:ss.zzz";

abshaev
4th September 2016, 20:58
This code does exactly what to do. Format string does not contain expressions for milliseconds. Try this :

const QString format = "yyyy-MM-dd hh:mm:ss.zzz";

I have added milliseconds to the format string, but resulting qdatetime is still invalid !))
Debug output is normal.
So I suppose that there is the bug in my Qt or Qt Creator release.

Lesiok
5th September 2016, 08:18
What does it mean "is still invalid" ? d2.isValid() returns false or You see somewhere different value than you expect ?

anda_skoa
5th September 2016, 11:37
I've changed the second output to


qDebug() << "d2=" << d2 << ", d2.isValid=" << d2.isValid();

and the output is


d2= QDateTime(2016-09-05 12:25:39.000 CEST Qt::TimeSpec(LocalTime)) , d2.isValid= true


Cheers,
_

Lesiok
5th September 2016, 12:14
So change it to :
qDebug() << "d2=" << d2.toString(format) << ", d2.isValid=" << d2.isValid();
P.S.
Look at qdatetime.cpp how operator << for qdebug is working for this class.
In Qt 4.8 it uses QDateTime::toString() for this, in 5.7 QDateTime::toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz t")).
So this is not a bug.