]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/datetime.cpp
fix (harmless for now) g++ warning about non-virtual dtor
[wxWidgets.git] / src / common / datetime.cpp
index f6ad3f6259ab5fd94d548c220cdf7d9029b301f6..1547b4a14e14bc0c957be8820626b8e0a9f14531 100644 (file)
@@ -381,7 +381,8 @@ extern const char *wxDumpDate(const wxDateTime* dt)
     static char buf[128];
 
     wxString fmt(dt->Format("%Y-%m-%d (%a) %H:%M:%S"));
-    wxStrncpy(buf, fmt + " (" + dt->GetValue().ToString() + " ticks)",
+    wxStrlcpy(buf,
+              (fmt + " (" + dt->GetValue().ToString() + " ticks)").ToAscii(),
               WXSIZEOF(buf));
 
     return buf;
@@ -487,8 +488,10 @@ static wxString CallStrftime(const wxString& format, const tm* tm)
 
     if ( !wxStrftime(buf, WXSIZEOF(buf), format, tm) )
     {
-        // buffer is too small?
+        // if the format is valid, buffer must be too small?
         wxFAIL_MSG(_T("strftime() failed"));
+
+        buf[0] = '\0';
     }
 
     s = buf;
@@ -1270,6 +1273,17 @@ wxDateTime wxDateTime::GetBeginDST(int year, Country country)
                             wxFAIL_MSG( _T("no first Sunday in April?") );
                         }
                     }
+                    else if ( year > 2006 )
+                    // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005).
+                    // Starting in 2007, daylight time begins in the United States on the
+                    // second Sunday in March and ends on the first Sunday in November
+                    {
+                        if ( !dt.SetToWeekDay(Sun, 2, Mar, year) )
+                        {
+                            // weird...
+                            wxFAIL_MSG( _T("no second Sunday in March?") );
+                        }
+                    }
                     else
                     {
                         if ( !dt.SetToWeekDay(Sun, 1, Apr, year) )
@@ -1346,21 +1360,36 @@ wxDateTime wxDateTime::GetEndDST(int year, Country country)
                     dt.Set(30, Sep, year);
                     break;
 
-                default:
-                    // DST ends at 2 a.m. on the last Sunday of October
-                    if ( !dt.SetToLastWeekDay(Sun, Oct, year) )
+                default: // default for switch (year)
+                    if ( year > 2006 )
+                      // Energy Policy Act of 2005, Pub. L. no. 109-58, 119 Stat 594 (2005).
+                      // Starting in 2007, daylight time begins in the United States on the
+                      // second Sunday in March and ends on the first Sunday in November
                     {
-                        // weirder and weirder...
-                        wxFAIL_MSG( _T("no last Sunday in October?") );
+                        if ( !dt.SetToWeekDay(Sun, 1, Nov, year) )
+                        {
+                            // weird...
+                            wxFAIL_MSG( _T("no first Sunday in November?") );
+                        }
+                    }
+                    else
+                     // pre-2007
+                     // DST ends at 2 a.m. on the last Sunday of October
+                    {
+                        if ( !dt.SetToLastWeekDay(Sun, Oct, year) )
+                        {
+                            // weirder and weirder...
+                            wxFAIL_MSG( _T("no last Sunday in October?") );
+                        }
                     }
 
                     dt += wxTimeSpan::Hours(2);
 
-                    // TODO what about timezone??
+            // TODO: what about timezone??
             }
             break;
 
-        default:
+        default: // default for switch (country)
             // assume October 26th as the end of the DST - totally bogus too
             dt.Set(26, Oct, year);
     }
@@ -3665,6 +3694,11 @@ wxDateTime::ParseFormat(const wxString& date,
     Tm tm = tmDef;
 
     // set the date
+    if ( haveMon )
+    {
+        tm.mon = mon;
+    }
+
     if ( haveYear )
     {
         tm.year = year;
@@ -3673,16 +3707,15 @@ wxDateTime::ParseFormat(const wxString& date,
     // TODO we don't check here that the values are consistent, if both year
     //      day and month/day were found, we just ignore the year day and we
     //      also always ignore the week day
-    if ( haveMon && haveDay )
+    if ( haveDay )
     {
-        if ( mday > GetNumOfDaysInMonth(tm.year, mon) )
+        if ( mday > GetNumOfDaysInMonth(tm.year, tm.mon) )
         {
             wxLogDebug(_T("bad month day in wxDateTime::ParseFormat"));
 
             return NULL;
         }
 
-        tm.mon = mon;
         tm.mday = mday;
     }
     else if ( haveYDay )
@@ -4267,6 +4300,14 @@ enum TimeSpanPart
 //  %l          milliseconds (000 - 999)
 wxString wxTimeSpan::Format(const wxString& format) const
 {
+    // we deal with only positive time spans here and just add the sign in
+    // front for the negative ones
+    if ( IsNegative() )
+    {
+        wxString str(Negate().Format(format));
+        return "-" + str;
+    }
+
     wxCHECK_MSG( !format.empty(), wxEmptyString,
                  _T("NULL format in wxTimeSpan::Format") );
 
@@ -4338,13 +4379,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
                     n = GetHours();
                     if ( partBiggest < Part_Hour )
                     {
-                        if ( n < 0 )
-                        {
-                            // the sign has already been taken into account
-                            // when outputting the biggest part
-                            n = -n;
-                        }
-
                         n %= HOURS_PER_DAY;
                     }
                     else
@@ -4359,9 +4393,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
                     n = GetMilliseconds().ToLong();
                     if ( partBiggest < Part_MSec )
                     {
-                        if ( n < 0 )
-                            n = -n;
-
                         n %= 1000;
                     }
                     //else: no need to reset partBiggest to Part_MSec, it is
@@ -4374,9 +4405,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
                     n = GetMinutes();
                     if ( partBiggest < Part_Min )
                     {
-                        if ( n < 0 )
-                            n = -n;
-
                         n %= MIN_PER_HOUR;
                     }
                     else
@@ -4391,9 +4419,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
                     n = GetSeconds().ToLong();
                     if ( partBiggest < Part_Sec )
                     {
-                        if ( n < 0 )
-                            n = -n;
-
                         n %= SEC_PER_MIN;
                     }
                     else
@@ -4407,10 +4432,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
 
             if ( digits )
             {
-                // negative numbers need one extra position for '-' display
-                if ( n < 0 )
-                    digits++;
-
                 fmtPrefix << _T("0") << digits;
             }
 
@@ -4596,7 +4617,7 @@ WXDLLIMPEXP_BASE void wxPrevWDay(wxDateTime::WeekDay& wd)
 wxDateTime& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME& st)
 {
     return Set(st.wDay,
-            wx_static_cast(wxDateTime::Month, wxDateTime::Jan + st.wMonth - 1),
+            static_cast<wxDateTime::Month>(wxDateTime::Jan + st.wMonth - 1),
             st.wYear,
             0, 0, 0);
 }