diff options
Diffstat (limited to 'src/widgets/widgets/qdatetimeedit.cpp')
-rw-r--r-- | src/widgets/widgets/qdatetimeedit.cpp | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 63d5ae268e..4880b5ee46 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> @@ -226,8 +229,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 @@ -251,11 +254,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); } } @@ -1714,6 +1720,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); @@ -2345,33 +2370,6 @@ void QDateTimeEdit::paintEvent(QPaintEvent *event) style()->drawComplexControl(QStyle::CC_ComboBox, &optCombo, &p, this); } -/* - Returns the string for AM and PM markers. - - If a translation for "AM" and "PM" is installed, then use that. - Otherwise, use the default implementation, which uses the locale. -*/ -QString QDateTimeEditPrivate::getAmPmText(AmPm ap, Case cs) const -{ - QString original; - QString translated; - if (ap == AmText) { - original = QLatin1String(cs == UpperCase ? "AM" : "am"); - translated = (cs == UpperCase ? QDateTimeParser::tr("AM") : QDateTimeParser::tr("am")); - } else { - original = QLatin1String(cs == UpperCase ? "PM" : "pm"); - translated = (cs == UpperCase ? QDateTimeParser::tr("PM") : QDateTimeParser::tr("pm")); - } - - // This logic fails if a translation exists but doesn't change the string, - // which we can accept as a corner-case for which a locale-derived answer - // will be acceptable. - if (original != translated) - return translated; - - return QDateTimeParser::getAmPmText(ap, cs); -} - int QDateTimeEditPrivate::absoluteIndex(QDateTimeEdit::Section s, int index) const { for (int i=0; i<sectionNodes.size(); ++i) { |