Remove untruth from QDateTime::fromString() documentation

Two examples were given, one to show "invalid" usage that would, in
fact, work now - producing a date in 2012 - and the other to show
"correct" code which, while correctly delivering the date in 1912 its
author appears to have meant (albeit, giving a four-digit year would
have made that clearer), uses the string API where code should
normally construct dates - much more efficiently - by just passing the
numbers to suitable constructors.

Add tests verifying that the two date-times from the out-of-date
examples do in fact work, even if you tell them the wrong century as
default for two-digit dates.

Pick-to: 6.7 6.5
Change-Id: I8155af019c80729323ba3958fe3942a72bfefc22
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 82d85c16d912b25bfa5b0a081e515fcecda1f975)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Edward Welbourne 2024-09-30 16:56:25 +02:00 committed by Qt Cherry-pick Bot
parent dcea42ea5d
commit 3167b157cd
3 changed files with 7 additions and 22 deletions

View File

@ -150,18 +150,6 @@ qDebug() << "UTC time is:" << UTC;
qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";
//! [19]
//! [20]
QString string = "Monday, 23 April 12 22:51:41";
QString format = "dddd, d MMMM yy hh:mm:ss";
QDateTime invalid = QDateTime::fromString(string, format);
//! [20]
//! [21]
QString string = "Tuesday, 23 April 12 22:51:41";
QString format = "dddd, d MMMM yy hh:mm:ss";
QDateTime valid = QDateTime::fromString(string, format);
//! [21]
//! [22]
// 23 April 2012:
QDate date = std::chrono::year_month_day(std::chrono::year(2012),

View File

@ -5883,16 +5883,7 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
two digits.
Incorrectly specified fields of the \a string will cause an invalid
QDateTime to be returned. For example, consider the following code,
where the two digit year 12 is read as 1912 (see the table below for all
field defaults); the resulting datetime is invalid because 23 April 1912
was a Tuesday, not a Monday:
\snippet code/src_corelib_time_qdatetime.cpp 20
The correct code is:
\snippet code/src_corelib_time_qdatetime.cpp 21
QDateTime to be returned.
\note Day and month names as well as AM/PM indicators must be given in
English (C locale). If localized month and day names or localized forms of

View File

@ -3309,6 +3309,12 @@ void tst_QDateTime::fromStringStringFormat_data()
<< u"2018 wilful long working block relief 12-19T21:09 cruel"_s
<< u"yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux"_s
<< 1900 << QDateTime();
QTest::newRow("fix-century-Mon")
<< u"Monday, 23 April 12 22:51:41"_s << u"dddd, d MMMM yy hh:mm:ss"_s << 1900
<< QDateTime(QDate(2012, 4, 23), QTime(22, 51, 41));
QTest::newRow("fix-century-Tue")
<< u"Tuesday, 23 April 12 22:51:41"_s << u"dddd, d MMMM yy hh:mm:ss"_s << 2000
<< QDateTime(QDate(1912, 4, 23), QTime(22, 51, 41));
// test unicode
QTest::newRow("unicode handling")