This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch openformula
in repository https://gitbox.apache.org/repos/asf/openoffice.git


The following commit(s) were added to refs/heads/openformula by this push:
     new 62e7f86e31 H.3.1: Complete DATEDIF semantics, help, and tests
62e7f86e31 is described below

commit 62e7f86e31aeeea8e6cfda4779001721ba725d12
Author: Pedro Giffuni <[email protected]>
AuthorDate: Tue Nov 17 15:21:34 2026 -0500

    H.3.1: Complete DATEDIF semantics, help, and tests
    
    - Correct md borrow, ym/yd anniversary handling, and signed arithmetic
    - Add ScDateDif declaration and help topic registration
    - Fix SG.DATEDIF-010-md expectation and expand DATEDIF calcval coverage
    - Update DATEDIF design and audit records
---
 .../datedif-semantic-design.md                     |  41 ++++++++
 .../function-gap-inventory.md                      |   2 +-
 .../function-gap-summary.tsv                       |   2 +-
 .../function-semantic-test-plan.md                 |  31 +++---
 .../helpcontent2/source/text/scalc/01/04060102.xhp |   5 +
 .../source/text/scalc/01/func_datedif.xhp          | 108 ++++++++++++++++++++
 main/helpcontent2/source/text/scalc/01/makefile.mk |   1 +
 main/sc/source/core/inc/interpre.hxx               |   1 +
 main/sc/source/core/tool/interpr2.cxx              | 112 ++++++++++-----------
 test/odf-validate/calcval-cases14.tsv              |  34 ++++++-
 10 files changed, 258 insertions(+), 79 deletions(-)

diff --git a/audit/openformula-function-gaps/datedif-semantic-design.md 
b/audit/openformula-function-gaps/datedif-semantic-design.md
index 8f78d0f9d0..834375cfb4 100644
--- a/audit/openformula-function-gaps/datedif-semantic-design.md
+++ b/audit/openformula-function-gaps/datedif-semantic-design.md
@@ -193,3 +193,44 @@ Files to Modify
 8. main/sc/util/hidother.src — Register Help ID
 9. test/odf-validate/calcval-cases14.tsv — Regression tests
 10. audit/openformula-function-gaps/ — Update audit records
+
+
+H.3.1 Implementation/Review Notes (2026-11-17)
+----------------------------------------------
+Implementation: `ScInterpreter::ScDateDif()` in
+`main/sc/source/core/tool/interpr2.cxx`, declared in
+`main/sc/source/core/inc/interpre.hxx`, dispatched from
+`main/sc/source/core/tool/interpr4.cxx` (`ocDateDif`), with resources/help IDs
+registered and BIFF5/7 import mapped import-only (`EXC_FUNCFLAG_IMPORTONLY`) in
+`main/sc/source/filter/excel/xlformula.cxx` (ID 351 → `ocDateDif`).
+
+Semantics as implemented (ODF 1.4 §6.10.3):
+- Exactly 3 arguments: StartDate, EndDate, Format (popped Format, EndDate,
+  StartDate).
+- Date serials floored to calendar dates via `::rtl::math::approxFloor()`; time
+  fractions discarded. Calendar conversion uses `pFormatter->GetNullDate()`.
+- Invalid/unsupported date serials (`Date::IsValid() == false`) → illegal
+  argument error.
+- Format is case-sensitive: only `y`, `m`, `d`, `md`, `ym`, `yd` accepted;
+  any other value (including `Y/M/D` and empty string) → illegal argument 
error.
+- Forward intervals (`StartDate <= EndDate`): `d` = calendar day difference;
+  `y` = complete calendar years; `m` = complete calendar months with
+  end-of-month substitution when the start day is the last day of its month;
+  `ym` = `m - y*12` (months remaining after complete years);
+  `md` = day difference ignoring months/years, borrowing from the start date's
+  month when end day < start day;
+  `yd` = days from the anniversary (start date shifted forward by complete
+  years, adjusted for Feb 29 in non-leap anniversary years) to end date.
+- Reverse intervals (`StartDate > EndDate`): computed on the swapped forward
+  interval. `y`, `m`, `d` return the negated forward result; `md`, `ym`, `yd`
+  return a non-negative component value (Excel-compatible policy; ODF 1.4 does
+  not define reverse intervals). This is an implementation constraint/policy,
+  not a normative ODF requirement.
+
+Validation notes:
+- Source-level review/static verification only in this environment; Calc and
+  calcval runtime tests cannot be executed because `libsot.so` / 
`sc`/`scaddins`
+  are not built (GCC 15.2 vs AOO 510 ABI mismatch, no older GCC, no sudo).
+- `SG.DATEDIF-010-md` expected value corrected to `26` (borrow from start
+  month January: `10 + 31 - 15`) and DATEDIF regression coverage expanded in
+  `test/odf-validate/calcval-cases14.tsv`.
diff --git a/audit/openformula-function-gaps/function-gap-inventory.md 
b/audit/openformula-function-gaps/function-gap-inventory.md
index 66fa5ef08b..f95d070f37 100644
--- a/audit/openformula-function-gaps/function-gap-inventory.md
+++ b/audit/openformula-function-gaps/function-gap-inventory.md
@@ -12,7 +12,7 @@ Summary table of all 15 audited functions.
 | FINDB | Present | ocFinDB (412) | ScFindB() | Yes (HID_FUNC_FINDB) | No | 
