]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/datetime.cpp
don't crash if conversion of a command line argument to Unicode fails
[wxWidgets.git] / src / common / datetime.cpp
index 12732e144b010de690fd276371e9cb5917e2c8c9..5f7f6931e296a30a0c903ea0d4822c5c6a1ec3cb 100644 (file)
@@ -169,7 +169,7 @@ wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter<wxDateTime> , wxFromStringCon
     #endif
 #endif // !WX_TIMEZONE && !WX_GMTOFF_IN_TM
 
-#if wxUSE_THREADS
+#if (!defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)) && wxUSE_THREADS && !defined(__WINDOWS__)
 static wxMutex timeLock;
 #endif
 
@@ -179,7 +179,7 @@ struct tm *wxLocaltime_r(const time_t* ticks, struct tm* temp)
 #if wxUSE_THREADS && !defined(__WINDOWS__)
   // No need to waste time with a mutex on windows since it's using
   // thread local storage for localtime anyway.
-  wxMutexLocker(timeLock);
+  wxMutexLocker locker(timeLock);
 #endif
   memcpy(temp, localtime(ticks), sizeof(struct tm));
   return temp;
@@ -192,7 +192,7 @@ struct tm *wxGmtime_r(const time_t* ticks, struct tm* temp)
 #if wxUSE_THREADS && !defined(__WINDOWS__)
   // No need to waste time with a mutex on windows since it's
   // using thread local storage for gmtime anyway.
-  wxMutexLocker(timeLock);
+  wxMutexLocker locker(timeLock);
 #endif
   memcpy(temp, gmtime(ticks), sizeof(struct tm));
   return temp;
@@ -204,13 +204,8 @@ struct tm *wxGmtime_r(const time_t* ticks, struct tm* temp)
 // ----------------------------------------------------------------------------
 
 // debugging helper: just a convenient replacement of wxCHECK()
-#define wxDATETIME_CHECK(expr, msg)     \
-        if ( !(expr) )                  \
-        {                               \
-            wxFAIL_MSG(msg);            \
-            *this = wxInvalidDateTime;  \
-            return *this;               \
-        }
+#define wxDATETIME_CHECK(expr, msg) \
+    wxCHECK2_MSG(expr, *this = wxInvalidDateTime; return *this, msg)
 
 // ----------------------------------------------------------------------------
 // private classes
@@ -1816,6 +1811,7 @@ wxDateTime::SetToWeekOfYear(int year, wxDateTime_t numWeek, WeekDay wd)
     return dt;
 }
 
+#if WXWIN_COMPATIBILITY_2_6
 // use a separate function to avoid warnings about using deprecated
 // SetToTheWeek in GetWeek below
 static wxDateTime
@@ -1853,6 +1849,7 @@ wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek,
 {
     return ::SetToTheWeek(GetYear(), numWeek, weekday, flags);
 }
+#endif // WXWIN_COMPATIBILITY_2_6
 
 wxDateTime& wxDateTime::SetToLastMonthDay(Month month,
                                           int year)
@@ -2407,34 +2404,40 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
                         {
                             nLostWeekDays += year++ % 4 ? 1 : 2;
                         }
-
+                       
+                       // Keep year below 2000 so the 2digit year number
+                       // can never match the month or day of the month
+                       if (year>=2000) year-=28;
                         // at any rate, we couldn't go further than 1988 + 9 + 28!
                         wxASSERT_MSG( year < 2030,
                                       _T("logic error in wxDateTime::Format") );
 
-                        wxString strYear, strYear2;
+                       wxString strYear, strYear2;
                         strYear.Printf(_T("%d"), year);
                         strYear2.Printf(_T("%d"), year % 100);
-
-                        // find two strings not occurring in format (this is surely
+       
+                        // find four strings not occurring in format (this is surely
                         // not the optimal way of doing it... improvements welcome!)
                         wxString fmt2 = format;
