From 4e01b851159342122d42be1091d4cfa464700a4e Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 15 Nov 2018 17:59:18 +0100 Subject: [PATCH] QDateTimeEdit: fix setDate() if time is in a spring-forward If the time the widget is set to use falls in the gap skipped by a spring-forward, setting the date to the day of the spring-forward turned a valid date into an invalid date-time. So use the usual trick to map the "draft" date-time to a valid one. Fixes: QTBUG-64485 Fixes: QTBUG-58947 Change-Id: Ib8f0f092cd5d6dce3da31eb52cd42150ca0d1fcb Reviewed-by: Mitch Curtis --- src/widgets/widgets/qdatetimeedit.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index fca81bec48..68bfd175ff 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -273,7 +273,13 @@ void QDateTimeEdit::setDate(const QDate &date) setDateRange(date, date); d->clearCache(); - d->setValue(QDateTime(date, d->value.toTime(), d->spec), EmitIfChanged); + QDateTime when(date, d->value.toTime(), d->spec); + // The specified time might not exist on the specified day, + // i.e. the time is in the gap a spring-forward jumps over. + if (!when.isValid()) + when = QDateTime::fromMSecsSinceEpoch(when.toMSecsSinceEpoch(), d->spec); + Q_ASSERT(when.isValid()); + d->setValue(when, EmitIfChanged); d->updateTimeSpec(); } }