N/A | No | No | No | No | None — implementation complete at source level | 
Resolved (was P1) | HIGH |
 | REPLACEB | Present | ocReplaceB (414) | ScReplaceB() | Yes 
(HID_FUNC_REPLACEB) | No | N/A | No | No | No | No | None — implementation 
complete at source level | Resolved (was P1) | HIGH |
 | SEARCHB | Present | ocSearchB (413) | ScSearchB() | Yes (HID_FUNC_SEARCHB) | 
No | N/A | No | No | No | No | None — implementation complete at source level | 
Resolved (was P1) | HIGH |
-| DATEDIF | Present | ocDateDif (415) | ScDateDif() | Yes (HID_FUNC_DATEDIF) | 
Yes | Large Group | Yes(BIFF5/7,ID351,ocDateDif) | No | No | No | None — 
implementation complete at source level | Resolved (was P1) | HIGH |
+| DATEDIF | Present | ocDateDif (415) | ScDateDif() | Yes (HID_FUNC_DATEDIF) | 
Yes | Large Group | Yes(BIFF5/7,ID351,ocDateDif,import-only) | No | No | No | 
Reverse-interval/component policy Excel-compatible (ODF undefined); runtime 
validation pending (no sc build) | Resolved/H.3.1 reviewed (was P1) | HIGH |
 | ISOWEEKNUM | Partial (wrong semantics) | ocWeek | ScGetWeekOfYear() | German 
only (KALENDERWOCHE) | No | N/A | No | Yes (ODF table, import-only) | No | 
_xlfnodf.ISOWEEKNUM | ISO 8601 semantics not implemented (only SUN/MON start) | 
P1 | MEDIUM |
 | DDE | Present (platform-dependent) | ocDde (369) | ScDde() | Yes 
(HID_FUNC_DDE) | No | N/A | No | No | No | No | Platform-dependent; no 
file-format mapping | DEFER | HIGH |
 | ODDFPRICE | Present (AddIn) | None | getOddfprice() (AddIn) | Yes 
(HID_AAI_FUNC_ODDFPRICE) | No | N/A | No | No | No | No | Analysis AddIn, not 
native | P2 | HIGH |
diff --git a/audit/openformula-function-gaps/function-gap-summary.tsv 
b/audit/openformula-function-gaps/function-gap-summary.tsv
index 3d9b75172b..a4c157aa8b 100644
--- a/audit/openformula-function-gaps/function-gap-summary.tsv
+++ b/audit/openformula-function-gaps/function-gap-summary.tsv
@@ -2,7 +2,7 @@ function        odf_1_4_defined odf_group       opcode  
parser_name     evaluator       help    xls_import
 FINDB  No      N/A     ocFinDB(412)    FINDB   ScFindB()       Yes 
(HID_FUNC_FINDB)    No      No      No      No      None — implementation 
complete at source level  Resolved (was P1)       HIGH    compiler.hrc:412; 
interpr4.cxx:3772 dispatch, interpr1.cxx:8155 evaluator; DBCS byte semantics
 REPLACEB       No      N/A     ocReplaceB(414) REPLACEB        ScReplaceB()    
Yes (HID_FUNC_REPLACEB) No      No      No      No      None — implementation 
complete at source level  Resolved (was P1)       HIGH    compiler.hrc:414; 
interpr4.cxx:3774 dispatch, interpr1.cxx:8250 evaluator; DBCS byte semantics
 SEARCHB        No      N/A     ocSearchB(413)  SEARCHB ScSearchB()     Yes 
