From aafefc094d1ef8419893805e64a8e1296889d392 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 15 Feb 2022 10:19:52 +0100 Subject: [PATCH] Extract functions: move some code to TransitionTimePair Documenting the case where a year starts in DST belongs on the pair whose job is to work that out. The selection of which transition to report based on an isDst flag could be said once instead of thrice if made into a method there, too. Change-Id: Ice59bb8594097c53bc2fefe1706434462b225274 Reviewed-by: Marc Mutz --- src/corelib/time/qtimezoneprivate_p.h | 7 +++--- src/corelib/time/qtimezoneprivate_win.cpp | 29 ++++++++++++++--------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/corelib/time/qtimezoneprivate_p.h b/src/corelib/time/qtimezoneprivate_p.h index 484bbe56eb..a436c87999 100644 --- a/src/corelib/time/qtimezoneprivate_p.h +++ b/src/corelib/time/qtimezoneprivate_p.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Copyright (C) 2013 John Layt ** Contact: https://www.qt.io/licensing/ ** @@ -455,10 +455,11 @@ public: QList availableTimeZoneIds() const override; -private: - void init(const QByteArray &ianaId); + // For use within implementation's TransitionTimePair: QTimeZonePrivate::Data ruleToData(const QWinTransitionRule &rule, qint64 atMSecsSinceEpoch, QTimeZone::TimeType type, bool fakeDst = false) const; +private: + void init(const QByteArray &ianaId); QByteArray m_windowsId; QString m_displayName; diff --git a/src/corelib/time/qtimezoneprivate_win.cpp b/src/corelib/time/qtimezoneprivate_win.cpp index eda46f8b9b..9f65ca8d01 100644 --- a/src/corelib/time/qtimezoneprivate_win.cpp +++ b/src/corelib/time/qtimezoneprivate_win.cpp @@ -388,6 +388,21 @@ struct TransitionTimePair return std == QTimeZonePrivate::invalidMSecs() || dst == QTimeZonePrivate::invalidMSecs(); } + + bool startsInDst() const + { + // Year starts in daylightTimeRule iff it has a valid transition out of + // DST before it has a transition into it. + return std != QTimeZonePrivate::invalidMSecs() && std < dst; + } + + QTimeZonePrivate::Data ruleToData(const QWinTimeZonePrivate::QWinTransitionRule &rule, + const QWinTimeZonePrivate *tzp, bool isDst) const + { + if (isDst) + return tzp->ruleToData(rule, dst, QTimeZone::DaylightTime, fakesDst()); + return tzp->ruleToData(rule, std, QTimeZone::StandardTime, fakesDst()); + } }; int yearEndOffset(const QWinTimeZonePrivate::QWinTransitionRule &rule, int year) @@ -676,11 +691,7 @@ QTimeZonePrivate::Data QWinTimeZonePrivate::nextTransition(qint64 afterMSecsSinc // Find first transition in this first rule. // Initial guess: first rule starts in standard time. TransitionTimePair pair(rule, rule.startYear, rule.standardTimeBias); - // Year starts in daylightTimeRule iff it has a valid transition - // out of DST before it has a transition into it. - if (pair.std != invalidMSecs() && pair.std < pair.dst) - return ruleToData(rule, pair.dst, QTimeZone::DaylightTime, pair.fakesDst()); - return ruleToData(rule, pair.std, QTimeZone::StandardTime, pair.fakesDst()); + return pair.ruleToData(rule, this, pair.startsInDst()); } const int endYear = ruleIndex + 1 < m_tranRules.count() ? qMin(m_tranRules.at(ruleIndex + 1).startYear, year + 2) : (year + 2); @@ -706,9 +717,7 @@ QTimeZonePrivate::Data QWinTimeZonePrivate::nextTransition(qint64 afterMSecsSinc continue; } - if (isDst) - return ruleToData(rule, pair.dst, QTimeZone::DaylightTime, pair.fakesDst()); - return ruleToData(rule, pair.std, QTimeZone::StandardTime, pair.fakesDst()); + return pair.ruleToData(rule, this, isDst); } // Fell off end of rule, try next rule. } // else: no transition during rule's period @@ -746,9 +755,7 @@ QTimeZonePrivate::Data QWinTimeZonePrivate::previousTransition(qint64 beforeMSec prior = year == 1 ? -1 : year - 1; // No year 0. continue; } - if (isDst) - return ruleToData(rule, pair.dst, QTimeZone::DaylightTime, pair.fakesDst()); - return ruleToData(rule, pair.std, QTimeZone::StandardTime, pair.fakesDst()); + return pair.ruleToData(rule, this, isDst); } // Fell off start of rule, try previous rule. } else if (ruleIndex == 0) {