Results 1 to 8 of 8

Thread: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

  1. #1
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    This works for me
    Qt Code:
    1. #include <QDateTime>
    2. #include <QDebug>
    3.  
    4. int main()
    5. {
    6. QDateTime d = QDateTime::currentDateTime();
    7.  
    8. const QString format = "yyyy-MM-dd hh:mm:ss";
    9. const QString formatted = d.toString(format);
    10. qDebug() << "d=" << d << ", formatted=" << formatted;
    11.  
    12. QDateTime d2 = QDateTime::fromString(formatted, format);
    13. qDebug() << "d2=" << d2;
    14.  
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Output is
    Qt Code:
    1. d= QDateTime(2016-09-04 18:51:28.549 CEST Qt::TimeSpec(LocalTime)) , formatted= "2016-09-04 18:51:28"
    2. d2= QDateTime(2016-09-04 18:51:28.000 CEST Qt::TimeSpec(LocalTime))
    To copy to clipboard, switch view to plain text mode 
    Qt 5.5.1, Debian/Unstable

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    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")

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    Quote Originally Posted by abshaev View Post
    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 :
    Qt Code:
    1. const QString format = "yyyy-MM-dd hh:mm:ss.zzz";
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    Quote Originally Posted by Lesiok View Post
    This code does exactly what to do. Format string does not contain expressions for milliseconds. Try this :
    Qt Code:
    1. const QString format = "yyyy-MM-dd hh:mm:ss.zzz";
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    What does it mean "is still invalid" ? d2.isValid() returns false or You see somewhere different value than you expect ?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    I've changed the second output to
    Qt Code:
    1. qDebug() << "d2=" << d2 << ", d2.isValid=" << d2.isValid();
    To copy to clipboard, switch view to plain text mode 
    and the output is
    Qt Code:
    1. d2= QDateTime(2016-09-05 12:25:39.000 CEST Qt::TimeSpec(LocalTime)) , d2.isValid= true
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.2 bug of incorrect calling of QDateTime::fromString() ?

    So change it to :
    Qt Code:
    1. qDebug() << "d2=" << d2.toString(format) << ", d2.isValid=" << d2.isValid();
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by Lesiok; 5th September 2016 at 12:23.

Similar Threads

  1. Incorrect Julian Day
    By Rayven in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2011, 13:10
  2. QMake Incorrect Makefile generated
    By Octal in forum Qt Tools
    Replies: 7
    Last Post: 19th March 2011, 15:05
  3. Incorrect position of QPushButton on mac
    By francis in forum Qt Programming
    Replies: 4
    Last Post: 1st March 2011, 17:34
  4. QDateTime fromString
    By mklieber in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 21:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.