(HID_FUNC_SEARCHB)  No      No      No      No      None — implementation 
complete at source level  Resolved (was P1)       HIGH    compiler.hrc:413; 
interpr4.cxx:3773 dispatch, interpr1.cxx:8197 evaluator; DBCS byte semantics
-DATEDIF        Yes     Large Group     ocDateDif(415)  DATEDIF ScDateDif()     
Yes (HID_FUNC_DATEDIF)  Yes(BIFF5/7,ID351,ocDateDif)    No      No      No      
None — implementation complete at source level  Resolved (was P1)       HIGH    
compiler.hrc:415; interpr4.cxx:3775 dispatch, interpr2.cxx:443 evaluator; 
xlformula.cxx:342 BIFF import maps to ocDateDif (import-only); ODF 6.10.3 
conformant
+DATEDIF        Yes     Large Group     ocDateDif(415)  DATEDIF ScDateDif()     
Yes (HID_FUNC_DATEDIF)  Yes(BIFF5/7,ID351,ocDateDif,import-only)        No      
No      No      Reverse-interval/component policy Excel-compatible (ODF 
undefined); runtime validation pending (no sc build)    Resolved/H.3.1 reviewed 
(was P1)        HIGH    compiler.hrc:415; interpr4.cxx:3775 dispatch, 
interpr2.cxx:443 evaluator; xlformula.cxx:342 BIFF5/7 import-only; H.3.1 fixed 
md borrow/ym/yd semantics, added help+expanded calcval cases; ODF 6.10.3 
source-level
 ISOWEEKNUM     No      N/A     ocWeek  ISOWEEKNUM      ScGetWeekOfYear()       
German only (KALENDERWOCHE)     No      Yes(ODF table,import-only)      No      
_xlfnodf.ISOWEEKNUM     ISO 8601 semantics not implemented (SUN/MON start only) 
P1      MEDIUM  interpr2.cxx:227 ScGetWeekOfYear() uses Date::GetWeekOfYear(); 
no ISO week-1=first-Thursday logic
 DDE    No      N/A     ocDde(369)      DDE     ScDde() Yes (HID_FUNC_DDE)      
No      No      No      No      Platform-dependent; no file-format mapping      
DEFER   HIGH    interpr2.cxx:2150; DDE via sfx2::LinkManager; disabled in 
server mode
 ODDFPRICE      No      N/A     None    ODDFPRICE       getOddfprice() (AddIn)  
Yes (HID_AAI_FUNC_ODDFPRICE)    No      No      No      No      Analysis AddIn, 
not native      P2      HIGH    odffmap.cxx:127; financial.cxx:404; AddIn 
mechanism only
diff --git a/audit/openformula-function-gaps/function-semantic-test-plan.md 
b/audit/openformula-function-gaps/function-semantic-test-plan.md
index b415b143bf..ce3c74d2f6 100644
--- a/audit/openformula-function-gaps/function-semantic-test-plan.md
+++ b/audit/openformula-function-gaps/function-semantic-test-plan.md
@@ -64,25 +64,28 @@ Purpose: Confirm function is entirely absent.
 4. DATEDIF
 ----------
 
-Not defined by ODF 1.4. Excel function for date differences.
+ODF 1.4 Part 4 §6.10.3. Excel-compatible date difference function.
 
-Test cases:
+Test cases (implemented as `SG.DATEDIF-*` in 
`test/odf-validate/calcval-cases14.tsv`):
   a) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"d") — days difference (365)
   b) =DATEDIF(DATE(2024;1;1);DATE(2025;1;1);"m") — months difference (12)
   c) =DATEDIF(DATE(2024;1;1);DATE(2025;1;1);"y") — years difference (1)
-  d) =DATEDIF(DATE(2024;2;28);DATE(2024;3;1);"d") — 2 days (leap year)
-  e) =DATEDIF(DATE(2024;3;1);DATE(2024;2;28);"d") — end before start (error 
expected)
-  f) =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"d") — same date (0)
-  g) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"yd") — days ignoring years (364)
-  h) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"ym") — months ignoring years 
(11)
-  i) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"md") — days ignoring 
months/years (30)
+  d) =DATEDIF(DATE(2024;2;29);DATE(2024;3;1);"d") — leap-day interval
+  e) =DATEDIF(DATE(2024;12;31);DATE(2024;1;1);"d") — reverse interval → -365 
(y/m/d negated)
+  f) =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"d"/"m"/"y"/"md"/"ym"/"yd") — same 
date (0)
+  g) =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"md") — days ignoring 
months/years with borrow (26)
+  h) =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"ym") — months ignoring years 
after complete months (1)
+  i) =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"yd") — days ignoring years from 
anniversary (55)
   j) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"x") — invalid unit string 
