Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QDate conversions from/to string inconsistent for QLocale::ShortFormat (issue with the century)
Jacques L. last edited by
Looks like a wrong feature in Qt (to avoid to call it a bug).
QDate qdate(2020,12,05); QString qstring = QLocale::system().toString(qdate, QLocale::ShortFormat); QDate qdate2 = QDate::fromString(qstring, QLocale::system().dateFormat(QLocale::ShortFormat)); QString qstring2 = QLocale::system().toString(qdate, QLocale::ShortFormat); #define __DV(x) qDebug() << #x " =" << x __DV(qdate); __DV(qstring); __DV(qdate2); __DV(qstring2);
qdate = QDate("2020-12-05") qstring = "05-12-20" qdate2 = QDate("1920-12-05") qstring2 = "05-12-20"
So, in the above code, I do not encode date strings myself, I'm just letting Qt to generate strings using QLocale::ShortFormat and interpreting its own result back to QDate, still using QLocale::ShortFormat. And Qt is mishandling the century.
Please note that I only need to work in the 21st century here. That's Qt that transform it in 20th century's date
The long format is too much for me but mm-dd-yyyy would be OK. I need to keep using QLocale. I need to save the date in a text file(i.e. as a string), and later read it back. Also I need to show it the shortest possible way in a QTableWidget.
The problem is that QDate string formatting in connection to QLocale is not very flexible, and Qt doesn't have a consistent interpretation in both direction of the QDate to/from QString conversions.
Of course, I may for sure solve this the long way (in terms of coding and runtime), but I wonder if there something that I'm doing wrong and that could be solved easily and a more clean way?
The problem comes from https://code.woboq.org/qt5/qtbase/src/corelib/time/qdatetimeparser.cpp.html#1988 - can't change it for Qt5 anymore since it may break existing implementations but maybe it's not too late for Qt6.