Skip to content

Commit fb7fa29

Browse files
laknollLars Knoll
authored andcommitted
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 <[email protected]>
1 parent 4badb86 commit fb7fa29

File tree

1 file changed

+17
-50
lines changed

1 file changed

+17
-50
lines changed

src/corelib/tools/qdatetime.cpp

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,6 @@ int QDate::daysInYear() const
453453
January 2000 has week number 52 in the year 1999, and 31 December
454454
2002 has week number 1 in the year 2003.
455455
456-
\legalese
457-
Copyright (c) 1989 The Regents of the University of California.
458-
All rights reserved.
459-
460-
Redistribution and use in source and binary forms are permitted
461-
provided that the above copyright notice and this paragraph are
462-
duplicated in all such forms and that any documentation,
463-
advertising materials, and other materials related to such
464-
distribution and use acknowledge that the software was developed
465-
by the University of California, Berkeley. The name of the
466-
University may not be used to endorse or promote products derived
467-
from this software without specific prior written permission.
468-
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
469-
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
470-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
471-
472456
\sa isValid()
473457
*/
474458

@@ -478,46 +462,29 @@ int QDate::weekNumber(int *yearNumber) const
478462
return 0;
479463

480464
int year = QDate::year();
481-
int yday = dayOfYear() - 1;
465+
int yday = dayOfYear();
482466
int wday = dayOfWeek();
483-
if (wday == 7)
484-
wday = 0;
485-
int w;
486-
487-
for (;;) {
488-
int len;
489-
int bot;
490-
int top;
491-
492-
len = isLeapYear(year) ? 366 : 365;
493-
/*
494-
** What yday (-3 ... 3) does
495-
** the ISO year begin on?
496-
*/
497-
bot = ((yday + 11 - wday) % 7) - 3;
498-
/*
499-
** What yday does the NEXT
500-
** ISO year begin on?
501-
*/
502-
top = bot - (len % 7);
503-
if (top < -3)
504-
top += 7;
505-
top += len;
506-
if (yday >= top) {
467+
468+
int week = (yday - wday + 10) / 7;
469+
470+
if (week == 0) {
471+
// last week of previous year
472+
--year;
473+
week = (yday + 365 + (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7;
474+
Q_ASSERT(week == 52 || week == 53);
475+
} else if (week == 53) {
476+
// maybe first week of next year
477+
int w = (yday - 365 - (QDate::isLeapYear(year + 1) ? 1 : 0) - wday + 10) / 7;
478+
if (w > 0) {
507479
++year;
508-
w = 1;
509-
break;
480+
week = w;
510481
}
511-
if (yday >= bot) {
512-
w = 1 + ((yday - bot) / 7);
513-
break;
514-
}
515-
--year;
516-
yday += isLeapYear(year) ? 366 : 365;
482+
Q_ASSERT(week == 53 || week == 1);
517483
}
484+
518485
if (yearNumber != 0)
519486
*yearNumber = year;
520-
return w;
487+
return week;
521488
}
522489

523490
#ifndef QT_NO_TEXTDATE

0 commit comments

Comments
 (0)