(error)
-  k) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31)) — wrong arg count (error)
-  l) XLS import (BIFF5/7) of DATEDIF — import maps to ocNoName → #NAME! 
(confirmed by source)
-  m) XLS import (BIFF8) of DATEDIF — import failure (not in BIFF8 table)
-  n) XLSX import of DATEDIF — import failure (not in OOXML table)
-
-Purpose: Confirm DATEDIF is entirely missing; BIFF5/7 import produces #NAME!.
+  k) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"") — empty format (error)
+  l) =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"D") — case-sensitive format 
(error)
+  m) end-of-month / leap-anniversary / multi-year / reverse-component / 
time-fraction edge cases
+  n) BIFF5/7 import of DATEDIF — maps to `ocDateDif` (import-only, ID 351); 
BIFF8/OOXML not mapped
+  o) wrong arg count / invalid date serials — error (reviewed in code)
+
+Purpose: Validate implemented DATEDIF semantics and reverse-interval policy.
+Source-level review completed in H.3.1; runtime execution pending build
+availability (`sc`/`scaddins` not built in this environment).
 
 
 5. ISOWEEKNUM
diff --git a/main/helpcontent2/source/text/scalc/01/04060102.xhp 
b/main/helpcontent2/source/text/scalc/01/04060102.xhp
index 568f1db65c..b71c7937af 100644
--- a/main/helpcontent2/source/text/scalc/01/04060102.xhp
+++ b/main/helpcontent2/source/text/scalc/01/04060102.xhp
@@ -187,6 +187,11 @@
 <embedvar href="text/scalc/01/func_days360.xhp#days360"/>
 </paragraph>
 </section>
+<section id="datedif">
+<paragraph role="paragraph" id="par_id3155511" xml-lang="en-US" l10n="NEW">
+<embedvar href="text/scalc/01/func_datedif.xhp#datedif"/>
+</paragraph>
+</section>
 <section id="weekday">
 <paragraph role="paragraph" id="par_id9172643" xml-lang="en-US" l10n="NEW">
 <embedvar href="text/scalc/01/func_weekday.xhp#weekday"/>
