diff options
author | Mitch Curtis <mitch.curtis@digia.com> | 2013-01-03 13:01:17 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-03 18:19:28 +0100 |
commit | 1b47059c886d76cc1a403e74abd034cce3b53a34 (patch) | |
tree | 3950a5ae1c4b9c76869c4b9defcfb6f26702651c | |
parent | ee7dbf97bbec888a9a5abceeab87c4a570ef20fc (diff) |
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>
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp | 16 | ||||
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 12 |
2 files changed, 27 insertions, 1 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp index a2b128800e..d57c1314a8 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp @@ -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]
\ No newline at end of file +//! [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] diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 9ac4c1a87f..9949aeb7a1 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -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: |