From c00ee2f31013e99c79b820a0db57003c110a5510 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 1 Jun 2021 18:27:48 +0200 Subject: Use UTC when parsing only a date or only a time, not a date-time This should reduce the amount of fall-out from DST complications. Also document the assumptions of QDateTimeParser's two fromString() methods (and fix the punctuation on the QDateTime parameter). Adjusted some tests to match. Since only QDateTime-returning methods will show the difference, and it's at least somewhat odd to be using those on QDateEdit or QTimeEdit, this should have little impact on API users. [ChangeLog][QtCore][Behavior Change] QDateEdit and QTimeEdit now operate in UTC, to avoid spurious complications arising from time-zone transitions (e.g. DST) causing the implicit other half to combine with the part being edited to make an invalid result. Returns from their dateTime() and other methods returning QDateTime (max/min) shall thus be in UTC where previously they were in local time. QDateTimeEdit continues using local time. The default can be over-ridden by setTimeSpec(), as ever. Change-Id: I44fece004c12342fe536bbe3048217d236fd97b2 Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetimeparser.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/corelib/time') diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 9117e74375..e699bdeb24 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -2198,21 +2198,25 @@ QString QDateTimeParser::stateName(State s) const } } +// Only called when we want only one of date or time; use UTC to avoid bogus DST issues. bool QDateTimeParser::fromString(const QString &t, QDate *date, QTime *time) const { - QDateTime datetime; - if (!fromString(t, &datetime)) + QDateTime val(QDate(1900, 1, 1).startOfDay(Qt::UTC)); + const StateNode tmp = parse(t, -1, val, false); + if (tmp.state != Acceptable || tmp.conflicts) return false; if (time) { - const QTime t = datetime.time(); + Q_ASSERT(!date); + const QTime t = tmp.value.time(); if (!t.isValid()) return false; *time = t; } if (date) { - const QDate d = datetime.date(); + Q_ASSERT(!time); + const QDate d = tmp.value.date(); if (!d.isValid()) return false; *date = d; @@ -2220,7 +2224,8 @@ bool QDateTimeParser::fromString(const QString &t, QDate *date, QTime *time) con return true; } -bool QDateTimeParser::fromString(const QString &t, QDateTime* datetime) const +// Only called when we want both date and time; default to local time. +bool QDateTimeParser::fromString(const QString &t, QDateTime *datetime) const { QDateTime val(QDate(1900, 1, 1).startOfDay()); const StateNode tmp = parse(t, -1, val, false); -- cgit v1.2.3