summaryrefslogtreecommitdiff
path: root/src/corelib/tools/qdatetime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qdatetime.cpp')
-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 375d25edc3..5869b71038 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