summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp16
-rw-r--r--src/corelib/tools/qdatetime.cpp12
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: