Fix wxDateTime::GetWeekOfYear() for the days in the last week of the year.

The code took into account the possibility that the days in the beginning of
the year might belong to the last week of the previous year but not that the
days at the end of the year could belong to the first week of the next year.

Closes #14973.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73402 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2013-01-20 02:09:48 +00:00
parent 09ca8913fa
commit 12ce0a7402
3 changed files with 22 additions and 15 deletions

View File

@@ -547,6 +547,7 @@ All:
- Add separate read/written bytes counters and per-direction NOWAIT and WAITALL
flags to wxSocket (Rob Bresalier).
- Add wxDir::Close() method (Silverstorm82).
- Fix wxDateTime::GetWeekOfYear() for the last week of year (aimo).
- Fix compilation of wxHash{Map,Set} with g++ 4.7 (Nathan Ridge).
- Fix posting large amounts of data in wxHTTP (Platonides).
- Add wxFile::ReadAll() for consistency with wxFFile.

View File

@@ -1921,7 +1921,6 @@ wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const
{
// adjust the weekdays to non-US style.
wdYearStart = ConvertWeekDayToMondayBase(wdYearStart);
wdTarget = ConvertWeekDayToMondayBase(wdTarget);
// quoting from http://www.cl.cam.ac.uk/~mgk25/iso-time.html:
//
@@ -1937,22 +1936,24 @@ wxDateTime::GetWeekOfYear(wxDateTime::WeekFlags flags, const TimeZone& tz) const
//
// 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;
int dayCountFix = wdYearStart < 4 ? 6 : -1;
// count the number of week
week = (nDayInYear + wdYearStart + dayCountFix) / DAYS_PER_WEEK;
// 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 )
if ( week == 0 )
{
week = wxDateTime(31, Dec, GetYear() - 1).GetWeekOfYear();
else
week = (nDayInYear + wdYearStart - 1 - wdTarget)/7;
}
else if ( week == 53 )
{
int wdYearEnd = (wdYearStart + 364 + IsLeapYear(GetYear()))
% DAYS_PER_WEEK;
// Week 53 only if last day of year is Thursday or later.
if ( wdYearEnd < 3 )
week = 1;
}
}

View File

@@ -537,6 +537,11 @@ for n in range(20):
{ { 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 },
{ { 31, wxDateTime::Dec, 2009, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 53, 5, 5, 365 },
{ { 31, wxDateTime::Dec, 2012, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 6, 366 },
{ { 29, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 52, 5, 5, 363 },
{ { 30, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 5, 364 },
{ { 31, wxDateTime::Dec, 2013, 0, 0, 0, 0.0, wxDateTime::Inv_WeekDay, 0 }, 1, 6, 5, 365 },
};
for ( size_t n = 0; n < WXSIZEOF(weekNumberTestDates); n++ )