@@ -453,22 +453,6 @@ int QDate::daysInYear() const
453
453
January 2000 has week number 52 in the year 1999, and 31 December
454
454
2002 has week number 1 in the year 2003.
455
455
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
-
472
456
\sa isValid()
473
457
*/
474
458
@@ -478,46 +462,29 @@ int QDate::weekNumber(int *yearNumber) const
478
462
return 0 ;
479
463
480
464
int year = QDate::year ();
481
- int yday = dayOfYear () - 1 ;
465
+ int yday = dayOfYear ();
482
466
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 ) {
507
479
++year;
508
- w = 1 ;
509
- break ;
480
+ week = w;
510
481
}
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 );
517
483
}
484
+
518
485
if (yearNumber != 0 )
519
486
*yearNumber = year;
520
- return w ;
487
+ return week ;
521
488
}
522
489
523
490
#ifndef QT_NO_TEXTDATE
0 commit comments