QDateTime::fromString() returns invalid
today a small problem with QDateTime, I'm pretty sure it's something minor and I'm just too stupid to see it again, but before I rack my head over this for several hours I might just ask somebody a little more knowledgable than myself. Here's what I have:
@eventTime = QDateTime::fromString( qsl.at(0), "yyyy-MM-dd h:mm:ss AP" );@
eventTime is a QDateTime object, qsl is a QStringList and at(0) might return something like
2014-03-11 4:35:38 PM. The format is always the same, but the values change, of course.
The values from the debugger are correct (qsl.at(0) is indeed of the aforementioned format), but the conversion fails.
I knocked together a little app doing just that, letting me feed it with a date, a matching format in another line and a third line showing me the date in a predefined format. Using the same values as above, everything works fine.
Help is much appreciated!
Can you elaborate on what fails ?
What input makes it fail ?
I replaced the qsl.at(0) call with a concrete date to make sure it's not because of that. Here's the line now:
@eventTime = QDateTime::fromString( "2014-03-11 4:35:38 PM", "yyyy-MM-dd h:mm:ss AP" );@
In debugger I get "(invalid)" as value of eventTime. I use the gdb debugger, btw.
What if you print the value of eventTime ?
What good would that do? I already know the value. Given that the debugger is bug-free. Anything curious about the format I used? Seems ok to me...
Your format looks fine visually, but I'm not able to test it in code at the moment.
[quote author="chatumao" date="1395615052"]What good would that do?[/quote]It might reveal some clues to what went wrong.
If the the value in the debugger is "(invalid)", what do you think will be printed?
Ok, I did it anyways. To my surprise, the value printed seems to be in order?!
Can someone explain? I used the debuger before to check on QDateTime objects and there has never been a problem with the values.
I tried your code, and it works on both Qt 4.8.5 and Qt 5.2.1. Your format string is correct.
[quote author="chatumao" date="1395639145"]If the the value in the debugger is "(invalid)", what do you think will be printed?[/quote]There are multiple possibilities. That's why printing it is helpful.
[quote]Ok, I did it anyways. To my surprise, the value printed seems to be in order?!
Can someone explain? I used the debuger before to check on QDateTime objects and there has never been a problem with the values.[/quote]I'm not sure, I haven't seen something like this before myself. But I daresay Qt can understand its own data quite well; what does eventTime.isValid() return?
P.S. Debuggers are not guaranteed to be bug-free: http://www.gnu.org/software/gdb/bugs/
Ok. eventTime.isValid() returns 1. I guess that's something.
I tried it with the Microsoft debugger cdb.exe.
@eventTime = QDateTime::fromString( "2014-03-11 3:00:38 PM", "yyyy-MM-dd h:mm:ss AP" );@
gives me unix time 0 in the debug information (01.01.1970, 00:00:00).
Printing out the date is working fine.
I am aware of bugs in debuggers, from what I gather there is no bug-free software as soon as there are enough lines of code involved. I just couldn't believe that I would actually encounter one in gdb myself, that's all.
I doubt that it is a bug in the debugger, though. cdb is also showing odd behavior, so I guess it's a Qt thing. Maybe I should report this somewhere?
Thanks for the time both of you.
Feel free to open a bug report at https://bugreports.qt-project.org
If you do, you might post the ID of the report here so anyone interested could follow it.
Alright, I will give this a try. Would be nice to know if you guys experience the same issues, though. Might save some peoples time.