Important: Please read the Qt Code of Conduct -

[solved] QDateTime::fromString missing seconds.

  • When running the following code in Qt 5.4.1 mingw Win7x64.

    QString bLine("2015-06-03T12.20.55Z");
    QDateTime begintime = QDateTime::fromString(bLine, Qt::ISODate);
    qDebug() << bLine << " --> " << begintime.toString(Qt::ISODate);

    I get weird results.

    "2015-06-03T12.20.55Z"  -->  "2015-06-03T12:20:33Z"

    Where did the 22 seconds go? Leap seconds?

  • It's probably because you used a period (.) as the time separator instead of a colon and it interpreted what you meant to be 55 seconds as 55/100 of a minute (33 seconds).

  • No way.

    "2015-06-03T12.20.99Z"  -->  "2015-06-03T12:20:59Z"

    So this is an bug right? Since ISO 8601 uses 0-59+1 (for leap seconds) and not 0-99.
    The Qdocu says "the second with a leading zero (00 to 59)" for ss.

    QString bLine("2015-06-03T12.20.30Z");
    QString format("");
    QDateTime begintime = QDateTime::fromString(bLine, format);
    qDebug() << bLine << " --> " << begintime.toString(Qt::ISODate);

    However, that will fail on leap seconds. Which I don't need... yet...

  • Why don't you just use:

    QString bLine("2015-06-03T12:20:55Z");
    QDateTime begintime = QDateTime::fromString(bLine, Qt::ISODate);

    Qt::ISODate expects colons between hours, minutes, and seconds, (HH:mm:ss) so your result isn't surprising.

  • Because it reads 20.30 minutes instead of 20 minutes and 30 seconds.
    I'll find and replace the dots with semicolons. Semicolons are not supported in file names, that why dots are used.
    It's so obvious if you think about it....

  • OK, it's not so obvious when you hadn't mentioned that the date-time string was being used for a filename. Also, this is a colon (:) and this is a semicolon (;).

  • @Jeroen3 please use the proper time format separator which is a colon (:)

  • @Pablo-J.-Rogina Yes I will QString::replace . for : (a colon).
    Since the filename timestamp and file content timestamp are created using the same routine on an proprietary platform I cannot use a colon initially.

Log in to reply