From ad554707dc1e564fa0ac0f164ed093ddc80f14b0 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 27 May 2021 16:30:35 +0200 Subject: [PATCH] Simplify QDate::weekNumber() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Eliminate two local variables and don't even compute the year if we don't need it. Change-Id: If968c619750cead317641885a0fb9b9974954782 Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim --- src/corelib/time/qdatetime.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 6b6674dccea..94ea0112d4e 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -735,14 +735,12 @@ int QDate::weekNumber(int *yearNumber) const // This could be replaced by use of QIso8601Calendar, once we implement it. // The Thursday of the same week determines our answer: - QDate thursday(addDays(4 - dayOfWeek())); - int year = thursday.year(); - // Week n's Thurs's DOY has 1 <= DOY - 7*(n-1) < 8, so 0 <= DOY + 6 - 7*n < 7: - int week = (thursday.dayOfYear() + 6) / 7; - + const QDate thursday(addDays(4 - dayOfWeek())); if (yearNumber) - *yearNumber = year; - return week; + *yearNumber = thursday.year(); + + // Week n's Thurs's DOY has 1 <= DOY - 7*(n-1) < 8, so 0 <= DOY + 6 - 7*n < 7: + return (thursday.dayOfYear() + 6) / 7; } static bool inDateTimeRange(qint64 jd, bool start)