@@ -862,34 +862,28 @@ LibraryManager.library = {
862862 // or more days in the new year, then it is considered week 1.
863863 // Otherwise, it is the last week of the previous year, and the next week is week 1.
864864 // Both January 4th and the first Thursday of January are always in week 1. [ tm_year, tm_wday, tm_yday]
865- var janFourthThisYear = new Date ( date . tm_year + 1900 , 0 , 4 ) ;
866- var janFourthNextYear = new Date ( date . tm_year + 1901 , 0 , 4 ) ;
867-
868- var firstWeekStartThisYear = getFirstWeekStartDate ( janFourthThisYear ) ;
869- var firstWeekStartNextYear = getFirstWeekStartDate ( janFourthNextYear ) ;
870-
871- var endDate = __addDays ( new Date ( date . tm_year + 1900 , 0 , 1 ) , date . tm_yday ) ;
872-
873- if ( compareByDay ( endDate , firstWeekStartThisYear ) < 0 ) {
874- // if given date is before this years first week, then it belongs to the 53rd week of last year
875- return '53' ;
865+ var val = Math . floor ( ( date . tm_yday + 7 - ( date . tm_wday + 6 ) % 7 ) / 7 ) ;
866+ // If 1 Jan is just 1-3 days past Monday, the previous week
867+ // is also in this year.
868+ if ( ( date . tm_wday + 371 - date . tm_yday - 2 ) % 7 <= 2 ) {
869+ val ++ ;
876870 }
877-
878- if ( compareByDay ( firstWeekStartNextYear , endDate ) <= 0 ) {
879- // if given date is after next years first week, then it belongs to the 01th week of next year
880- return '01' ;
881- }
882-
883- // given date is in between CW 01..53 of this calendar year
884- var daysDifference ;
885- if ( firstWeekStartThisYear . getFullYear ( ) < date . tm_year + 1900 ) {
886- // first CW of this year starts last year
887- daysDifference = date . tm_yday + 32 - firstWeekStartThisYear . getDate ( )
888- } else {
889- // first CW of this year starts this year
890- daysDifference = date . tm_yday + 1 - firstWeekStartThisYear . getDate ( ) ;
871+ if ( ! val ) {
872+ val = 52 ;
873+ // If 31 December of prev year a Thursday, or Friday of a
874+ // leap year, then the prev year has 53 weeks.
875+ var dec31 = ( date . tm_wday + 7 - date . tm_yday - 1 ) % 7 ;
876+ if ( dec31 == 4 || ( dec31 == 5 && __isLeapYear ( date . tm_year % 400 - 1 ) ) ) {
877+ val ++ ;
878+ }
879+ } else if ( val == 53 ) {
880+ // If 1 January is not a Thursday, and not a Wednesday of a
881+ // leap year, then this year has only 52 weeks.
882+ var jan1 = ( date . tm_wday + 371 - date . tm_yday ) % 7 ;
883+ if ( jan1 != 4 && ( jan1 != 3 || ! __isLeapYear ( date . tm_year ) ) )
884+ val = 1 ;
891885 }
892- return leadingNulls ( Math . ceil ( daysDifference / 7 ) , 2 ) ;
886+ return leadingNulls ( val , 2 ) ;
893887 } ,
894888 '%w' : function ( date ) {
895889 return date . tm_wday ;
0 commit comments