[Libreoffice-commits] core.git: 3 commits - tools/qa tools/source

Eike Rathke erack at redhat.com
Tue May 2 19:12:43 UTC 2017


 tools/qa/cppunit/test_date.cxx  |   33 +++++++++++++++++++++++++++++++++
 tools/source/datetime/tdate.cxx |   16 ++++++++++++++++
 2 files changed, 49 insertions(+)

New commits:
commit 4c6876582d864897c3ebd4d00bd9fb5c6334edc1
Author: Eike Rathke <erack at redhat.com>
Date:   Tue May 2 21:11:38 2017 +0200

    Add more unit tests for Date::Normalize()
    
    Change-Id: I013317bfa9dad02194177cca0907b0ffbb171ccd

diff --git a/tools/qa/cppunit/test_date.cxx b/tools/qa/cppunit/test_date.cxx
index 2ea3673082a2..054b38b1a727 100644
--- a/tools/qa/cppunit/test_date.cxx
+++ b/tools/qa/cppunit/test_date.cxx
@@ -78,6 +78,35 @@ void DateTest::testDate()
     aDate.Normalize();
     CPPUNIT_ASSERT_EQUAL( Date(4,3,1999).GetDate(), aDate.GetDate());
 
+    // Empty date is not normalized and stays empty date.
+    aDate = Date( Date::EMPTY );
+    aDate.Normalize();
+    CPPUNIT_ASSERT_EQUAL( Date(Date::EMPTY).GetDate(), aDate.GetDate());
+    CPPUNIT_ASSERT( !aDate.IsValidDate());  // GetDate() also shall have no normalizing side effect
+
+    // 0000-01-00 normalized to -0001-12-31
+    // SetYear(0) asserts, use empty date to force.
+    aDate = Date( Date::EMPTY );
+    aDate.SetMonth(1);
+    aDate.SetDay(0);
+    aDate.Normalize();
+    CPPUNIT_ASSERT_EQUAL( Date(31,12,-1).GetDate(), aDate.GetDate());
+
+    // 1999-00-00 normalized to 1998-12-31 (not 1998-11-30, or otherwise
+    // also 0001-00-00 should be -0001-11-30 which it should not, should it?)
+    aDate.SetYear(1999);
+    aDate.SetMonth(0);
+    aDate.SetDay(0);
+    aDate.Normalize();
+    CPPUNIT_ASSERT_EQUAL( Date(31,12,1998).GetDate(), aDate.GetDate());
+
+    // 0001-00-00 normalized to -0001-12-31
+    aDate.SetYear(1);
+    aDate.SetMonth(0);
+    aDate.SetDay(0);
+    aDate.Normalize();
+    CPPUNIT_ASSERT_EQUAL( Date(31,12,-1).GetDate(), aDate.GetDate());
+
     // Year -1 is a leap year.
     aDate = Date(28,2,-1);
     CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), (aDate += 1).GetDate());
commit 1edd4229904575ea103730ac15254b402eb86709
Author: Eike Rathke <erack at redhat.com>
Date:   Tue May 2 20:52:02 2017 +0200

    Date::Normalize: handle day 0 and empty date
    
    * Empty date (initialized with Date::EMPTY == all 0) stays empty.
    * Day 0 is normalized to the last day of the previous month.
    * Day 0 AND month 0 is also normalized to the last day of the previous month
      (and not the last day of the previous-1 month).
    * Before,
      * all day 0 cases weren't handled at all and day 0 stayed day 0, which
        doesn't make sense in a Normalize() context
      * empty date wasn't detected and resulted in nonsense date -0001-12-00
    
    Change-Id: Ib12ff7694e1adb17724af038a266f6f284285f36

diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index 9815716ebe3f..3dd8f68b3ced 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -493,6 +493,18 @@ bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear
     if (IsValidDate( rDay, rMonth, rYear))
         return false;
 
+    if (rDay == 0 && rMonth == 0 && rYear == 0)
+        return false;   // empty date
+
+    if (rDay == 0)
+    {
+        if (rMonth == 0)
+            ;   // nothing, handled below
+        else
+            --rMonth;
+        // Last day of month is determined at the end.
+    }
+
     if (rMonth > 12)
     {
         rYear += rMonth / 12;
@@ -550,6 +562,10 @@ bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear
             }
         }
     }
+
+    if (rDay == 0)
+        rDay = ImplDaysInMonth( rMonth, rYear);
+
     return true;
 }
 
commit 246a2a54662eb52468f9a411b315a4300aaa17c0
Author: Eike Rathke <erack at redhat.com>
Date:   Tue May 2 17:53:13 2017 +0200

    Test that empty date is not a valid date
    
    Change-Id: If5f2db3d4bae9158b482b01ce7dbb7335b4cb16b

diff --git a/tools/qa/cppunit/test_date.cxx b/tools/qa/cppunit/test_date.cxx
index c2c31e10b51c..2ea3673082a2 100644
--- a/tools/qa/cppunit/test_date.cxx
+++ b/tools/qa/cppunit/test_date.cxx
@@ -61,6 +61,10 @@ void DateTest::testDate()
     aDate.Normalize();
     CPPUNIT_ASSERT_EQUAL( aMax.GetDate(), aDate.GetDate());
 
+    // Empty date is not a valid date.
+    aDate = Date( Date::EMPTY );
+    CPPUNIT_ASSERT( !aDate.IsValidDate());
+
     // 0001-00-x normalized to -0001-12-x
     aDate.SetYear(1);
     aDate.SetMonth(0);


More information about the Libreoffice-commits mailing list