-                        wxString replacement = (wxChar)-1;
-                        while ( fmt2.Find(replacement) != wxNOT_FOUND )
-                        {
-                            replacement << (wxChar)-1;
-                        }
-
-                        wxString replacement2 = (wxChar)-2;
-                        while ( fmt2.Find(replacement) != wxNOT_FOUND )
-                        {
-                            replacement << (wxChar)-2;
-                        }
-
+                       wxString replacement,replacement2,replacement3,replacement4;
+                       for (int rnr=1; rnr<5 ; rnr++) {
+                               wxString r = (wxChar)-rnr;
+                               while ( fmt2.Find(r) != wxNOT_FOUND )
+                               {
+                                   r << (wxChar)-rnr;
+                               }
+
+                               switch (rnr) {
+                                       case 1: replacement=r; break;
+                                       case 2: replacement2=r; break;
+                                       case 3: replacement3=r; break;
+                                       case 4: replacement4=r; break;
+                               }
+                       }
                         // replace all occurrences of year with it
                         bool wasReplaced = fmt2.Replace(strYear, replacement) > 0;
-                        if ( !wasReplaced )
-                            wasReplaced = fmt2.Replace(strYear2, replacement2) > 0;
+                        // evaluation order ensures we always attempt the replacement.
+                       wasReplaced = (fmt2.Replace(strYear2, replacement2) > 0) | wasReplaced ;
 
                         // use strftime() to format the same date but in supported
                         // year
@@ -2459,11 +2462,17 @@ wxString wxDateTime::Format(const wxChar *format, const TimeZone& tz) const
                                                     &tmAdjusted);
 
                         // now replace the occurrence of 1999 with the real year
+                       // we do this in two stages to stop the 2 digit year
+                       // matching any substring of the 4 digit year.
+                       // Any day,month hours and minutes components should be safe due
+                       // to ensuring the range of the years.
                         wxString strYearReal, strYearReal2;
                         strYearReal.Printf(_T("%04d"), yearReal);
                         strYearReal2.Printf(_T("%02d"), yearReal % 100);
-                        str.Replace(strYear, strYearReal);
-                        str.Replace(strYear2, strYearReal2);
+                        str.Replace(strYear, replacement3);
+                        str.Replace(strYear2,replacement4);
+                        str.Replace(replacement3, strYearReal);
+                        str.Replace(replacement4, strYearReal2);
 
                         // and replace back all occurrences of replacement string
                         if ( wasReplaced )
@@ -3810,9 +3819,11 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
                 }
                 else // may be either day or year
                 {
+                    // use a leap year if we don't have the year yet to allow
+                    // dates like 2/29/1976 which would be rejected otherwise
                     wxDateTime_t max_days = (wxDateTime_t)(
                         haveMon
-                        ? GetNumOfDaysInMonth(haveYear ? year : Inv_Year, mon)
+                        ? GetNumOfDaysInMonth(haveYear ? year : 1976, mon)
                         : 31
                     );
 
@@ -3965,7 +3976,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
     {
         wxLogDebug(_T("ParseDate: no day, no weekday hence no date."));
 
-        return (wxChar *)NULL;
+        return NULL;
     }
 
     if ( haveWDay && (haveMon || haveYear || haveDay) &&
@@ -3974,7 +3985,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
         // without adjectives (which we don't support here) the week day only
         // makes sense completely separately or with the full date
         // specification (what would "Wed 1999" mean?)
-        return (wxChar *)NULL;
+        return NULL;
     }
 
     if ( !haveWDay && haveYear && !(haveDay && haveMon) )
@@ -4004,7 +4015,7 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
             // if we give the year, month and day must be given too
             wxLogDebug(_T("ParseDate: day and month should be specified if year is."));
 
-            return (wxChar *)NULL;
+            return NULL;
         }
     }
 
@@ -4020,6 +4031,11 @@ const wxChar *wxDateTime::ParseDate(const wxChar *date)
 
     if ( haveDay )
     {
+        // normally we check the day above but the check is optimistic in case
+        // we find the day before its month/year so we have to redo it now
+        if ( day > GetNumOfDaysInMonth(year, mon) )
+            return NULL;
+
         Set(day, mon, year);
 
         if ( haveWDay )