From ea61644abe9e7cd35cdb54452dac327782bbc402 Mon Sep 17 00:00:00 2001 From: Andreas Heinisch Date: Tue, 23 Mar 2021 10:54:14 +0100 Subject: tdf#58745 - Detect end of month when extending a date list Change-Id: Icaa64a493598dc4bb8f2d6d076ad4300e2e4dde6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112976 Tested-by: Jenkins Reviewed-by: Andreas Heinisch --- tools/qa/cppunit/test_date.cxx | 25 +++++++++++++++++++++++++ tools/source/datetime/tdate.cxx | 11 +++++++++++ 2 files changed, 36 insertions(+) (limited to 'tools') diff --git a/tools/qa/cppunit/test_date.cxx b/tools/qa/cppunit/test_date.cxx index 9a243cce504c..e11270e6a299 100644 --- a/tools/qa/cppunit/test_date.cxx +++ b/tools/qa/cppunit/test_date.cxx @@ -26,6 +26,7 @@ public: void testGetDayOfWeek(); void testGetDaysInMonth(); void testIsBetween(); + void testIsEndOfMonth(); CPPUNIT_TEST_SUITE(DateTest); CPPUNIT_TEST(testDate); @@ -37,6 +38,7 @@ public: CPPUNIT_TEST(testGetDayOfWeek); CPPUNIT_TEST(testGetDaysInMonth); CPPUNIT_TEST(testIsBetween); + CPPUNIT_TEST(testIsEndOfMonth); CPPUNIT_TEST_SUITE_END(); }; @@ -533,6 +535,29 @@ void DateTest::testIsBetween() CPPUNIT_ASSERT(aDate.IsBetween(Date(1, 1, 2018), Date(1, 12, 2018))); } +void DateTest::testIsEndOfMonth() +{ + { + Date aDate(31, 12, 2000); + CPPUNIT_ASSERT(aDate.IsEndOfMonth()); + } + + { + Date aDate(30, 12, 2000); + CPPUNIT_ASSERT(!aDate.IsEndOfMonth()); + } + + { + Date aDate(29, 2, 2000); + CPPUNIT_ASSERT(aDate.IsEndOfMonth()); + } + + { + Date aDate(28, 2, 2000); + CPPUNIT_ASSERT(!aDate.IsEndOfMonth()); + } +} + CPPUNIT_TEST_SUITE_REGISTRATION(DateTest); } diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx index a38fb8e986c3..979611333813 100644 --- a/tools/source/datetime/tdate.cxx +++ b/tools/source/datetime/tdate.cxx @@ -452,6 +452,17 @@ bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear ) return true; } +bool Date::IsEndOfMonth() const +{ + return IsEndOfMonth(GetDay(), GetMonth(), GetYear()); +} + +//static +bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear) +{ + return IsValidDate(nDay, nMonth, nYear) && ImplDaysInMonth(nMonth, nYear) == nDay; +} + void Date::Normalize() { sal_uInt16 nDay = GetDay(); -- cgit v1.2.3