Simplify calculation of week number
This also removes a dependency to 3rd party licensed code. Change-Id: Ia4818a5cf306501bdb7192265edc4bcba8e597d8 Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
This commit is contained in:
parent
de92efd448
commit
a29b7635bd
@ -560,22 +560,6 @@ int QDate::daysInYear() const
|
|||||||
January 2000 has week number 52 in the year 1999, and 31 December
|
January 2000 has week number 52 in the year 1999, and 31 December
|
||||||
2002 has week number 1 in the year 2003.
|
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()
|
\sa isValid()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -585,46 +569,29 @@ int QDate::weekNumber(int *yearNumber) const
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int year = QDate::year();
|
int year = QDate::year();
|
||||||
int yday = dayOfYear() - 1;
|
int yday = dayOfYear();
|
||||||
int wday = dayOfWeek();
|
int wday = dayOfWeek();
|
||||||
if (wday == 7)
|
|
||||||
wday = 0;
|
|
||||||
int w;
|
|
||||||
|
|
||||||
for (;;) {
|
int week = (yday - wday + 10) / 7;
|
||||||
int len;
|
|
||||||
int bot;
|
|
||||||
int top;
|
|
||||||
|
|
||||||
len = isLeapYear(year) ? 366 : 365;
|
if (week == 0) {
|
||||||
/*
|
// last week of previous year
|
||||||
** 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) {
|
|
||||||
++year;
|
|
||||||
w = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (yday >= bot) {
|
|
||||||
w = 1 + ((yday - bot) / 7);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
--year;
|
--year;
|
||||||
yday += isLeapYear(year) ? 366 : 365;
|
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;
|
||||||
|
week = w;
|
||||||
|
}
|
||||||
|
Q_ASSERT(week == 53 || week == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (yearNumber != 0)
|
if (yearNumber != 0)
|
||||||
*yearNumber = year;
|
*yearNumber = year;
|
||||||
return w;
|
return week;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef QT_NO_TEXTDATE
|
#ifndef QT_NO_TEXTDATE
|
||||||
|
Loading…
Reference in New Issue
Block a user