]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix wxDateTime::GetWeekOfYear() for the days in the last week of the year.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 20 Jan 2013 02:09:48 +0000 (02:09 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 20 Jan 2013 02:09:48 +0000 (02:09 +0000)
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

docs/changes.txt
src/common/datetime.cpp
tests/datetime/datetimetest.cpp

index 8d2f3fc5cfc227da2c8bc469744756f39fee6c85..4b2afe56a7da8be32e1f5ef1ae072f2058626663 100644 (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.
index 05ceb27e0d25f3b9da9e49d808afd9aefb6659bd..6ea7a0a37a3a01f0845483387a4333c2a5c3adcf 100644 (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;
+        // check if we happen to be at the last week of previous year:
+        if ( week == 0 )
+        {
+            week = wxDateTime(31, Dec, GetYear() - 1).GetWeekOfYear();
         }
-        else // Jan 1 is in the last week of the previous year
+        else if ( week == 53 )
         {
-            // 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;
+            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;
         }
     }
 
index e317c4707de1b0ee4313559e646552d2eb2482ad..39ef9387a0f07a551d4ae18dc86a7706921f2e65 100644 (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++ )