From 981b3336f6ed7aed92e9b9d0fd50824340be5d61 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 25 Aug 2022 14:07:25 +0200 Subject: [PATCH] Simplify one of the gap cases in QTimeZonePrivate::dataForLocalTime() The code computed the transition gap and subtracted it from one side's proposed UTC time, or added it to the other's; the effect was the same as swapping these two values. Doing that overtly as a swap simplifies the remainder of the code. Clarified the accompanying comment in the process. Change-Id: I00b8d2bb98ea08b6edd11e01d05a091cb39f3511 Reviewed-by: Thiago Macieira --- src/corelib/time/qtimezoneprivate.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/corelib/time/qtimezoneprivate.cpp b/src/corelib/time/qtimezoneprivate.cpp index b04f0f384f..74419e66c0 100644 --- a/src/corelib/time/qtimezoneprivate.cpp +++ b/src/corelib/time/qtimezoneprivate.cpp @@ -340,18 +340,12 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs, /* Neither is valid (e.g. in a spring-forward's gap) and - nextTran.atMSecsSinceEpoch < nextStart <= tran.atMSecsSinceEpoch, so - 0 < tran.atMSecsSinceEpoch - nextTran.atMSecsSinceEpoch - = (nextTran.offsetFromUtc - tran.offsetFromUtc) * 1000 + nextTran.atMSecsSinceEpoch < nextStart <= tran.atMSecsSinceEpoch; + swap their atMSecsSinceEpoch to give each a moment on its side of + the transition; and pick the reverse of what hint asked for: */ - int dstStep = (nextTran.offsetFromUtc - tran.offsetFromUtc) * 1000; - Q_ASSERT(dstStep > 0); // How else could we get here ? - if (nextFirst) { // hint thought we needed nextTran, so use tran - tran.atMSecsSinceEpoch -= dstStep; - return tran; - } - nextTran.atMSecsSinceEpoch += dstStep; - return nextTran; + std::swap(tran.atMSecsSinceEpoch, nextTran.atMSecsSinceEpoch); + return nextFirst ? tran : nextTran; } // Before first transition, or system has transitions but not for this zone. // Try falling back to offsetFromUtc (works for before first transition, at least).