diff --git a/main/helpcontent2/source/text/scalc/01/func_datedif.xhp 
b/main/helpcontent2/source/text/scalc/01/func_datedif.xhp
new file mode 100644
index 0000000000..f3797d0af2
--- /dev/null
+++ b/main/helpcontent2/source/text/scalc/01/func_datedif.xhp
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--***********************************************************
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ ***********************************************************-->
+
+<helpdocument version="1.0">
+<meta>
+<topic id="textscalc01func_datedifxml" indexer="include" status="PUBLISH">
+<title id="tit" xml-lang="en-US">DATEDIF</title>
+<filename>/text/scalc/01/func_datedif.xhp</filename>
+</topic>
+</meta>
+<body>
+<bookmark xml-lang="en-US" branch="index" id="bm_id3155511">
+<bookmark_value>DATEDIF function</bookmark_value>
+<bookmark_value>date difference</bookmark_value>
+</bookmark>
+<bookmark xml-lang="en-US" branch="hid/SC_HID_FUNC_DATEDIF" id="bm_id3155512" 
localize="false"/>
+<paragraph role="heading" id="hd_id3155510" xml-lang="en-US" level="2" 
l10n="U"><variable id="datedif"><link 
href="text/scalc/01/func_datedif.xhp">DATEDIF</link>
+</variable></paragraph>
+<paragraph role="paragraph" id="par_id3155513" xml-lang="en-US" 
l10n="U"><ahelp hid="HID_FUNC_DATEDIF">Returns the difference between two date 
values, in the units specified.</ahelp></paragraph>
+<paragraph role="heading" id="hd_id3155514" xml-lang="en-US" level="3" 
l10n="U">Syntax</paragraph>
+<paragraph role="code" id="par_id3155515" xml-lang="en-US" 
l10n="U">DATEDIF(StartDate; EndDate; Format)</paragraph>
+<paragraph role="paragraph" id="par_id3155516" xml-lang="en-US" 
l10n="U"><emph>StartDate</emph> is the start date of the interval.</paragraph>
+<paragraph role="paragraph" id="par_id3155517" xml-lang="en-US" 
l10n="U"><emph>EndDate</emph> is the end date of the interval. Time fractions 
are ignored.</paragraph>
+<paragraph role="paragraph" id="par_id3155518" xml-lang="en-US" 
l10n="U"><emph>Format</emph> is a text value that specifies the unit of the 
result. The format is case-sensitive.</paragraph>
+<table id="tbl_id3155519">
+<tablerow>
+<tablecell>
+<paragraph role="tablehead" id="par_id3155520" xml-lang="en-US" 
l10n="U">Format</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablehead" id="par_id3155521" xml-lang="en-US" 
l10n="U">Meaning</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155522" xml-lang="en-US" 
l10n="U">"d"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155523" xml-lang="en-US" 
l10n="U">Number of days between the dates.</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155524" xml-lang="en-US" 
l10n="U">"m"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155525" xml-lang="en-US" 
l10n="U">Number of complete calendar months between the dates.</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155526" xml-lang="en-US" 
l10n="U">"y"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155527" xml-lang="en-US" 
l10n="U">Number of complete calendar years between the dates.</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155528" xml-lang="en-US" 
l10n="U">"md"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155529" xml-lang="en-US" 
l10n="U">Days, ignoring months and years.</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155530" xml-lang="en-US" 
l10n="U">"ym"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155531" xml-lang="en-US" 
l10n="U">Months, ignoring years.</paragraph>
+</tablecell>
+</tablerow>
+<tablerow>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155532" xml-lang="en-US" 
l10n="U">"yd"</paragraph>
+</tablecell>
+<tablecell>
+<paragraph role="tablecontent" id="par_id3155533" xml-lang="en-US" 
l10n="U">Days, ignoring years.</paragraph>
+</tablecell>
+</tablerow>
+</table>
+<paragraph role="paragraph" id="par_id3155534" xml-lang="en-US" l10n="U">If 
<emph>StartDate</emph> is later than <emph>EndDate</emph>, the result for 
formats "d", "m", and "y" is negative. The component formats "md", "ym", and 
"yd" return a non-negative value.</paragraph>
+<paragraph role="heading" id="hd_id3155535" xml-lang="en-US" level="3" 
l10n="U">Examples</paragraph>
+<paragraph role="paragraph" id="par_id3155536" xml-lang="en-US" l10n="U"><item 
type="input">=DATEDIF("2024-01-01";"2024-12-31";"d")</item> returns 
365.</paragraph>
+<paragraph role="paragraph" id="par_id3155537" xml-lang="en-US" l10n="U"><item 
type="input">=DATEDIF("2024-01-01";"2025-01-01";"y")</item> returns 
1.</paragraph>
+</body>
+</helpdocument>
diff --git a/main/helpcontent2/source/text/scalc/01/makefile.mk 
b/main/helpcontent2/source/text/scalc/01/makefile.mk
index 5392e30a64..de6b7b598f 100644
--- a/main/helpcontent2/source/text/scalc/01/makefile.mk
+++ b/main/helpcontent2/source/text/scalc/01/makefile.mk
@@ -201,6 +201,7 @@ XHPFILES = \
    func_day.xhp \
    func_days.xhp \
    func_days360.xhp \
+   func_datedif.xhp \
    func_eastersunday.xhp \
    func_edate.xhp \
    func_eomonth.xhp \
diff --git a/main/sc/source/core/inc/interpre.hxx 
b/main/sc/source/core/inc/interpre.hxx
index bc868204d3..9bcae6c17a 100644
--- a/main/sc/source/core/inc/interpre.hxx
+++ b/main/sc/source/core/inc/interpre.hxx
@@ -633,6 +633,7 @@ void ScGetDate();
 void ScGetTime();
 void ScGetDiffDate();
 void ScGetDiffDate360();
+void ScDateDif();
 void ScCurrent();
 void ScStyle();
 void ScDde();
diff --git a/main/sc/source/core/tool/interpr2.cxx 
b/main/sc/source/core/tool/interpr2.cxx
index 58cb927c7d..451bdef7a7 100644
--- a/main/sc/source/core/tool/interpr2.cxx
+++ b/main/sc/source/core/tool/interpr2.cxx
@@ -472,66 +472,60 @@ void ScInterpreter::ScDateDif()
             return;
         }
 
