diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index e20225d593..3f036e61da 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -2054,16 +2054,22 @@ wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags, const TimeZone& tz) const { Tm tm = GetTm(tz); - wxDateTime dtMonthStart = wxDateTime(1, tm.mon, tm.year); - int nWeek = GetWeekOfYear(flags) - dtMonthStart.GetWeekOfYear(flags) + 1; - if ( nWeek < 0 ) + const wxDateTime dateFirst = wxDateTime(1, tm.mon, tm.year); + const wxDateTime::WeekDay wdFirst = dateFirst.GetWeekDay(); + + if ( flags == Default_First ) { - // this may happen for January when Jan, 1 is the last week of the - // previous year - nWeek += IsLeapYear(tm.year - 1) ? 53 : 52; + flags = GetCountry() == USA ? Sunday_First : Monday_First; } - return (wxDateTime::wxDateTime_t)nWeek; + // compute offset of dateFirst from the beginning of the week + int firstOffset; + if ( flags == Sunday_First ) + firstOffset = wdFirst - Sun; + else + firstOffset = wdFirst == Sun ? DAYS_PER_WEEK - 1 : wdFirst - Mon; + + return (wxDateTime::wxDateTime_t)((tm.mday - 1 + firstOffset)/7 + 1); } wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday) diff --git a/tests/datetime/datetimetest.cpp b/tests/datetime/datetimetest.cpp index 6cddf64142..4731265645 100644 --- a/tests/datetime/datetimetest.cpp +++ b/tests/datetime/datetimetest.cpp @@ -555,6 +555,8 @@ for n in range(20): { { 16, wxDateTime::Oct, 1942, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 42, 3, 3, 289 }, { { 30, wxDateTime::Dec, 2003, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 5, 5, 364 }, { { 2, wxDateTime::Jan, 2004, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 1, 1, 2 }, + { { 5, wxDateTime::Jan, 2010, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 5 }, + { { 3, wxDateTime::Jan, 2011, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 2, 2, 3 }, }; for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ ) @@ -570,10 +572,14 @@ for n in range(20): wmon2 = dt.GetWeekOfMonth(wxDateTime::Sunday_First), dnum = dt.GetDayOfYear(); - CPPUNIT_ASSERT_EQUAL( wn.dnum, dnum ); - CPPUNIT_ASSERT_EQUAL( wn.wmon, wmon ); - CPPUNIT_ASSERT_EQUAL( wn.wmon2, wmon2 ); - CPPUNIT_ASSERT_EQUAL( wn.week, week ); + WX_ASSERT_EQUAL_MESSAGE( ("day of year for %s", d.Format()), + wn.dnum, dnum ); + WX_ASSERT_EQUAL_MESSAGE( ("week of month (Monday) for %s", d.Format()), + wn.wmon, wmon ); + WX_ASSERT_EQUAL_MESSAGE( ("week of month (Sunday) for %s", d.Format()), + wn.wmon2, wmon2 ); + WX_ASSERT_EQUAL_MESSAGE( ("week of year for %s", d.Format()), + wn.week, week ); int year = d.year; if ( week == 1 && d.month != wxDateTime::Jan )