Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting QDateTime fromString - issue with specific hour
Forum Updated to NodeBB v4.3 + New Features

Converting QDateTime fromString - issue with specific hour

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Edin-ME Offline
    Edin-ME Offline
    Edin-M
    wrote on last edited by Edin-M
    #1

    Hi, I'm using QDateTime::fromString() however I've encountered false isValid() result. Further inspection and stand alone case show something dazzling.

    #include <QCoreApplication>
    
    #include <QString>
    #include <QDateTime>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
      QCoreApplication a(argc, argv);
    
      QString n1format = "d.M.yyyy. H:mm:ss";
      QString n1src = "27.3.2016. 2:24:22";
      QDateTime d2 = QDateTime::fromString(n1src, n1format);
      bool isvalid2 = true;
      isvalid2 = d2.isValid();
      QString dt4 = d2.toString(n1format);
      QString dt3 = d2.addMonths(1).toString(n1format);
    
      qDebug() << n1format << n1src;
      qDebug() << d2.isValid();
      qDebug() << dt4 << dt3;
    
      qDebug() << QDateTime::fromString("27.3.2016. 1:01:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 2:04:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 3:01:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 3:01:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 4:01:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 7:01:01", "d.M.yyyy. H:mm:ss").toString();
      qDebug() << QDateTime::fromString("27.3.2016. 17:01:01", "d.M.yyyy. H:mm:ss").toString();
    
      for (int i = 0; i < 24; i++) {
        qDebug() << QDateTime::fromString(QString("27.3.2016. %1:01:01").arg(QString::number(i)), "d.M.yyyy. H:mm:ss").toString();
      }
    
      return a.exec();
    }
    
    

    Output:

    "d.M.yyyy. H:mm:ss" "27.3.2016. 2:24:22"
    false
    "" "27.4.2016. 2:24:22"
    "ned o₧u 27 01:01:01 2016"
    ""
    "ned o₧u 27 03:01:01 2016"
    "ned o₧u 27 03:01:01 2016"
    "ned o₧u 27 04:01:01 2016"
    "ned o₧u 27 07:01:01 2016"
    "ned o₧u 27 17:01:01 2016"
    "ned o₧u 27 00:01:01 2016"
    "ned o₧u 27 01:01:01 2016"
    ""
    "ned o₧u 27 03:01:01 2016"
    "ned o₧u 27 04:01:01 2016"
    "ned o₧u 27 05:01:01 2016"
    "ned o₧u 27 06:01:01 2016"
    "ned o₧u 27 07:01:01 2016"
    "ned o₧u 27 08:01:01 2016"
    "ned o₧u 27 09:01:01 2016"
    "ned o₧u 27 10:01:01 2016"
    "ned o₧u 27 11:01:01 2016"
    "ned o₧u 27 12:01:01 2016"
    "ned o₧u 27 13:01:01 2016"
    "ned o₧u 27 14:01:01 2016"
    "ned o₧u 27 15:01:01 2016"
    "ned o₧u 27 16:01:01 2016"
    "ned o₧u 27 17:01:01 2016"
    "ned o₧u 27 18:01:01 2016"
    "ned o₧u 27 19:01:01 2016"
    "ned o₧u 27 20:01:01 2016"
    "ned o₧u 27 21:01:01 2016"
    "ned o₧u 27 22:01:01 2016"
    "ned o₧u 27 23:01:01 2016"
    

    It fails to convert and print .toString() only for hour 2.

    Can anybody reproduce this same issue?

    It happens for me on Qt 5.4.2, Qt 5.5.1, Qt 5.6 using mingw491, 492 and Qt 5.4 with MSVC2013.

    If it does not happen for you, what could possibly cause this issue?

    jerome_isAviableJ 1 Reply Last reply
    0
    • Edin-ME Edin-M

      Hi, I'm using QDateTime::fromString() however I've encountered false isValid() result. Further inspection and stand alone case show something dazzling.

      #include <QCoreApplication>
      
      #include <QString>
      #include <QDateTime>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
        QCoreApplication a(argc, argv);
      
        QString n1format = "d.M.yyyy. H:mm:ss";
        QString n1src = "27.3.2016. 2:24:22";
        QDateTime d2 = QDateTime::fromString(n1src, n1format);
        bool isvalid2 = true;
        isvalid2 = d2.isValid();
        QString dt4 = d2.toString(n1format);
        QString dt3 = d2.addMonths(1).toString(n1format);
      
        qDebug() << n1format << n1src;
        qDebug() << d2.isValid();
        qDebug() << dt4 << dt3;
      
        qDebug() << QDateTime::fromString("27.3.2016. 1:01:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 2:04:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 3:01:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 3:01:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 4:01:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 7:01:01", "d.M.yyyy. H:mm:ss").toString();
        qDebug() << QDateTime::fromString("27.3.2016. 17:01:01", "d.M.yyyy. H:mm:ss").toString();
      
        for (int i = 0; i < 24; i++) {
          qDebug() << QDateTime::fromString(QString("27.3.2016. %1:01:01").arg(QString::number(i)), "d.M.yyyy. H:mm:ss").toString();
        }
      
        return a.exec();
      }
      
      

      Output:

      "d.M.yyyy. H:mm:ss" "27.3.2016. 2:24:22"
      false
      "" "27.4.2016. 2:24:22"
      "ned o₧u 27 01:01:01 2016"
      ""
      "ned o₧u 27 03:01:01 2016"
      "ned o₧u 27 03:01:01 2016"
      "ned o₧u 27 04:01:01 2016"
      "ned o₧u 27 07:01:01 2016"
      "ned o₧u 27 17:01:01 2016"
      "ned o₧u 27 00:01:01 2016"
      "ned o₧u 27 01:01:01 2016"
      ""
      "ned o₧u 27 03:01:01 2016"
      "ned o₧u 27 04:01:01 2016"
      "ned o₧u 27 05:01:01 2016"
      "ned o₧u 27 06:01:01 2016"
      "ned o₧u 27 07:01:01 2016"
      "ned o₧u 27 08:01:01 2016"
      "ned o₧u 27 09:01:01 2016"
      "ned o₧u 27 10:01:01 2016"
      "ned o₧u 27 11:01:01 2016"
      "ned o₧u 27 12:01:01 2016"
      "ned o₧u 27 13:01:01 2016"
      "ned o₧u 27 14:01:01 2016"
      "ned o₧u 27 15:01:01 2016"
      "ned o₧u 27 16:01:01 2016"
      "ned o₧u 27 17:01:01 2016"
      "ned o₧u 27 18:01:01 2016"
      "ned o₧u 27 19:01:01 2016"
      "ned o₧u 27 20:01:01 2016"
      "ned o₧u 27 21:01:01 2016"
      "ned o₧u 27 22:01:01 2016"
      "ned o₧u 27 23:01:01 2016"
      

      It fails to convert and print .toString() only for hour 2.

      Can anybody reproduce this same issue?

      It happens for me on Qt 5.4.2, Qt 5.5.1, Qt 5.6 using mingw491, 492 and Qt 5.4 with MSVC2013.

      If it does not happen for you, what could possibly cause this issue?

      jerome_isAviableJ Offline
      jerome_isAviableJ Offline
      jerome_isAviable
      wrote on last edited by jerome_isAviable
      #2

      @Edin-M Hi, i think the format is wrong.

      try it:

        QString n1format = "dd.MM.yyyy. hh:mm:ss";
      

      but also, i'm not sure the hour could be 1 char without AM/PM indication.
      if format is 24H (HH), then it needs 2 chars (02 instead of 2). Else, it need to know if it is the morning or the evening. I think, but not sure. Also, i always use this like that and never had problem.

      1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @Edin-M
        Daylight savings? If your country uses it (as it ordinarily should) and Qt (or actually the OS) is set up with that default locale/settings, then for the said date there is no such hour. The clock jumps from 1:59 to 3:00.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • Edin-ME Offline
          Edin-ME Offline
          Edin-M
          wrote on last edited by
          #4

          @kshegunov Oh, thanks. You are right. I forgot all about that. European summer time started this morning. And I was there debugging for hours...

          ? 1 Reply Last reply
          0
          • Edin-ME Edin-M

            @kshegunov Oh, thanks. You are right. I forgot all about that. European summer time started this morning. And I was there debugging for hours...

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @Edin-M Wow! That was "bad timing" :-D

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved