From 5af6b59b6e7d1b88de219570011ea482603ea684 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 24 Feb 2021 13:27:15 +0100 Subject: [PATCH] Rework an assertion to eliminate a common sub-expression using a lambda MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This take more lines but makes the condition clearer and the lines shorter, even after converting to use the names for constants in the condition. Change-Id: I9e5b7b79ff62095ed11b8723be238444fd32d9c1 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 9e9f0912a1..6818837e8c 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -3203,14 +3203,15 @@ inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QT // Get the effective data from QTimeZone QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(hint)); Q_ASSERT(zone.d->offsetFromUtc(data.atMSecsSinceEpoch) == data.offsetFromUtc); - Q_ASSERT((zoneMSecs - data.atMSecsSinceEpoch) / 1000 == data.offsetFromUtc - // If zoneMSecs fell in a spring-forward's gap, we get this instead: - || (zoneMSecs - data.atMSecsSinceEpoch) / 1000 == data.standardTimeOffset - // If we have a second DST, like in Europe/Berlin 1947 (mid-summer time). - // If zoneMSecs fell in a gap at beginning of mid-summer time, we get this instead: - || (zoneMSecs - data.atMSecsSinceEpoch) / 1000 == 2 * data.standardTimeOffset - // If it fell in a skipped day (Pacific date-line crossings), this happens: - || (data.offsetFromUtc - (zoneMSecs - data.atMSecsSinceEpoch) / 1000) % 86400 == 0); + Q_ASSERT(([data](qint64 offset) { + return offset == data.offsetFromUtc + // When zoneMSecs falls in a spring-forward's gap: + || offset == data.standardTimeOffset + // When it falls in the gap leading into double-DST: + || offset == 2 * data.standardTimeOffset + // When it falls in a skipped day (Pacific date-line crossings): + || (data.offsetFromUtc - offset) % SECS_PER_DAY == 0; + })((zoneMSecs - data.atMSecsSinceEpoch) / MSECS_PER_SEC)); // Docs state any time before 1970-01-01 will *not* have any DST applied // but all affected times afterwards will have DST applied. if (data.atMSecsSinceEpoch < 0) {