]> git.saurik.com Git - wxWidgets.git/commitdiff
fix wxTimeSpan::Format() for negative spans with 0 hour component (#10055)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 21 Dec 2008 12:16:06 +0000 (12:16 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 21 Dec 2008 12:16:06 +0000 (12:16 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57474 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/datetime.cpp
tests/datetime/datetimetest.cpp

index 80d8fd579166b08989799c7764add2423240f5f9..16ccbc4094dd29d4fe4125044e81fa4bf610eb0b 100644 (file)
@@ -4274,6 +4274,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") );
 
@@ -4345,13 +4353,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
@@ -4366,9 +4367,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
@@ -4381,9 +4379,6 @@ wxString wxTimeSpan::Format(const wxString& format) const
                     n = GetMinutes();
                     if ( partBiggest < Part_Min )
                     {
-                        if ( n < 0 )
-                            n = -n;
-
                         n %= MIN_PER_HOUR;
                     }
                     else
@@ -4398,9 +4393,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
@@ -4414,10 +4406,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;
             }
 
index f4f87df2f4675aed9cfbb63f7a91861e460cfb6f..033707b17cec68a46f4a06d5823c431595fa7258 100644 (file)
@@ -738,13 +738,15 @@ void DateTimeTestCase::TestTimeSpanFormat()
         {  219,  0,  0,   0, "%H",            "219"                   },
         {  219,  0,  0,   0, "%D, %H",        "9, 03"                 },
         {  219,  0,  0,   0, "%E, %D, %H",    "1, 2, 03"              },
+        {    0, -1,  0,   0, "%H:%M:%S",      "-00:01:00"             },
+        {    0,  0, -1,   0, "%H:%M:%S",      "-00:00:01"             },
     };
 
     for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )
     {
         const TimeSpanFormatTestData& td = testSpans[n];
         wxTimeSpan ts(td.h, td.min, td.sec, td.msec);
-        CPPUNIT_ASSERT_EQUAL( wxString(td.result), ts.Format(td.fmt) );
+        CPPUNIT_ASSERT_EQUAL( td.result, ts.Format(td.fmt) );
     }
 }