diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 3ce1f501c4..02f020b032 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -2282,7 +2282,12 @@ QDate QLocale::toDate(const QString &string, const QString &format, QCalendar ca \note The month and day names used must be given in the user's local language. - If the string could not be parsed, returns an invalid QDateTime. + If the string could not be parsed, returns an invalid QDateTime. If the + string can be parsed and represents an invalid date-time (e.g. in a gap + skipped by a time-zone transition), an invalid QDateTime is returned, whose + toMSecsSinceEpoch() represents a near-by date-time that is valid. Passing + that to fromMSecsSinceEpoch() will produce a valid date-time that isn't + faithfully represented by the string parsed. \sa dateTimeFormat(), toTime(), toDate(), QDateTime::fromString() */ @@ -2302,7 +2307,7 @@ QDateTime QLocale::toDateTime(const QString &string, const QString &format, QCal QDateTimeParser dt(QMetaType::QDateTime, QDateTimeParser::FromString, cal); dt.setDefaultLocale(*this); - if (dt.parseFormat(format) && dt.fromString(string, &datetime)) + if (dt.parseFormat(format) && (dt.fromString(string, &datetime) || !datetime.isValid())) return datetime; #else Q_UNUSED(string); diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 342ff5ccde..757636ede2 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -4977,7 +4977,13 @@ QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format) \snippet code/src_corelib_time_qdatetime.cpp 12 - If the format is not satisfied, an invalid QDateTime is returned. + If the format is not satisfied, an invalid QDateTime is returned. If the + format is satisfied but \a string represents an invalid date-time (e.g. in a + gap skipped by a time-zone transition), an invalid QDateTime is returned, + whose toMSecsSinceEpoch() represents a near-by date-time that is + valid. Passing that to fromMSecsSinceEpoch() will produce a valid date-time + that isn't faithfully represented by the string parsed. + The expressions that don't have leading zeroes (d, M, h, m, s, z) will be greedy. This means that they will use two digits (or three, for z) even if this will put them outside the range and/or leave too few digits for other @@ -5025,7 +5031,7 @@ QDateTime QDateTime::fromString(const QString &string, QStringView format, QCale QDateTimeParser dt(QMetaType::QDateTime, QDateTimeParser::FromString, cal); dt.setDefaultLocale(QLocale::c()); - if (dt.parseFormat(format) && dt.fromString(string, &datetime)) + if (dt.parseFormat(format) && (dt.fromString(string, &datetime) || !datetime.isValid())) return datetime; #else Q_UNUSED(string);