summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@theqtcompany.com>2015-02-27 12:17:16 (GMT)
committerLars Knoll <lars.knoll@digia.com>2015-03-11 11:02:58 (GMT)
commitfb7fa2918b558381dd98aff3d3250786300d8a3b (patch)
treee53cbfb3bcc3c63b51dcfc686088944e9e3cca3b /src
parent4badb867bbffbd66762c150258b37b6c0b7e3c22 (diff)
downloadQt-fb7fa2918b558381dd98aff3d3250786300d8a3b.zip
Qt-fb7fa2918b558381dd98aff3d3250786300d8a3b.tar.gz
Qt-fb7fa2918b558381dd98aff3d3250786300d8a3b.tar.bz2
Simplify calculation of week number
This also removes a dependency to 3rd party licensed code. Backport of a29b7635bd1d58b29fca96bd3e7831d0ee1f6666 in Qt 5. Change-Id: I647457d7787eed6d5bfc31de4816e68a9f236239 Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qdatetime.cpp67
1 files changed, 17 insertions, 50 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 375d25e..5869b71 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -453,22 +453,6 @@ int QDate::daysInYear() const
January 2000 has week number 52 in the year 1999, and 31 December
2002 has week number 1 in the year 2003.
- \legalese
- Copyright (c) 1989 The Regents of the University of California.
- All rights reserved.
-
- Redistribution and use in source and binary forms are permitted
- provided that the above copyright notice and this paragraph are
- duplicated in all such forms and that any documentation,
- advertising materials, and other materials related to such
- distribution and use acknowledge that the software was developed
- by the University of California, Berkeley. The name of the
- University may not be used to endorse or promote products derived
- from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
-
\sa isValid()
*/
@@ -478,46 +462,29 @@ int QDate::weekNumber(int *yearNumber) const
return 0;
int year = QDate::year();
- int yday = dayOfYear() - 1;
+ int yday = dayOfYear();
int wday = dayOfWeek();
- if (wday == 7)
- wday = 0;
- int w;
-
- for (;;) {
- int len;
- int bot;
- int top;
-
- len = isLeapYear(year) ? 366 : 365;
- /*
- ** What yday (-3 ... 3) does
- ** the ISO year begin on?
- */
- bot = ((yday + 11 - wday) % 7) - 3;
- /*
- ** What yday does the NEXT
- ** ISO year begin on?
- */
- top = bot - (len % 7);
- if (top < -3)
- top += 7;
- top += len;
- if (yday >= top) {
+
+ int week = (yday - wday + 10) / 7;
+
+ if (week == 0) {
+ // last week of previous year
+ --year;
+ week = (yday + 365 + (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7;
+ Q_ASSERT(week == 52 || week == 53);
+ } else if (week == 53) {
+ // maybe first week of next year
+ int w = (yday - 365 - (QDate::isLeapYear(year + 1) ? 1 : 0) - wday + 10) / 7;
+ if (w > 0) {
++year;
- w = 1;
- break;
+ week = w;
}
- if (yday >= bot) {
- w = 1 + ((yday - bot) / 7);
- break;
- }
- --year;
- yday += isLeapYear(year) ? 366 : 365;
+ Q_ASSERT(week == 53 || week == 1);
}
+
if (yearNumber != 0)
*yearNumber = year;
- return w;
+ return week;
}
#ifndef QT_NO_TEXTDATE