summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qdatetimeedit.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2019-11-28 14:22:09 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2019-12-12 11:36:01 +0100
commit2a653fde48f7312ccd2f792d72d305061b410ae3 (patch)
tree398d018623b2649ce90be1fba6cbd141d92ddd8a /src/widgets/widgets/qdatetimeedit.cpp
parent5b193e3dd47704daf56c8817d0157f66363044c3 (diff)
Convert date-time faithfully in QDateTimeEdit::setDateTime()
Previously, setDateTime() was documented to ignore the new date-time's time-spec. It used the date and time (determined using that timespec) with the QDateTimeEdit's configured spec. It is debatable whether that really counts as ignoring its time-spec. All the same, that's what it did. Fixing it is a behavior change. Added tests. [ChangeLog][QtWidgets][QDateTimeEdit] QDateTimeEdit::setDateTime() now converts the new datetime to the QDateTimeEdit's time-spec, rather than combining its date and time (determined using the time spec it came with) with the QDateTimeEdit's date and time. Fixes: QTBUG-71181 Change-Id: Ibf0bd87723c3957ca00a2199d51d992032ef57ee Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Konstantin Shegunov <kshegunov@gmail.com>
Diffstat (limited to 'src/widgets/widgets/qdatetimeedit.cpp')
-rw-r--r--src/widgets/widgets/qdatetimeedit.cpp33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp
index 3d6716666d..33300b542a 100644
--- a/src/widgets/widgets/qdatetimeedit.cpp
+++ b/src/widgets/widgets/qdatetimeedit.cpp
@@ -53,6 +53,9 @@
#include <qlayout.h>
#include <qset.h>
#include <qstyle.h>
+#if QT_CONFIG(timezone)
+#include <QTimeZone>
+#endif
#include <algorithm>
@@ -218,8 +221,8 @@ QDateTimeEdit::~QDateTimeEdit()
\property QDateTimeEdit::dateTime
\brief the QDateTime that is set in the QDateTimeEdit
- When setting this property the timespec of the QDateTimeEdit remains the same
- and the timespec of the new QDateTime is ignored.
+ When setting this property, the new QDateTime is converted to the timespec of
+ the QDateTimeEdit, which thus remains unchanged.
By default, this property is set to the start of 2000 CE. It can only be set
to a valid QDateTime value. If any operation causes this property to have an
@@ -243,11 +246,14 @@ void QDateTimeEdit::setDateTime(const QDateTime &datetime)
{
Q_D(QDateTimeEdit);
if (datetime.isValid()) {
+ QDateTime when = d->convertTimeSpec(datetime);
+ Q_ASSERT(when.timeSpec() == d->spec);
+
d->clearCache();
- const QDate date = datetime.date();
+ const QDate date = when.date();
if (!(d->sections & DateSections_Mask))
setDateRange(date, date);
- d->setValue(QDateTime(date, datetime.time(), d->spec), EmitIfChanged);
+ d->setValue(when, EmitIfChanged);
}
}
@@ -1706,6 +1712,25 @@ QDateTimeEditPrivate::QDateTimeEditPrivate()
#endif
}
+QDateTime QDateTimeEditPrivate::convertTimeSpec(const QDateTime &datetime)
+{
+ Q_ASSERT(value.toDateTime().timeSpec() == spec);
+ switch (spec) {
+ case Qt::UTC:
+ return datetime.toUTC();
+ case Qt::LocalTime:
+ return datetime.toLocalTime();
+ case Qt::OffsetFromUTC:
+ return datetime.toOffsetFromUtc(value.toDateTime().offsetFromUtc());
+#if QT_CONFIG(timezone)
+ case Qt::TimeZone:
+ return datetime.toTimeZone(value.toDateTime().timeZone());
+#endif
+ }
+ Q_UNREACHABLE();
+}
+
+// FIXME: architecturaly incompatible with OffsetFromUTC or TimeZone as spec (QTBUG-80417).
void QDateTimeEditPrivate::updateTimeSpec()
{
minimum = minimum.toDateTime().toTimeSpec(spec);