+        // Work forward for calculation; apply sign for reverse intervals at 
end.
+        bool bReverse = (aStart > aEnd);
+        Date aFrom = bReverse ? aEnd : aStart;
+        Date aTo = bReverse ? aStart : aEnd;
+
+        sal_uInt32 nYear1  = aFrom.GetYear();
+        sal_uInt32 nMonth1 = aFrom.GetMonth();
+        sal_uInt32 nDay1   = aFrom.GetDay();
+        sal_uInt32 nYear2  = aTo.GetYear();
+        sal_uInt32 nMonth2 = aTo.GetMonth();
+        sal_uInt32 nDay2   = aTo.GetDay();
+
+        // Complete calendar years (ODF 1.4 §6.10.3 "y")
+        long nYears = (long)nYear2 - (long)nYear1;
+        if ( nMonth2 < nMonth1 || (nMonth2 == nMonth1 && nDay2 < nDay1) )
+            nYears--;
+
+        // Complete calendar months (ODF 1.4 §6.10.3 "m")
+        long nMonths = ((long)nYear2 - (long)nYear1) * 12
+                     + ((long)nMonth2 - (long)nMonth1);
+        if ( nDay2 < nDay1 )
+        {
+            // If start is the last day of its month, treat the end month as
+            // reached (end-of-month substitution); otherwise decrement.
+            if ( nDay1 != aFrom.GetDaysInMonth() )
+                nMonths--;
+        }
+
         double fResult = 0.0;
         bool bValidFormat = true;
 
         if ( aFormat == "d" )
         {
-            // Calendar day difference
-            fResult = (double)(aEnd - aStart);
+            // Calendar day difference (ODF 1.4 §6.10.3)
+            fResult = (double)(aTo - aFrom);
         }
         else if ( aFormat == "m" )
         {
-            // Complete calendar months
-            sal_uInt32 nYear1 = aStart.GetYear();
-            sal_uInt32 nMonth1 = aStart.GetMonth();
-            sal_uInt32 nDay1 = aStart.GetDay();
-            sal_uInt32 nYear2 = aEnd.GetYear();
-            sal_uInt32 nMonth2 = aEnd.GetMonth();
-            sal_uInt32 nDay2 = aEnd.GetDay();
-
-            long nMonths = (long)(nYear2 - nYear1) * 12 + (long)(nMonth2 - 
nMonth1);
-            if ( nDay2 < nDay1 &&
-                 !(aStart.GetDay() == aStart.GetDaysInMonth() &&
-                   aEnd.GetDay() == aEnd.GetDaysInMonth()) )
-                nMonths--;
             fResult = (double)nMonths;
         }
         else if ( aFormat == "y" )
         {
-            // Complete calendar years
-            sal_uInt32 nYear1 = aStart.GetYear();
-            sal_uInt32 nMonth1 = aStart.GetMonth();
-            sal_uInt32 nDay1 = aStart.GetDay();
-            sal_uInt32 nYear2 = aEnd.GetYear();
-            sal_uInt32 nMonth2 = aEnd.GetMonth();
-            sal_uInt32 nDay2 = aEnd.GetDay();
-
-            long nYears = (long)(nYear2 - nYear1);
-            if ( nMonth2 < nMonth1 || (nMonth2 == nMonth1 && nDay2 < nDay1) )
-                nYears--;
             fResult = (double)nYears;
         }
         else if ( aFormat == "md" )
         {
-            // Days ignoring months and years
-            sal_uInt32 nDay1 = aStart.GetDay();
-            sal_uInt32 nDay2 = aEnd.GetDay();
-
+            // Days ignoring months and years (ODF 1.4 §6.10.3).
+            // If the end day is before the start day, borrow days from the
+            // start date's month (Excel-compatible / common ODF 
interpretation).
             long nDays;
             if ( nDay2 < nDay1 )
             {
-                // Borrow days from the previous month
-                sal_uInt32 nPrevMonth = aEnd.GetMonth() - 1;
-                sal_uInt32 nPrevYear = aEnd.GetYear();
-                if ( nPrevMonth < 1 )
-                {
-                    nPrevMonth = 12;
-                    nPrevYear--;
-                }
-                Date aPrevMonth( 1, (sal_uInt16)nPrevMonth, 
(sal_uInt16)nPrevYear );
-                sal_uInt16 nPrevDays = aPrevMonth.GetDaysInMonth();
-                nDays = (long)nDay2 + (long)nPrevDays - (long)nDay1;
+                sal_uInt16 nStartDays = aFrom.GetDaysInMonth();
+                nDays = (long)nDay2 + (long)nStartDays - (long)nDay1;
             }
             else
             {
@@ -541,23 +535,24 @@ void ScInterpreter::ScDateDif()
         }
         else if ( aFormat == "ym" )
         {
-            // Months ignoring years
-            sal_uInt32 nMonth1 = aStart.GetMonth();
-            sal_uInt32 nMonth2 = aEnd.GetMonth();
-
-            long nMonths = (long)nMonth2 - (long)nMonth1;
-            if ( nMonths < 0 )
-                nMonths += 12;
-            fResult = (double)nMonths;
+            // Months ignoring years (ODF 1.4 §6.10.3): complete months
+            // remaining after subtracting complete years.
+            long nYM = nMonths - nYears * 12;
+            if ( nYM < 0 )
+                nYM = 0;
+            fResult = (double)nYM;
         }
         else if ( aFormat == "yd" )
         {
-            // Days ignoring years (day-of-year difference)
-            sal_uInt16 nDoy1 = aStart.GetDayOfYear();
-            sal_uInt16 nDoy2 = aEnd.GetDayOfYear();
-
-            long nDays = (long)nDoy2 - (long)nDoy1;
-            fResult = (double)nDays;
+            // Days ignoring years (ODF 1.4 §6.10.3): days between the start
+            // date shifted forward by the number of complete years and the end
+            // date, in the end date's year context.
+            sal_uInt16 nAnnYear = (sal_uInt16)((long)nYear2 - nYears);
+            Date aAnniversary( (sal_uInt16)nDay1, (sal_uInt16)nMonth1, 
nAnnYear );
+            // Handle anniversary like Feb 29 in a non-leap anniversary year.
+            if ( !aAnniversary.IsValid() && nMonth1 == 2 && nDay1 == 29 )
+                aAnniversary = Date( 28, 2, nAnnYear );
+            fResult = (double)(aTo - aAnniversary);
         }
         else
         {
@@ -570,18 +565,17 @@ void ScInterpreter::ScDateDif()
             return;
         }
 
