sc/source/core/tool/interpr5.cxx |   22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

New commits:
commit 174a72f3fd50d1146d6bedd4cc2a1971aa33be67
Author:     Eike Rathke <er...@redhat.com>
AuthorDate: Mon Jun 19 16:52:40 2023 +0200
Commit:     Eike Rathke <er...@redhat.com>
CommitDate: Mon Jun 19 18:29:12 2023 +0200

    Use tools::Duration in ScInterpreter::CalculateAddSub()
    
    ... for all (date+)time inflicted operands.
    
    Change-Id: I93043d912867e2ef7d4af271b5c4566a3ffb4ef9
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153291
    Reviewed-by: Eike Rathke <er...@redhat.com>
    Tested-by: Jenkins

diff --git a/sc/source/core/tool/interpr5.cxx b/sc/source/core/tool/interpr5.cxx
index b27e27c5e188..dcb5ee6ea343 100644
--- a/sc/source/core/tool/interpr5.cxx
+++ b/sc/source/core/tool/interpr5.cxx
@@ -27,6 +27,7 @@
 
 #include <unotools/bootstrap.hxx>
 #include <svl/zforlist.hxx>
+#include <tools/duration.hxx>
 
 #include <interpre.hxx>
 #include <global.hxx>
@@ -1220,6 +1221,7 @@ void ScInterpreter::CalculateAddSub(bool _bSub)
     double fVal1 = 0.0, fVal2 = 0.0;
     SvNumFormatType nFmt1, nFmt2;
     nFmt1 = nFmt2 = SvNumFormatType::UNDEFINED;
+    bool bDuration = false;
     SvNumFormatType nFmtCurrencyType = nCurFmtType;
     sal_uLong nFmtCurrencyIndex = nCurFmtIndex;
     SvNumFormatType nFmtPercentType = nCurFmtType;
@@ -1235,6 +1237,7 @@ void ScInterpreter::CalculateAddSub(bool _bSub)
             case SvNumFormatType::DATETIME :
             case SvNumFormatType::DURATION :
                 nFmt2 = nCurFmtType;
+                bDuration = true;
             break;
             case SvNumFormatType::CURRENCY :
                 nFmtCurrencyType = nCurFmtType;
@@ -1258,6 +1261,7 @@ void ScInterpreter::CalculateAddSub(bool _bSub)
             case SvNumFormatType::DATETIME :
             case SvNumFormatType::DURATION :
                 nFmt1 = nCurFmtType;
+                bDuration = true;
             break;
             case SvNumFormatType::CURRENCY :
                 nFmtCurrencyType = nCurFmtType;
@@ -1334,10 +1338,22 @@ void ScInterpreter::CalculateAddSub(bool _bSub)
             if (nFmtPercentType == SvNumFormatType::PERCENT && nFuncFmtType == 
SvNumFormatType::NUMBER)
                 nFuncFmtType = SvNumFormatType::PERCENT;
         }
-        if ( _bSub )
-            PushDouble( ::rtl::math::approxSub( fVal1, fVal2 ) );
+        if ((nFuncFmtType == SvNumFormatType::DURATION || bDuration)
+                && ((_bSub && std::fabs(fVal1 - fVal2) <= SAL_MAX_INT32)
+                    || (!_bSub && std::fabs(fVal1 + fVal2) <= SAL_MAX_INT32)))
+        {
+            if (_bSub)
+                PushDouble( ::tools::Duration( fVal1 - fVal2).GetInDays());
+            else
+                PushDouble( ::tools::Duration( fVal1 + fVal2).GetInDays());
+        }
         else
-            PushDouble( ::rtl::math::approxAdd( fVal1, fVal2 ) );
+        {
+            if (_bSub)
+                PushDouble( ::rtl::math::approxSub( fVal1, fVal2 ) );
+            else
+                PushDouble( ::rtl::math::approxAdd( fVal1, fVal2 ) );
+        }
     }
 }
 

Reply via email to