diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2025-07-01 10:38:54 +0800 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2025-07-23 20:27:50 +0000 |
| commit | ab3781ee0a1f4c6fe239e6b216e93a37657caad0 (patch) | |
| tree | aca833dcfa2c7598c2d99a693d9203c19fc4224c | |
| parent | 2b4129675d268ff5750f385d929485ff91e13eb1 (diff) | |
MonthGrid: address post-merge review feedback on timezone patch
Amends 57325020f65665d91e63dc300d674a8e8dc411f1.
- Add a comment explaining why we store
QQuickMonthGridPrivate::pressedDate as QDateTime now.
- Store QDateTimes as local time to simplify things.
- Make firstDateToDisplay const while we're here.
Task-number: QTBUG-72208
Pick-to: 6.8
Change-Id: I9fbee12608855f30d229b4e3a4e78da5561feb44
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 2e1337d87a60b8d1a30f5296bf3f614ec0bca383)
(cherry picked from commit 60f0e4f5f85365a1a342c28e981ee681c94ed7e8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
| -rw-r--r-- | src/quicktemplates/qquickmonthgrid.cpp | 3 | ||||
| -rw-r--r-- | src/quicktemplates/qquickmonthmodel.cpp | 18 |
2 files changed, 8 insertions, 13 deletions
diff --git a/src/quicktemplates/qquickmonthgrid.cpp b/src/quicktemplates/qquickmonthgrid.cpp index 05f74e67c8..54ec72288c 100644 --- a/src/quicktemplates/qquickmonthgrid.cpp +++ b/src/quicktemplates/qquickmonthgrid.cpp @@ -95,6 +95,9 @@ public: QString title; QVariant source; + + // Only the date matters, but we have to store it as QDateTime for compatibility + // with Date: QTBUG-72208. See QQuickMonthModelPrivate::populate for more info. QDateTime pressedDate; int pressTimer; QQuickItem *pressedItem; diff --git a/src/quicktemplates/qquickmonthmodel.cpp b/src/quicktemplates/qquickmonthmodel.cpp index 0cbdb448e1..a3cbda990b 100644 --- a/src/quicktemplates/qquickmonthmodel.cpp +++ b/src/quicktemplates/qquickmonthmodel.cpp @@ -50,19 +50,15 @@ bool QQuickMonthModelPrivate::populate(int m, int y, const QLocale &l, bool forc // QDate is converted to local time when converted to a JavaScript Date, // so if we stored our dates as QDates, it's possible that the date provided // to delegates will be wrong in certain timezones: - // e.g. 00:00 UTC converted to UTC-8 is 20:00 the day before. - // To account for this, we pick a time of day that can't possibly result - // in a different day when converted to local time. - QDateTime firstDayOfMonthDateTime(firstDayOfMonthDate, QTime(0, 0), QTimeZone(QTimeZone::UTC)); - const int localTimeOffsetFromUtc = QDateTime(firstDayOfMonthDate, QTime(0, 0), QTimeZone(QTimeZone::LocalTime)).offsetFromUtc(); - const int timeOffsetAdjustment = localTimeOffsetFromUtc * -1; - firstDayOfMonthDateTime.setSecsSinceEpoch(firstDayOfMonthDateTime.toSecsSinceEpoch() + timeOffsetAdjustment); + // e.g. 00:00 UTC converted to UTC-8 is 16:00 the day before. + // To account for this, we store our dates as local QDateTimes. + const QDateTime firstDayOfMonthDateTime = firstDayOfMonthDate.startOfDay(); int difference = ((firstDayOfMonthDate.dayOfWeek() - l.firstDayOfWeek()) + 7) % 7; // The first day to display should never be the 1st of the month, as we want some days from // the previous month to be visible. if (difference == 0) difference += 7; - QDateTime firstDateToDisplay = firstDayOfMonthDateTime.addDays(-difference); + const QDateTime firstDateToDisplay = firstDayOfMonthDateTime.addDays(-difference); today = QDate::currentDate(); for (int i = 0; i < daysOnACalendarMonth; ++i) @@ -71,11 +67,7 @@ bool QQuickMonthModelPrivate::populate(int m, int y, const QLocale &l, bool forc q->setTitle(l.standaloneMonthName(m) + QStringLiteral(" ") + QString::number(y)); qCDebug(lcMonthModel) << "populated model for month" << m << "year" << y << "locale" << locale - << "initial firstDayOfMonthDateTime" << QDateTime(firstDayOfMonthDate, QTime(0, 0), QTimeZone(QTimeZone::UTC)) - << "localTimeOffsetFromUtc" << localTimeOffsetFromUtc / 60 / 60 - << "timeOffsetAdjustment" << timeOffsetAdjustment / 60 / 60 - << "firstDayOfMonthDateTime" << firstDayOfMonthDateTime - << "firstDayOfMonthDateTime.toLocalTime()" << firstDayOfMonthDateTime.toLocalTime(); + << "firstDayOfMonthDateTime" << firstDayOfMonthDateTime; return true; } |