-        // Handle reverse dates (start > end)
-        if ( aStart > aEnd )
+        // ODF 1.4 §6.10.3 does not define reverse intervals; apply
+        // Excel-compatible policy: negate y/m/d, keep md/ym/yd non-negative.
+        if ( bReverse )
         {
             if ( aFormat == "md" || aFormat == "ym" || aFormat == "yd" )
             {
-                // Component formats: absolute difference (Excel compatibility)
                 if ( fResult < 0.0 )
                     fResult = -fResult;
             }
             else
             {
-                // Forward formats: negate the forward result
                 fResult = -fResult;
             }
         }
diff --git a/test/odf-validate/calcval-cases14.tsv 
b/test/odf-validate/calcval-cases14.tsv
index bf49559bb2..07794bcf59 100644
--- a/test/odf-validate/calcval-cases14.tsv
+++ b/test/odf-validate/calcval-cases14.tsv
@@ -308,9 +308,35 @@ SG.DATEDIF-006-d-jan-feb29 
=DATEDIF(DATE(2024;1;1);DATE(2024;2;29);"d")    cell    59
 SG.DATEDIF-007-d-feb29-mar1    =DATEDIF(DATE(2024;2;29);DATE(2024;3;1);"d")    
cell    1       6.10.3 one day from Feb 29 to Mar 1 in leap year
 SG.DATEDIF-008-m-jan-jun       =DATEDIF(DATE(2024;1;1);DATE(2024;6;1);"m")     
cell    5       6.10.3 five complete months Jan to Jun
 SG.DATEDIF-009-d-jan-jun       =DATEDIF(DATE(2024;1;1);DATE(2024;6;1);"d")     
cell    152     6.10.3 days from Jan 1 to Jun 1 in leap year
-SG.DATEDIF-010-md      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"md")  cell    
24      6.10.3 days ignoring months/years: 10+29-15=24
-SG.DATEDIF-011-ym      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"ym")  cell    
2       6.10.3 months ignoring years: Mar-Jan=2
-SG.DATEDIF-012-yd      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"yd")  cell    
55      6.10.3 days ignoring years: day 70 - day 15 = 55
-SG.DATEDIF-013-reverse-d       =DATEDIF(DATE(2024;12;31);DATE(2024;1;1);"d")   
cell    -365    6.10.3 reverse date negates forward result
+SG.DATEDIF-010-md      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"md")  cell    
26      6.10.3 days ignoring months/years with borrow from start month 
(10+31-15)
+SG.DATEDIF-011-ym      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"ym")  cell    
1       6.10.3 months ignoring years after complete months (1m 26d)
+SG.DATEDIF-012-yd      =DATEDIF(DATE(2024;1;15);DATE(2024;3;10);"yd")  cell    
55      6.10.3 days ignoring years from anniversary to end date
+SG.DATEDIF-013-reverse-d       =DATEDIF(DATE(2024;12;31);DATE(2024;1;1);"d")   
cell    -365    6.10.3 reverse date negates forward y/m/d result
 SG.DATEDIF-014-invalid-format  =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"x")   
cell    #ERR    6.10.3 invalid format string is an error
 SG.DATEDIF-015-empty-format    =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"")    
