diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/time/qdatetimeparser.cpp | 8 | ||||
-rw-r--r-- | src/widgets/widgets/qdatetimeedit.cpp | 22 |
2 files changed, 29 insertions, 1 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; } diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 63d5ae268e..c89cef99b5 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -1442,7 +1442,16 @@ void QDateTimeEdit::fixup(QString &input) const QValidator::State state; int copy = d->edit->cursorPosition(); - d->validateAndInterpret(input, copy, state, true); + QDateTime value = d->validateAndInterpret(input, copy, state, true); + /* + String was valid, but the datetime still is not; use the time that + has the same distance from epoch. + CorrectToPreviousValue correction is handled by QAbstractSpinBox. + */ + if (!value.isValid() && d->correctionMode == QAbstractSpinBox::CorrectToNearestValue) { + value = QDateTime::fromMSecsSinceEpoch(value.toMSecsSinceEpoch(), value.timeSpec()); + input = textFromDateTime(value); + } } @@ -2060,6 +2069,17 @@ QDateTime QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) c const int oldDay = v.date().day(calendar); setDigit(v, sectionIndex, val); + /* + Stepping into a daylight saving time that doesn't exist, + so use the time that has the same distance from epoch. + */ + if (!v.isValid()) { + auto msecsSinceEpoch = v.toMSecsSinceEpoch(); + // decreasing from e.g 3am to 2am would get us back to 3am, but we want 1am + if (steps < 0 && sn.type & HourSectionMask) + msecsSinceEpoch -= 3600 * 1000; + v = QDateTime::fromMSecsSinceEpoch(msecsSinceEpoch, v.timeSpec()); + } // if this sets year or month it will make // sure that days are lowered if needed. |