0

I have been trying to use QDateTime in my program for a while now, to no such luck. I am not sure what I am doing wrong and it is driving me a tad mad.

Code snippet as follows:

QDateTime dteNow = QDateTime::currentDateTime();
QDateTime dteStart = QDateTime::fromString(QString::fromStdString(advertSchedule.ValidFrom),"yyyy-MM-dd hh:mm:ss"); //ValidFrom = "1990-01-01 00:00:00"
QDateTime dteFinish = QDateTime::fromString(QString::fromStdString(advertSchedule.ValidTo),"yyyy-MM-dd hh:mm:ss"); //ValidTo = "2015-01-01 00:00:00"

//Breakpointed on the line below. Continuing to run causes an exception. 
//Hovering over the QDateTime objects displays "dteNow (invalid) QDateTime".
if(dteNow >= dteStart
    && dteNow < dteFinish
    && dteNow.time() >= dteStart.time()
    && dteNow.time() < dteFinish.time())
{

All three of these date times are invalid at runtime.

Thanks in advance.

GenericMadman
  • 57
  • 1
  • 8
  • 1
    What do you define as "invalid at runtime"? Let's just concentrate on QDateTime::currentDateTime(). what do you get if you print out QDateTime::currentDateTime().toString(Qt::TextDate) ? – TheDarkKnight Oct 14 '14 at 15:02
  • I get nothing, it doesn't output anything. And by at runtime I mean I have breakpointed after these three lines, and hovering over them all shows they are all invalid. – GenericMadman Oct 14 '14 at 15:07
  • Can you post the context of the code? Could the QDateTime objects be going out of scope? Ignore what the debugger says and output the text to the console. – TheDarkKnight Oct 14 '14 at 15:32
  • I have edited the code snippet. I hope it helps. – GenericMadman Oct 14 '14 at 15:36
  • From the docs for QDateTime::fromStdString "Returns the QDateTime represented by the string, using the format given, or an invalid datetime if this is not possible." I expect that the format is wrong and you're getting an invalid string, so calling .time is triggering the exception. – TheDarkKnight Oct 14 '14 at 15:41
  • Check the values that are being entered into QDateTime::fromString and note that QDateTime has an isValid() function, which you can check too: http://qt-project.org/doc/qt-4.8/qdatetime.html#isValid – TheDarkKnight Oct 14 '14 at 15:42
  • @Merlin069 I guessed as much for the second two datetimes, but the first datetime object "QDateTime dteNow = QDateTime::currentDateTime()" is returning invalid too. – GenericMadman Oct 14 '14 at 15:44

1 Answers1

1

As an exception is being caused when calling .time() on a QDateTime, one or more of the QDateTime objects is invalid.

I suggest you breakdown these lines: -

QDateTime::fromString(QString::fromStdString(advertSchedule.ValidFrom),"yyyy-MM-dd hh:mm:ss");
QDateTime dteFinish = QDateTime::fromString(QString::fromStdString(advertSchedule.ValidTo),"yyyy-MM-dd hh:mm:ss"); 

Check what is returned in calling QString::fromStdString(advertSchedule.ValidFrom) and QString::fromStdString(advertSchedule.ValidTo).

Try the following too: -

QDateTime dteNow = QDateTime::currentDateTime();
if(dteNow.isValid())
{
    qDebug("The date is valid: %s\n", dteNow.toString());
}
else
{
    qDebug("The current date returned is invalid\n");
}

If this prints that the date is invalid in the Application Output window (or console, if you're running from there), clean the project and rebuild.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85