--- a/sc/source/core/tool/interpr2.cxx +++ b/sc/source/core/tool/interpr2.cxx @@ -220,21 +220,55 @@ void ScInterpreter::ScGetDateValue() { OUString aInputString = GetString().getString(); - sal_uInt32 nFIndex = 0; // for a default country/language + sal_uInt32 nFIndex = 0; double fVal; + const Date& nullDate = mrContext.NFGetNullDate(); + const bool excel = tabmorphExcelSource(); + const bool excel1900 = tabmorphExcel1900(nullDate); if (mrContext.NFIsNumberFormat(aInputString, nFIndex, fVal)) { SvNumFormatType eType = mrContext.NFGetType(nFIndex); if (eType == SvNumFormatType::DATE || eType == SvNumFormatType::DATETIME) { + // DATEVALUE cannot produce dates before the selected Excel epoch. + const double minimum = excel1900 ? 2.0 : 0.0; + const double maximum = Date(31, 12, 9999) - nullDate; + if (excel && (fVal < minimum || fVal >= maximum + 1.0)) + { + PushError(FormulaError::NoValue); + return; + } nFuncFmtType = SvNumFormatType::DATE; - if (tabmorphExcel1900(mrContext.NFGetNullDate()) && fVal >= 2.0 && fVal < 61.0) + if (excel1900 && fVal < 61.0) fVal -= 1.0; PushDouble(::rtl::math::approxFloor(fVal)); - } - else - PushIllegalArgument(); - } + return; + } + } + // Let the existing locale-aware parser recognize the fictitious Feb29. + // This working string never changes the source cell or its formula. + const sal_Int32 at = aInputString.indexOf(u"1900"); + auto digit = [](sal_Unicode c) { return c >= '0' && c <= '9'; }; + if (excel1900 && at >= 0 && aInputString.indexOf(u"1900", at + 4) < 0 + && (at == 0 || !digit(aInputString[at - 1])) + && (at + 4 == aInputString.getLength() || !digit(aInputString[at + 4]))) + { + OUString probe = aInputString.replaceAt(at, 4, u"2000"_ustr); + nFIndex = 0; + if (mrContext.NFIsNumberFormat(probe, nFIndex, fVal)) + { + SvNumFormatType type = mrContext.NFGetType(nFIndex); + if ((type == SvNumFormatType::DATE || type == SvNumFormatType::DATETIME) + && ::rtl::math::approxFloor(fVal) == Date(29, 2, 2000) - nullDate) + { + nFuncFmtType = SvNumFormatType::DATE; + PushDouble(60.0); + return; + } + } + } + if (excel) + PushError(FormulaError::NoValue); else PushIllegalArgument(); } @@ -307,7 +341,8 @@ sal_Int16 nFlag = (nParamCount == 1) ? 1 : GetInt16WithDefault(1); Date aDate = mrContext.NFGetNullDate(); - aDate.AddDays( GetFloor32()); + const sal_Int32 serial = GetFloor32(); + aDate.AddDays(serial); sal_Int32 nMinimumNumberOfDaysInWeek; DayOfWeek eFirstDayOfWeek; @@ -340,6 +375,21 @@ default : PushIllegalArgument(); return; + } + if (tabmorphExcel1900(mrContext.NFGetNullDate()) && serial >= 0 && serial <= 366 + && nMinimumNumberOfDaysInWeek == 1) + { + const int offset = (SUNDAY - eFirstDayOfWeek + 7) % 7; + PushInt(static_cast(std::floor((serial - 1 + offset) / 7.0)) + 1); + return; + } + if (tabmorphExcelSource() && nMinimumNumberOfDaysInWeek == 1) + { + // Excel system1 always keeps the partial final week in this year. + const Date firstDay(1, 1, aDate.GetYear()); + const int offset = (firstDay.GetDayOfWeek() - eFirstDayOfWeek + 7) % 7; + PushInt((aDate.GetDayOfYear() - 1 + offset) / 7 + 1); + return; } PushInt( static_cast(aDate.GetWeekOfYear( eFirstDayOfWeek, nMinimumNumberOfDaysInWeek )) ); } @@ -631,6 +681,13 @@ PushIllegalArgument(); return; } + const bool excel = tabmorphExcelSource(); + const sal_Int32 maximum = Date(31, 12, 9999) - rNullDate; + if (excel && (nDate < 0 || nDate > maximum || nDays > maximum - nDate || nDays < -nDate)) + { + PushError(FormulaError::IllegalFPOperation); + return; + } nDate += nNullDate; if ( !nDays ) @@ -646,6 +703,11 @@ do { ++nDate; + if (excel && nDate > nNullDate + maximum) + { + PushError(FormulaError::IllegalFPOperation); + return; + } } while ( bWeekendMask[ GetDayOfWeek( nDate ) ] ); //jump over weekend day(s) @@ -664,6 +726,11 @@ do { --nDate; + if (excel && nDate < nNullDate) + { + PushError(FormulaError::IllegalFPOperation); + return; + } } while ( bWeekendMask[ GetDayOfWeek( nDate ) ] ); //jump over weekend day(s) @@ -772,6 +839,35 @@ } else nSign = 1; + if (tabmorphExcelSource()) + { + const Date& epoch = mrContext.NFGetNullDate(); + const bool excel1900 = tabmorphExcel1900(epoch); + const sal_Int32 maximum = Date(31, 12, 9999) - epoch; + if (nDate1 < 0 || nDate2 < 0 || nDate1 > maximum || nDate2 > maximum) + { + PushError(FormulaError::IllegalFPOperation); + return; + } + auto fields = [&](sal_Int32 serial, int& year, int& month, int& day) { + Date date = epoch; + date.AddDays(tabmorphExcelDateOffset(serial, epoch)); + year = date.GetYear(); month = date.GetMonth(); day = date.GetDay(); + if (excel1900 && serial == 0) { year = 1900; month = 1; day = 0; } + if (excel1900 && serial == 60) { year = 1900; month = 2; day = 29; } + }; + int y1, m1, d1, y2, m2, d2; + fields(nDate1, y1, m1, d1); fields(nDate2, y2, m2, d2); + if (d1 == 31) d1 = 30; + else if (!bFlag && m1 == 2) + { + const bool leap = y1 == 1900 || (y1 % 4 == 0 && (y1 % 100 != 0 || y1 % 400 == 0)); + if (d1 == (leap ? 29 : 28)) d1 = 30; + } + if (d2 == 31 && (bFlag || d1 == 30)) d2 = 30; + PushDouble(static_cast(nSign) * ((y2-y1)*360 + (m2-m1)*30 + d2-d1)); + return; + } Date aDate1 = mrContext.NFGetNullDate(); aDate1.AddDays( nDate1); Date aDate2 = mrContext.NFGetNullDate(); --- a/sc/source/core/tool/addincol.cxx +++ b/sc/source/core/tool/addincol.cxx @@ -1532,6 +1532,11 @@ { nErrCode = FormulaError::IllegalArgument; } + catch(const sheet::NoConvergenceException& error) + { + nErrCode = error.Message == u"TABMORPH_NUMERIC_DATE"_ustr + ? FormulaError::IllegalFPOperation : FormulaError::NoValue; + } catch(const reflection::InvocationTargetException& rWrapped) { if ( rWrapped.TargetException.getValueType().equals( @@ -1539,7 +1544,12 @@ nErrCode = FormulaError::IllegalArgument; else if ( rWrapped.TargetException.getValueType().equals( cppu::UnoType::get()) ) - nErrCode = FormulaError::NoConvergence; + { + sheet::NoConvergenceException error; + rWrapped.TargetException >>= error; + nErrCode = error.Message == u"TABMORPH_NUMERIC_DATE"_ustr + ? FormulaError::IllegalFPOperation : FormulaError::NoConvergence; + } else nErrCode = FormulaError::NoValue; } --- a/scaddins/source/analysis/analysis.cxx +++ b/scaddins/source/analysis/analysis.cxx @@ -31,6 +31,7 @@ #include #include #include +#include #include constexpr OUString ADDIN_SERVICE = u"com.sun.star.sheet.AddIn"_ustr; @@ -40,6 +41,17 @@ using namespace ::com::sun::star; using namespace sca::analysis; +static bool tabmorphAnalysisExcelSource() +{ + const char* mode = std::getenv("TABMORPH_EXCEL_1900"); + return mode && mode[0] == '1' && mode[1] == '\0'; +} + +[[noreturn]] static void tabmorphNumericDateError() +{ + throw sheet::NoConvergenceException(u"TABMORPH_NUMERIC_DATE"_ustr); +} + static bool tabmorphAnalysisExcel1900(sal_Int32 nullDate) { const char* mode = std::getenv("TABMORPH_EXCEL_1900"); @@ -51,14 +63,14 @@ { const sal_Int32 epoch = DateToDays(31, 12, 1899); if (serial < 0 || serial > DateToDays(31, 12, 9999) - epoch + 1) - throw lang::IllegalArgumentException(); + tabmorphNumericDateError(); sal_uInt16 day, month, year; if (serial == 0) { day = 0; month = 1; year = 1900; } else if (serial == 60) { day = 29; month = 2; year = 1900; } else DaysToDate(epoch + serial - (serial > 60 ? 1 : 0), day, month, year); const sal_Int64 ym = static_cast(year) * 12 + month - 1 + months; if (ym < 1900 * 12 || ym >= 10000 * 12) - throw lang::IllegalArgumentException(); + tabmorphNumericDateError(); year = static_cast(ym / 12); month = static_cast(ym % 12 + 1); // Excel16.88 EDATE/EOMONTH use the real Gregorian last day of February, @@ -360,10 +372,13 @@ sal_Int32 SAL_CALL AnalysisAddIn::getWorkday( const uno::Reference< beans::XPropertySet >& xOptions, sal_Int32 nDate, sal_Int32 nDays, const uno::Any& aHDay ) { - if( !nDays ) + const sal_Int32 nNullDate = GetNullDate(xOptions); + const bool excel = tabmorphAnalysisExcelSource(); + const sal_Int32 maximum = DateToDays(31, 12, 9999) - nNullDate; + if (excel && (nDate < 0 || nDate > maximum || nDays > maximum - nDate || nDays < -nDate)) + tabmorphNumericDateError(); + if (!nDays) return nDate; - - sal_Int32 nNullDate = GetNullDate( xOptions ); SortedIndividualInt32List aSrtLst; @@ -380,6 +395,7 @@ while( nDays ) { nActDate++; + if (excel && nActDate > nNullDate + maximum) tabmorphNumericDateError(); if( GetDayOfWeek( nActDate ) < 5 ) { @@ -399,6 +415,7 @@ while( nDays ) { nActDate--; + if (excel && nActDate < nNullDate) tabmorphNumericDateError(); if( GetDayOfWeek( nActDate ) < 5 ) { @@ -410,6 +427,8 @@ } } + if (excel && (nActDate < nNullDate || nActDate > nNullDate + maximum)) + tabmorphNumericDateError(); return nActDate - nNullDate; } @@ -417,8 +436,23 @@ double SAL_CALL AnalysisAddIn::getYearfrac( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& rMode ) { - double fRet = GetYearFrac( xOpt, nStartDate, nEndDate, getDateMode( xOpt, rMode ) ); - return finiteOrThrow( fRet ); + const sal_Int32 nullDate = GetNullDate(xOpt); + const bool excel = tabmorphAnalysisExcelSource(); + const sal_Int32 maximum = DateToDays(31, 12, 9999) - nullDate; + sal_Int32 mode; + if (excel) + { + mode = aAnyConv.getInt32(xOpt, rMode, 0); + if (mode < 0 || mode > 4 || nStartDate < 0 || nEndDate < 0 + || nStartDate > maximum || nEndDate > maximum) + tabmorphNumericDateError(); + } + else mode = getDateMode(xOpt, rMode); + // Restrict the observed1900 compatibility calendar to YEARFRAC itself. + // Other financial functions retain their separate native conventions. + double fRet = GetYearFrac(nullDate, nStartDate, nEndDate, mode, + tabmorphAnalysisExcel1900(nullDate)); + return finiteOrThrow(fRet); } sal_Int32 SAL_CALL AnalysisAddIn::getEdate( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nMonths ) --- a/scaddins/source/analysis/analysishelper.cxx +++ b/scaddins/source/analysis/analysishelper.cxx @@ -454,7 +454,7 @@ * The calculations are defined in: * Open Document Format for Office Applications version 1.2 Part 2, par. 4.11.7 */ -double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) +double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode , bool bExcel1900 ) { if( nStartDate == nEndDate ) return 0.0; // nothing to do... @@ -469,8 +469,21 @@ sal_uInt16 nMonth1, nMonth2; sal_uInt16 nYear1, nYear2; - DaysToDate( nDate1, nDay1, nMonth1, nYear1 ); - DaysToDate( nDate2, nDay2, nMonth2, nYear2 ); + if (bExcel1900) + { + auto fields = [&](sal_Int32 serial, sal_uInt16& day, sal_uInt16& month, sal_uInt16& year) { + if (serial == 0) { day = 0; month = 1; year = 1900; } + else if (serial == 60) { day = 29; month = 2; year = 1900; } + else DaysToDate(nNullDate + serial + (serial < 60 ? 1 : 0), day, month, year); + }; + fields(nStartDate, nDay1, nMonth1, nYear1); + fields(nEndDate, nDay2, nMonth2, nYear2); + } + else + { + DaysToDate(nDate1, nDay1, nMonth1, nYear1); + DaysToDate(nDate2, nDay2, nMonth2, nYear2); + } // calculate days between nDate1 and nDate2 sal_Int32 nDayDiff; --- a/scaddins/source/analysis/analysishelper.hxx +++ b/scaddins/source/analysis/analysishelper.hxx @@ -81,7 +81,7 @@ sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode ); /// @throws css::uno::RuntimeException /// @throws css::lang::IllegalArgumentException -double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ); +double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode , bool bExcel1900 = false ); /// @throws css::uno::RuntimeException /// @throws css::lang::IllegalArgumentException inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ); --- a/sc/source/core/tool/compiler.cxx +++ b/sc/source/core/tool/compiler.cxx @@ -3129,6 +3129,7 @@ OpCode eOp; }; static const FunctionName aOoxmlAliases[] = { + { "_xlfn.WORKDAY.INTL", ocWorkday_MS }, // valid future-function import alias { "EFFECTIVE", ocEffect }, // EFFECTIVE -> EFFECT { "ERRORTYPE", ocErrorType }, // ERRORTYPE -> _xlfn.ORG.OPENOFFICE.ERRORTYPE { "MULTIRANGE", ocMultiArea }, // MULTIRANGE -> _xlfn.ORG.OPENOFFICE.MULTIRANGE