]> git.saurik.com Git - wxWidgets.git/commitdiff
handle negative time spans correctly in Format()
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 20 Jul 2006 15:04:10 +0000 (15:04 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 20 Jul 2006 15:04:10 +0000 (15:04 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40205 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

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

index a63f0a833dfc79744a241f50ba7f93e6bc823230..9efa044396a672617bc899b34270f52045d1d511 100644 (file)
@@ -86,6 +86,7 @@ All:
 - Make wx-config compatible with Bourne shells.
 - Fixed wxDb::Open(wxDbConnectInf) when using connection string (Hellwolf Misty)
 - Fixed crash in wxDb::Open() in Unicode build (Massimiliano Marretta)
+- Fixed wxTimeSpan::Format() for negative time spans
 
 All (GUI):
 
index b27c1010bb651f8ae429539d718d1bf40ac06d33..7b85eea106982b99579e7e227744cec2582d3807 100644 (file)
@@ -4234,11 +4234,14 @@ wxString wxTimeSpan::Format(const wxChar *format) const
         if ( ch == _T('%') )
         {
             // the start of the format specification of the printf() below
-            wxString fmtPrefix = _T('%');
+            wxString fmtPrefix(_T('%'));
 
             // the number
             long n;
 
+            // the number of digits for the format string, 0 if unused
+            unsigned digits = 0;
+
             ch = *++pch;    // get the format spec char
             switch ( ch )
             {
@@ -4273,6 +4276,13 @@ wxString wxTimeSpan::Format(const wxChar *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
@@ -4280,25 +4290,31 @@ wxString wxTimeSpan::Format(const wxChar *format) const
                         partBiggest = Part_Hour;
                     }
 
-                    fmtPrefix += _T("02");
+                    digits = 2;
                     break;
 
                 case _T('l'):
                     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
                     //      the least significant one anyhow
 
-                    fmtPrefix += _T("03");
+                    digits = 3;
                     break;
 
                 case _T('M'):
                     n = GetMinutes();
                     if ( partBiggest < Part_Min )
                     {
+                        if ( n < 0 )
+                            n = -n;
+
                         n %= MIN_PER_HOUR;
                     }
                     else
@@ -4306,13 +4322,16 @@ wxString wxTimeSpan::Format(const wxChar *format) const
                         partBiggest = Part_Min;
                     }
 
-                    fmtPrefix += _T("02");
+                    digits = 2;
                     break;
 
                 case _T('S'):
                     n = GetSeconds().ToLong();
                     if ( partBiggest < Part_Sec )
                     {
+                        if ( n < 0 )
+                            n = -n;
+
                         n %= SEC_PER_MIN;
                     }
                     else
@@ -4320,10 +4339,19 @@ wxString wxTimeSpan::Format(const wxChar *format) const
                         partBiggest = Part_Sec;
                     }
 
-                    fmtPrefix += _T("02");
+                    digits = 2;
                     break;
             }
 
+            if ( digits )
+            {
+                // negative numbers need one extra position for '-' display
+                if ( n < 0 )
+                    digits++;
+
+                fmtPrefix << _T("0") << digits;
+            }
+
             str += wxString::Format(fmtPrefix + _T("ld"), n);
         }
         else
index 8f9971c4c42cc471ab1f9ab9ac7cd17526f26ea5..150bd8fecf8ed2857eb8b85032b23cc28f9b0b45 100644 (file)
@@ -689,6 +689,8 @@ void DateTimeTestCase::TestTimeSpanFormat()
         { 12, 34, 56, 789, _T("%H:%M:%S.%l"),   _T("12:34:56.789")          },
         {  1,  2,  3,   0, _T("%H:%M:%S"),      _T("01:02:03")              },
         {  1,  2,  3,   0, _T("%S"),            _T("3723")                  },
+        { -1, -2, -3,   0, _T("%S"),            _T("-3723")                 },
+        { -1, -2, -3,   0, _T("%H:%M:%S"),      _T("-01:02:03")             },
     };
 
     for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )