summaryrefslogtreecommitdiffstats
path: root/src/corelib/time
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-02-20 17:30:57 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-02-27 19:35:28 +0100
commit924b97d6abf6f9935c9698984a1596c65324e3fa (patch)
treea0726e4c0e18bdb133dd0d09734c27f94e2d0bb8 /src/corelib/time
parent31b06a0437f96935f12eb0db003a5867c83a9228 (diff)
Fix QDateTimeEdit's handling of invalid time in dst gap
During a spring forward, a time-zone omits an hour. A QDateTime with such an hour is invalid, but QDateTimeEdit's handling of this invalid time was not done correctly. With this fix, up/down changes of any field that would result in an invalid date-time corrects the time to be valid, while leaving as much as possible of the user-entered data unchanged. To do that, we rely on QDateTime::toMSecsSinceEpoch to return a value even for such an invalid time, which then can be used to construct a valid QDateTime. Edits that would result in an invalid hour are reverted to the previous when pressing return, if correctionMode is CorrectToPreviousValue. This change also implements support for CorrectToNearestValue, which uses the same mechanism as when stepping over an invalid time. Include a test that verifies that the various interactions result in a reasonable value. Since QDateTimeEdit does not respect the timezone or timespec of the QDateTime it is initialized with, we have to find the first hour of daylight saving time for a year that we know works for most time zones. Failing that, we have to skip the tests. Verified in a wide range of time zones. Change-Id: I05b906ae3b5f6681891d23704f00f9c10cd479ae Fixes: QTBUG-79803 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/time')
-rw-r--r--src/corelib/time/qdatetimeparser.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp
index 790c20004a..2a19611493 100644
--- a/src/corelib/time/qdatetimeparser.cpp
+++ b/src/corelib/time/qdatetimeparser.cpp
@@ -1539,6 +1539,14 @@ QDateTimeParser::parse(QString input, int position, const QDateTime &defaultValu
text = scan.input = input;
// Set spec *after* all checking, so validity is a property of the string:
scan.value = scan.value.toTimeSpec(spec);
+
+ /*
+ However, even with a valid string we might have ended up with an invalid datetime:
+ the non-existent hour during dst changes, for instance.
+ */
+ if (!scan.value.isValid() && scan.state == Acceptable)
+ scan.state = Intermediate;
+
return scan;
}