Converting QDateTime fromString - issue with specific hour
-
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?
-
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?
@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. -
@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. -
@kshegunov Oh, thanks. You are right. I forgot all about that. European summer time started this morning. And I was there debugging for hours...
-
@kshegunov Oh, thanks. You are right. I forgot all about that. European summer time started this morning. And I was there debugging for hours...
@Edin-M Wow! That was "bad timing" :-D