- // FIXME some more elegant way??
- WeekDay wdYearStart = wxDateTime(1, Jan, GetYear()).GetWeekDay();
- if ( wdYearStart == Wed || wdYearStart == Thu )
- {
- week++;
+ // quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html:
+ //
+ // Week 01 of a year is per definition the first week that has the
+ // Thursday in this year, which is equivalent to the week that
+ // contains the fourth day of January. In other words, the first
+ // week of a new year is the week that has the majority of its
+ // days in the new year. Week 01 might also contain days from the
+ // previous year and the week before week 01 of a year is the last
+ // week (52 or 53) of the previous year even if it contains days
+ // from the new year. A week starts with Monday (day 1) and ends
+ // with Sunday (day 7).
+ //
+
+ // if Jan 1 is Thursday or less, it is in the first week of this year
+ if ( wdYearStart < 4 )
+ {
+ // count the number of entire weeks between Jan 1 and this date
+ week = (nDayInYear + wdYearStart + 6 - wdTarget)/7;
+
+ // be careful to check for overflow in the next year
+ if ( week == 53 && tm.mday - wdTarget > 28 )
+ week = 1;
+ }
+ else // Jan 1 is in the last week of the previous year
+ {
+ // check if we happen to be at the last week of previous year:
+ if ( tm.mon == Jan && tm.mday < 8 - wdYearStart )
+ week = wxDateTime(31, Dec, GetYear()-1).GetWeekOfYear();
+ else
+ week = (nDayInYear + wdYearStart - 1 - wdTarget)/7;
+ }