cell    #ERR    6.10.3 empty format string is an error
+SG.DATEDIF-016-same-m  =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"m")     cell    
0       6.10.3 zero interval months
+SG.DATEDIF-017-same-y  =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"y")     cell    
0       6.10.3 zero interval years
+SG.DATEDIF-018-same-md =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"md")    cell    
0       6.10.3 zero interval md
+SG.DATEDIF-019-same-ym =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"ym")    cell    
0       6.10.3 zero interval ym
+SG.DATEDIF-020-same-yd =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"yd")    cell    
0       6.10.3 zero interval yd
+SG.DATEDIF-021-y-incomplete    =DATEDIF(DATE(2024;3;1);DATE(2025;2;28);"y")    
cell    0       6.10.3 incomplete year not counted
+SG.DATEDIF-022-y-leap-anniv    =DATEDIF(DATE(2024;2;29);DATE(2025;2;28);"y")   
cell    0       6.10.3 leap-day anniversary not reached on Feb 28 non-leap year
+SG.DATEDIF-023-y-leap-to-leap  =DATEDIF(DATE(2020;2;29);DATE(2024;2;29);"y")   
cell    4       6.10.3 four complete years including leap cycle
+SG.DATEDIF-024-m-end-of-month  =DATEDIF(DATE(2024;1;31);DATE(2024;2;29);"m")   
cell    1       6.10.3 end-of-month substitution counts full month
+SG.DATEDIF-025-m-partial-day   =DATEDIF(DATE(2024;1;31);DATE(2024;3;30);"m")   
cell    1       6.10.3 incomplete month when end day before start day
+SG.DATEDIF-026-m-eom-to-eom    =DATEDIF(DATE(2024;1;31);DATE(2024;3;31);"m")   
cell    2       6.10.3 end-of-month to end-of-month complete months
+SG.DATEDIF-027-d-nonleap-year  =DATEDIF(DATE(2023;1;1);DATE(2023;12;31);"d")   
cell    364     6.10.3 days in non-leap year span
+SG.DATEDIF-028-md-no-borrow    =DATEDIF(DATE(2024;1;10);DATE(2024;3;15);"md")  
cell    5       6.10.3 days ignoring months/years without borrow
+SG.DATEDIF-029-ym-no-borrow    =DATEDIF(DATE(2024;1;10);DATE(2024;3;15);"ym")  
cell    2       6.10.3 months ignoring years without day borrow
+SG.DATEDIF-030-yd-across-year  =DATEDIF(DATE(2023;12;15);DATE(2024;1;10);"yd") 
cell    26      6.10.3 days ignoring years across year boundary
+SG.DATEDIF-031-yd-same-year    =DATEDIF(DATE(2024;1;1);DATE(2024;7;1);"yd")    
cell    182     6.10.3 days ignoring years within same year
+SG.DATEDIF-032-reverse-m       =DATEDIF(DATE(2025;1;1);DATE(2024;1;1);"m")     
cell    -12     6.10.3 reverse interval negates complete months
+SG.DATEDIF-033-reverse-y       =DATEDIF(DATE(2025;1;1);DATE(2024;1;1);"y")     
cell    -1      6.10.3 reverse interval negates complete years
+SG.DATEDIF-034-reverse-md      =DATEDIF(DATE(2024;3;10);DATE(2024;1;15);"md")  
cell    26      6.10.3 reverse md component is absolute
+SG.DATEDIF-035-reverse-ym      =DATEDIF(DATE(2024;3;10);DATE(2024;1;15);"ym")  
cell    1       6.10.3 reverse ym component is absolute
+SG.DATEDIF-036-reverse-yd      =DATEDIF(DATE(2024;3;10);DATE(2024;1;15);"yd")  
cell    55      6.10.3 reverse yd component is absolute
+SG.DATEDIF-037-time-fraction   
=DATEDIF(DATE(2024;1;1)+0.75;DATE(2024;1;2)+0.25;"d")   cell    1       6.10.3 
time fractions are floored to calendar dates
+SG.DATEDIF-038-multi-year      =DATEDIF(DATE(2020;6;15);DATE(2024;8;20);"y")   
cell    4       6.10.3 complete years over multi-year span
+SG.DATEDIF-039-multi-year-m    =DATEDIF(DATE(2020;6;15);DATE(2024;8;20);"m")   
cell    50      6.10.3 complete months over multi-year span
+SG.DATEDIF-040-multi-year-ym   =DATEDIF(DATE(2020;6;15);DATE(2024;8;20);"ym")  
cell    2       6.10.3 months ignoring years over multi-year span
+SG.DATEDIF-041-case-format     =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"D")   
cell    #ERR    6.10.3 format string is case-sensitive

Reply via email to