From 9be232e7a97579963efa00896e5603c040669b26 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 21 Dec 2008 12:16:06 +0000 Subject: [PATCH] fix wxTimeSpan::Format() for negative spans with 0 hour component (#10055) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@57474 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/common/datetime.cpp | 31 ++++++++++--------------------- tests/datetime/datetimetest.cpp | 2 ++ 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 2f8175fcad..65303bce4d 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -95,6 +95,7 @@ All: - wxHashMap::insert() doesn't update the value if it didn't insert the element any more (Marcin Malich). +- Correct bug in wxTimeSpan::Format() for negative spans. - Added Vietnamese translation (Tran Ngoc Quan). - Updated Slovenian translation (Martin Srebotnjak). diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index b2bd5cc287..897c24ea17 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -4271,7 +4271,16 @@ enum TimeSpanPart // %l milliseconds (000 - 999) wxString wxTimeSpan::Format(const wxChar *format) const { - wxCHECK_MSG( format, wxEmptyString, _T("NULL format in wxTimeSpan::Format") ); + // 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, wxEmptyString, + _T("NULL format in wxTimeSpan::Format") ); wxString str; str.Alloc(wxStrlen(format)); @@ -4341,13 +4350,6 @@ 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 @@ -4362,9 +4364,6 @@ wxString wxTimeSpan::Format(const wxChar *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 @@ -4377,9 +4376,6 @@ wxString wxTimeSpan::Format(const wxChar *format) const n = GetMinutes(); if ( partBiggest < Part_Min ) { - if ( n < 0 ) - n = -n; - n %= MIN_PER_HOUR; } else @@ -4394,9 +4390,6 @@ wxString wxTimeSpan::Format(const wxChar *format) const n = GetSeconds().ToLong(); if ( partBiggest < Part_Sec ) { - if ( n < 0 ) - n = -n; - n %= SEC_PER_MIN; } else @@ -4410,10 +4403,6 @@ wxString wxTimeSpan::Format(const wxChar *format) const if ( digits ) { - // negative numbers need one extra position for '-' display - if ( n < 0 ) - digits++; - fmtPrefix << _T("0") << digits; } diff --git a/tests/datetime/datetimetest.cpp b/tests/datetime/datetimetest.cpp index 2773218ae9..cec7d2c54a 100644 --- a/tests/datetime/datetimetest.cpp +++ b/tests/datetime/datetimetest.cpp @@ -706,6 +706,8 @@ void DateTimeTestCase::TestTimeSpanFormat() { 219, 0, 0, 0, _T("%H"), _T("219") }, { 219, 0, 0, 0, _T("%D, %H"), _T("9, 03") }, { 219, 0, 0, 0, _T("%E, %D, %H"), _T("1, 2, 03") }, + { 0, -1, 0, 0, _T("%H:%M:%S"), _T("-00:01:00") }, + { 0, 0, -1, 0, _T("%H:%M:%S"), _T("-00:00:01") }, }; for ( size_t n = 0; n < WXSIZEOF(testSpans); n++ )