[PATCH] fdo#40100 corect result for calc function YEARFRAC

Winfried Donkers (via Code Review) gerrit at gerrit.libreoffice.org
Sat Apr 13 10:47:46 PDT 2013


Hi,

I have submitted a patch for review:

    https://gerrit.libreoffice.org/3375

To pull it, you can do:

    git pull ssh://gerrit.libreoffice.org:29418/core refs/changes/75/3375/1

fdo#40100 corect result for calc function YEARFRAC

Change-Id: Ief5e5c89b7fb69fb9849cf2d6efe2b4c5b7f5391
---
M scaddins/source/analysis/analysishelper.cxx
1 file changed, 18 insertions(+), 13 deletions(-)



diff --git a/scaddins/source/analysis/analysishelper.cxx b/scaddins/source/analysis/analysishelper.cxx
index 278b26f..100b86f 100644
--- a/scaddins/source/analysis/analysishelper.cxx
+++ b/scaddins/source/analysis/analysishelper.cxx
@@ -366,21 +366,15 @@
                                         nDay2, nMonth2, nYear2, nMode == 0 ) - nYears * nDaysInYear;
             break;
         case 1:         // 1=exact/exact
-            nYears = nYear2 - nYear1;
+            //fdo40100 because of possible leap years calculate number
+            //of days separately for year1 and year2 and let nYears
+            //contain only number of full calendar years
+            nYears = nYear2 - nYear1 - 1;
 
             nDaysInYear = IsLeapYear( nYear1 )? 366 : 365;
 
-            if( nYears && ( nMonth1 > nMonth2 || ( nMonth1 == nMonth2 && nDay1 > nDay2 ) ) )
-                nYears--;
-
-            if( nYears )
-                nDayDiff = nDate2 - DateToDays( nDay1, nMonth1, nYear2 );
-            else
-                nDayDiff = nDate2 - nDate1;
-
-            if( nDayDiff < 0 )
-                nDayDiff += nDaysInYear;
-
+            nDayDiff = ( nDate2 - DateToDays( 1, 1, nYear2 ) ) +
+                ( DateToDays( 31, 12, nYear1 ) - nDate1 ) + 1; //plus 1 for Dec 31 to Jan 1
             break;
         case 2:         // 2=exact/360
             nDaysInYear = 360;
@@ -508,6 +502,18 @@
 }
 
 
+//TODO: In the case of nMode == 1 (exact), there still remains a small error
+//      in the result because of leap years. nDaysInYear is an integer and
+//      returns the days in the year of nStartDate. If nEndDate is not a
+//      leap year, the year fraction of the days in the year of nEndDate is
+//      a little bit too small. If nStardate is not in a leap year, but
+//      nEndDate is, then year fraction of the days in the year of nEndDate
+//      is a little bit too large.
+//      This could be corrected when nDaysInYear is a float and the value
+//      reflects the number of days in the leap year and the non-leap year.
+//      Example: If there are 80 days in the leap year and 80 days in the
+//      non-leap year, then nDaysInYear would be 365.5, resulting in a more
+//      or less exact year fraction.
 double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
 {
     if( nStartDate == nEndDate )
@@ -517,7 +523,6 @@
     sal_Int32   nDayDiff, nDaysInYear;
 
     GetDiffParam( nNullDate, nStartDate, nEndDate, nMode, nYears, nDayDiff, nDaysInYear );
-
     return double( nYears ) + double( nDayDiff ) / double( nDaysInYear );
 }
 

-- 
To view, visit https://gerrit.libreoffice.org/3375
To unsubscribe, visit https://gerrit.libreoffice.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ief5e5c89b7fb69fb9849cf2d6efe2b4c5b7f5391
Gerrit-PatchSet: 1
Gerrit-Project: core
Gerrit-Branch: master
Gerrit-Owner: Winfried Donkers <osc at dci-electronics.nl>



More information about the LibreOffice mailing list