Clarify how two digit years are parsed by QDateTime::fromString().

It's currently not obvious that two digit years will always be in the
20th century (1900's).

Task-number: QTBUG-28797
Change-Id: I7dee9a46e0cb803a8f097debc5443d1789c2f16c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Venugopal Shivashankar <venugopal.shivashankar@digia.com>
This commit is contained in:
Mitch Curtis 2013-01-03 13:01:17 +01:00 committed by The Qt Project
parent ee7dbf97bb
commit 1b47059c88
2 changed files with 27 additions and 1 deletions

View File

@ -143,6 +143,8 @@ QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid
//! [14]
QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s");
// dateTime is January 30 in 1900 at 00:00:01.
dateTime = QDateTime::fromString("12", "yy");
// dateTime is January 1 in 1912 at 00:00:00.
//! [14]
//! [15]
@ -191,4 +193,16 @@ UTC.setTimeSpec(Qt::UTC);
qDebug() << "UTC time is:" << UTC;
qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";
//! [19]
//! [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]

View File

@ -3533,6 +3533,18 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
This could have meant 1 January 00:30.00 but the M will grab
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_tools_qdatetime.cpp 20
The correct code is:
\snippet code/src_corelib_tools_qdatetime.cpp 21
For any field that is not represented in the format, the following
defaults are used: