Creating QDateTime from string
-
I am trying to create an instance of QDateTime from a string, the string I have is in the format:
yyyy/MM/dd hh:mm:ss.zzzI've tried:
QDateTime dtmCreated(QDateTime::fromString(strValue));Where strValue contains:
2021-08-20T06:45:18I then output dtmCreated to the console:
qDebug() << strValue << dtmCreated.isValid();isValid returns false, I've also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));Same result isValid is false.
Also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));Same result isValid is false.
-
I am trying to create an instance of QDateTime from a string, the string I have is in the format:
yyyy/MM/dd hh:mm:ss.zzzI've tried:
QDateTime dtmCreated(QDateTime::fromString(strValue));Where strValue contains:
2021-08-20T06:45:18I then output dtmCreated to the console:
qDebug() << strValue << dtmCreated.isValid();isValid returns false, I've also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));Same result isValid is false.
Also tried:
QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));Same result isValid is false.
@SPlatten said in Creating QDateTime from string:
So, your date string contains-as separators and your format is passed as/2021-08-20T06:45:18
Do you see the discrepancy? 😉
-
@SPlatten said in Creating QDateTime from string:
So, your date string contains-as separators and your format is passed as/2021-08-20T06:45:18
Do you see the discrepancy? 😉
-
int main(int argc, char *argv[]) { QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss"); qDebug() << td.isValid() << td; return 0; } -
@J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.
-
@J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.
@SPlatten said in Creating QDateTime from string:
when converting to QDateTime, its only accurate to seconds.
QDateTimeaccuracy is down to milliseconds:// Read milliseconds time stamp QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz"); // add milliseconds to QDateTime dt = dt.addMSecs(998); -
@SPlatten said in Creating QDateTime from string:
when converting to QDateTime, its only accurate to seconds.
QDateTimeaccuracy is down to milliseconds:// Read milliseconds time stamp QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz"); // add milliseconds to QDateTime dt = dt.addMSecs(998);@KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:
QString strName(crField.name()), strValue(crField.value().toString());crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.
-
@KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:
QString strName(crField.name()), strValue(crField.value().toString());crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.
@SPlatten said in Creating QDateTime from string:
when converting the TIMESTAMP(6) from the database
According to
QVariantdocumentation,QVariant::toDateTime()will useQt::ISODateand notQt::ISODateWithMs. So with the default conversion, you will never got milliseconds.Do you really need micro-seconds in DB? Is
TIMESTAMP(3)(milliseconds) not enough for you?
With this type, you should be able to parse the DATETIME with milliseconds (withQt::ISODateWithMs/yyyy-MM-ddThh:mm:ss.zzz). -
@SPlatten said in Creating QDateTime from string:
when converting the TIMESTAMP(6) from the database
According to
QVariantdocumentation,QVariant::toDateTime()will useQt::ISODateand notQt::ISODateWithMs. So with the default conversion, you will never got milliseconds.Do you really need micro-seconds in DB? Is
TIMESTAMP(3)(milliseconds) not enough for you?
With this type, you should be able to parse the DATETIME with milliseconds (withQt::ISODateWithMs/yyyy-MM-ddThh:mm:ss.zzz).@KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
-
@KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
@SPlatten said in Creating QDateTime from string:
to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
Yes, I think so:
QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS); -
@SPlatten said in Creating QDateTime from string:
to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?
Yes, I think so:
QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS);@KroMignon, I'm not sure that is going to work because the toString function already removes the fraction part of the time.