From ffcdcc16174862c9f027f980ca3c5d66953ecb50 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 30 Jun 2016 16:49:20 +0200 Subject: [PATCH] Make wxTimeSpan::operator-() const Due to an oversight, it wasn't declared as const, making it impossible to subtract from a const wxTimeSpan object. Fix this and add a unit test verifying that this compiles and works as expected. Closes #17583. --- include/wx/datetime.h | 2 +- tests/datetime/datetimetest.cpp | 4 ++++ tests/testdate.h | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 89215177c9..194c1f4d79 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -1210,7 +1210,7 @@ public: inline wxTimeSpan& Subtract(const wxTimeSpan& diff); // subtract another timespan wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); } - inline wxTimeSpan operator-(const wxTimeSpan& ts) + inline wxTimeSpan operator-(const wxTimeSpan& ts) const { return wxTimeSpan(GetValue() - ts.GetValue()); } diff --git a/tests/datetime/datetimetest.cpp b/tests/datetime/datetimetest.cpp index 9a13b3d7f1..1b3513bfb9 100644 --- a/tests/datetime/datetimetest.cpp +++ b/tests/datetime/datetimetest.cpp @@ -1346,6 +1346,10 @@ void DateTimeTestCase::TestTimeArithmetics() // And a reverse. Now we should use days in Jun (again 30 => 4w 1d) CPPUNIT_ASSERT_EQUAL( wxDateSpan(0, -10, -4, -1), dtd1.DiffAsDateSpan(dtd2) ); + + const wxTimeSpan ts1 = wxTimeSpan::Seconds(30); + const wxTimeSpan ts2 = wxTimeSpan::Seconds(5); + CPPUNIT_ASSERT_EQUAL( wxTimeSpan::Seconds(25), ts1 - ts2 ); } void DateTimeTestCase::TestDSTBug() diff --git a/tests/testdate.h b/tests/testdate.h index dacf33818c..5d3d43ff5a 100644 --- a/tests/testdate.h +++ b/tests/testdate.h @@ -30,6 +30,17 @@ inline std::ostream& operator<<(std::ostream& ostr, const wxDateSpan& span) return ostr; } +inline std::ostream& operator<<(std::ostream& ostr, const wxTimeSpan& span) +{ + ostr << span.GetWeeks() << "W, " + << span.GetDays() << "D, " + << span.GetHours() << ":" + << span.GetMinutes() << ":" + << span.GetSeconds() << "." + << span.GetMilliseconds(); + + return ostr; +} WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxDateTime::wxDateTime_t) #endif // _WX_TESTS_TESTDATE_H_