1*4f909564SJim Jagielski<!-- 2*4f909564SJim Jagielski Licensed to the Apache Software Foundation (ASF) under one 3*4f909564SJim Jagielski or more contributor license agreements. See the NOTICE file 4*4f909564SJim Jagielski distributed with this work for additional information 5*4f909564SJim Jagielski regarding copyright ownership. The ASF licenses this file 6*4f909564SJim Jagielski to you under the Apache License, Version 2.0 (the 7*4f909564SJim Jagielski "License"); you may not use this file except in compliance 8*4f909564SJim Jagielski with the License. You may obtain a copy of the License at 9*4f909564SJim Jagielski 10*4f909564SJim Jagielski http://www.apache.org/licenses/LICENSE-2.0 11*4f909564SJim Jagielski 12*4f909564SJim Jagielski Unless required by applicable law or agreed to in writing, 13*4f909564SJim Jagielski software distributed under the License is distributed on an 14*4f909564SJim Jagielski "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15*4f909564SJim Jagielski KIND, either express or implied. See the License for the 16*4f909564SJim Jagielski specific language governing permissions and limitations 17*4f909564SJim Jagielski under the License. 18*4f909564SJim Jagielski--> 19*4f909564SJim Jagielski 20f8b15cbbSPedro GiffuniDATEDIF Semantic Design — ODF 1.4 §6.10.3 21f8b15cbbSPedro Giffuni========================================== 22f8b15cbbSPedro Giffuni 23f8b15cbbSPedro GiffuniBranch: openformula 24f8b15cbbSPedro GiffuniHEAD: 0224e3dc6c (H.1/H.2 complete) 25f8b15cbbSPedro Giffuni 26*4f909564SJim JagielskiUpdated 2026-11-18 to match the code after the post-merge review. Jim chose 27*4f909564SJim JagielskiExcel-compatible behavior for the cases ODF leaves open; decisions 3, 4 and 8 28*4f909564SJim Jagielskiand the "md"/"yd" algorithms below were changed to what the code does now. 29*4f909564SJim JagielskiThe section "2026-11-18: as implemented" at the end is the current reference. 30*4f909564SJim Jagielski 31f8b15cbbSPedro GiffuniThis document specifies the semantics for the DATEDIF function implementation 32f8b15cbbSPedro Giffuniin Apache OpenOffice Calc, conforming to ODF 1.4 Part 4, Section 6.10.3. 33f8b15cbbSPedro Giffuni 34f8b15cbbSPedro GiffuniNormative Reference 35f8b15cbbSPedro Giffuni------------------- 36*4f909564SJim Jagielski 37f8b15cbbSPedro GiffuniODF 1.4 OpenFormula, Section 6.10.3 — DATEDIF: Difference between two dates. 38f8b15cbbSPedro Giffuni 39f8b15cbbSPedro GiffuniFunction Signature 40f8b15cbbSPedro Giffuni------------------ 41*4f909564SJim Jagielski 42f8b15cbbSPedro GiffuniDATEDIF( StartDate ; EndDate ; Format ) 43f8b15cbbSPedro Giffuni 44f8b15cbbSPedro Giffuni- StartDate: Date — the start of the interval. 45f8b15cbbSPedro Giffuni- EndDate: Date — the end of the interval. 46f8b15cbbSPedro Giffuni- Format: Text — one of "y", "m", "d", "md", "ym", "yd". 47f8b15cbbSPedro Giffuni 48f8b15cbbSPedro GiffuniResult: Number — the difference between the two dates in the units specified 49f8b15cbbSPedro Giffuniby Format. 50f8b15cbbSPedro Giffuni 51f8b15cbbSPedro GiffuniODF 1.4 §6.10.3 Defined Formats 52f8b15cbbSPedro Giffuni------------------------------- 53*4f909564SJim Jagielski 54f8b15cbbSPedro Giffuni| Format | Meaning | 55f8b15cbbSPedro Giffuni|--------|----------------------------| 56f8b15cbbSPedro Giffuni| y | Number of complete years | 57f8b15cbbSPedro Giffuni| m | Number of complete months | 58f8b15cbbSPedro Giffuni| d | Number of days | 59f8b15cbbSPedro Giffuni| md | Days ignoring months/years | 60f8b15cbbSPedro Giffuni| ym | Months ignoring years | 61f8b15cbbSPedro Giffuni| yd | Days ignoring years | 62f8b15cbbSPedro Giffuni 63f8b15cbbSPedro GiffuniSemantic Decisions 64f8b15cbbSPedro Giffuni------------------ 65f8b15cbbSPedro Giffuni 66f8b15cbbSPedro Giffuni1. Start date earlier than end date: Normal forward interval. Compute the 67f8b15cbbSPedro Giffuni difference normally for all formats. 68f8b15cbbSPedro Giffuni 69f8b15cbbSPedro Giffuni2. Start date equal to end date: Zero-length interval. All formats return 0. 70f8b15cbbSPedro Giffuni 71f8b15cbbSPedro Giffuni3. Start date later than end date (reverse interval): The ODF specification 72*4f909564SJim Jagielski does not define this case. Excel 16 returns #NUM! for every format 73*4f909564SJim Jagielski (checked). Policy (2026-11-18): an error (Err:502), as Excel. The earlier 74*4f909564SJim Jagielski policy of negating "y", "m", "d" and returning absolute components was 75*4f909564SJim Jagielski dropped. 76f8b15cbbSPedro Giffuni 77f8b15cbbSPedro Giffuni4. End-of-month dates: When computing complete months ("m"), a month is 78f8b15cbbSPedro Giffuni complete only when the end date's day is >= the start date's day within the 79f8b15cbbSPedro Giffuni same month context. If the end date's day is less than the start date's day 80*4f909564SJim Jagielski (e.g., Jan 31 to Feb 28), the last partial month is not counted. Policy 81*4f909564SJim Jagielski (2026-11-18): a month is complete only when the end day is >= the start 82*4f909564SJim Jagielski day. ODF 1.2 and 1.4 say "If there is not a complete month between the 83*4f909564SJim Jagielski dates, 0 will be returned", and Excel gives 0 for Jan 31 to Feb 29, 2024. 84*4f909564SJim Jagielski The end-of-month substitution was dropped. 85f8b15cbbSPedro Giffuni 86f8b15cbbSPedro Giffuni5. February 28/29 and leap years: Leap-day anniversaries are handled by 87*4f909564SJim Jagielski comparing month/day pairs. Policy: compare month/day pairs directly, so 88*4f909564SJim Jagielski Feb 29, 2024 to Feb 28, 2025 is 0 complete years (Excel also gives 0). 89f8b15cbbSPedro Giffuni 90f8b15cbbSPedro Giffuni6. Fractional time components: Date serials may contain fractional parts 91f8b15cbbSPedro Giffuni representing time of day. DATEDIF operates on calendar dates by discarding 92f8b15cbbSPedro Giffuni time fractions (floor to midnight). Time components do not affect the 93f8b15cbbSPedro Giffuni result. Policy: Floor both dates to integer serials before computation. 94f8b15cbbSPedro Giffuni 95f8b15cbbSPedro Giffuni7. Null date: The document's configured null date is used via 96f8b15cbbSPedro Giffuni pFormatter->GetNullDate(). No assumption is made about the serial epoch. 97f8b15cbbSPedro Giffuni 98f8b15cbbSPedro Giffuni8. Invalid format strings: Any format string other than "y", "m", "d", "md", 99*4f909564SJim Jagielski "ym", "yd" produces an error result. Upper and lower case are equivalent 100*4f909564SJim Jagielski (2026-11-18), as in Excel; ODF does not say. 101f8b15cbbSPedro Giffuni 102f8b15cbbSPedro Giffuni9. Missing arguments / invalid argument types: Fewer than 3 arguments, or 103f8b15cbbSPedro Giffuni non-date arguments, produce an error result. 104f8b15cbbSPedro Giffuni 105f8b15cbbSPedro Giffuni10. Date serials outside supported range: Serials that produce invalid Date 106f8b15cbbSPedro Giffuni objects produce an error result. 107f8b15cbbSPedro Giffuni 108f8b15cbbSPedro GiffuniAlgorithm Specification 109f8b15cbbSPedro Giffuni----------------------- 110f8b15cbbSPedro Giffuni 111*4f909564SJim Jagielski```text 112f8b15cbbSPedro GiffuniHelper: date_from_serial(serial) -> Date 113f8b15cbbSPedro Giffuni null = pFormatter->GetNullDate() 114f8b15cbbSPedro Giffuni return null + floor(serial) 115f8b15cbbSPedro Giffuni 116f8b15cbbSPedro GiffuniHelper: is_last_day_of_month(date) -> bool 117f8b15cbbSPedro Giffuni return date.GetDay() == date.GetDaysInMonth() 118f8b15cbbSPedro Giffuni 119f8b15cbbSPedro GiffuniFormat "d" — Calendar day difference: 120f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 121f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 122f8b15cbbSPedro Giffuni return (d2 - d1) in days (serial difference, floored) 123f8b15cbbSPedro Giffuni For reverse: compute forward, negate. 124f8b15cbbSPedro Giffuni 125f8b15cbbSPedro GiffuniFormat "m" — Complete calendar months: 126f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 127f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 128f8b15cbbSPedro Giffuni months = (d2.GetYear() - d1.GetYear()) * 12 + (d2.GetMonth() - d1.GetMonth()) 129f8b15cbbSPedro Giffuni if d2.GetDay() < d1.GetDay(): 130f8b15cbbSPedro Giffuni months -= 1 131f8b15cbbSPedro Giffuni elif d2.GetDay() >= d1.GetDay() and is_last_day_of_month(d1) and is_last_day_of_month(d2): 132f8b15cbbSPedro Giffuni # both end-of-month: count the full month 133f8b15cbbSPedro Giffuni pass 134f8b15cbbSPedro Giffuni # clamp to non-negative for forward intervals 135f8b15cbbSPedro Giffuni For reverse: compute forward, negate. 136f8b15cbbSPedro Giffuni 137f8b15cbbSPedro GiffuniFormat "y" — Complete calendar years: 138f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 139f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 140f8b15cbbSPedro Giffuni years = d2.GetYear() - d1.GetYear() 141f8b15cbbSPedro Giffuni if (d2.GetMonth(), d2.GetDay()) < (d1.GetMonth(), d1.GetDay()): 142f8b15cbbSPedro Giffuni years -= 1 143f8b15cbbSPedro Giffuni # Feb 29 anniversary in non-leap year: month/day (2,28) < (2,29) handled 144f8b15cbbSPedro Giffuni # naturally by tuple comparison above 145f8b15cbbSPedro Giffuni For reverse: compute forward, negate. 146f8b15cbbSPedro Giffuni 147*4f909564SJim JagielskiThe algorithms below are the original design. Where they differ from the 148*4f909564SJim Jagielskicode, the code (see "2026-11-18: as implemented") wins; "md" and "yd" and the 149*4f909564SJim Jagielskireverse-interval handling differ. 150*4f909564SJim Jagielski 151f8b15cbbSPedro GiffuniFormat "md" — Days ignoring months/years: 152f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 153f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 154f8b15cbbSPedro Giffuni # Compare day-of-month, adjusting for month boundaries 155f8b15cbbSPedro Giffuni day1 = d1.GetDay() 156f8b15cbbSPedro Giffuni day2 = d2.GetDay() 157f8b15cbbSPedro Giffuni # If end day < start day, borrow from previous month 158f8b15cbbSPedro Giffuni if day2 < day1: 159f8b15cbbSPedro Giffuni # Compute the last day of the month before d2's month 160f8b15cbbSPedro Giffuni prev_month = d2.GetMonth() - 1 161f8b15cbbSPedro Giffuni prev_year = d2.GetYear() 162f8b15cbbSPedro Giffuni if prev_month < 1: 163f8b15cbbSPedro Giffuni prev_month = 12 164f8b15cbbSPedro Giffuni prev_year -= 1 165f8b15cbbSPedro Giffuni Date prev_last(prev_month's last day, prev_month, prev_year) 166f8b15cbbSPedro Giffuni day_diff = day2 + prev_last.GetDaysInMonth() - day1 167f8b15cbbSPedro Giffuni else: 168f8b15cbbSPedro Giffuni day_diff = day2 - day1 169f8b15cbbSPedro Giffuni return abs(day_diff) for reverse-date compatibility 170f8b15cbbSPedro Giffuni 171f8b15cbbSPedro GiffuniFormat "ym" — Months ignoring years: 172f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 173f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 174f8b15cbbSPedro Giffuni months = d2.GetMonth() - d1.GetMonth() 175f8b15cbbSPedro Giffuni # If end month < start month, this is a partial year 176f8b15cbbSPedro Giffuni # The month component ignoring years: if we've passed the start month 177f8b15cbbSPedro Giffuni # in the end year, count it; otherwise count from previous year 178f8b15cbbSPedro Giffuni if months < 0: 179f8b15cbbSPedro Giffuni months += 12 180f8b15cbbSPedro Giffuni return abs(months) for reverse-date compatibility 181f8b15cbbSPedro Giffuni 182f8b15cbbSPedro GiffuniFormat "yd" — Days ignoring years: 183f8b15cbbSPedro Giffuni d1 = date_from_serial(StartDate) 184f8b15cbbSPedro Giffuni d2 = date_from_serial(EndDate) 185f8b15cbbSPedro Giffuni # Compute day-of-year difference, wrapping at year boundary 186f8b15cbbSPedro Giffuni doy1 = d1.GetDayOfYear() 187f8b15cbbSPedro Giffuni doy2 = d2.GetDayOfYear() 188f8b15cbbSPedro Giffuni day_diff = doy2 - doy1 189f8b15cbbSPedro Giffuni return abs(day_diff) for reverse-date compatibility 190*4f909564SJim Jagielski``` 191f8b15cbbSPedro Giffuni 192f8b15cbbSPedro GiffuniImplementation Notes 193f8b15cbbSPedro Giffuni-------------------- 194*4f909564SJim Jagielski 195f8b15cbbSPedro Giffuni- Use ::rtl::math::approxFloor() for flooring serials (consistent with 196f8b15cbbSPedro Giffuni ScGetDiffDate360 in interpr2.cxx). 197f8b15cbbSPedro Giffuni- Use pFormatter->GetNullDate() for the document's null date. 198f8b15cbbSPedro Giffuni- Use Date class methods: GetDay(), GetMonth(), GetYear(), GetDaysInMonth(), 199f8b15cbbSPedro Giffuni IsLeapYear(), GetDayOfYear(), operator+=, comparison operators. 200f8b15cbbSPedro Giffuni- Do not use fabs() to conceal negative results; handle sign explicitly. 201f8b15cbbSPedro Giffuni- Do not approximate months as 30 days or years as 365 days; use calendar 202f8b15cbbSPedro Giffuni arithmetic. 203f8b15cbbSPedro Giffuni- Validate exactly 3 arguments via MustHaveParamCount. 204*4f909564SJim Jagielski- (Superseded 2026-11-18: the format is case-insensitive.) 205f8b15cbbSPedro Giffuni 206f8b15cbbSPedro GiffuniError Handling 207f8b15cbbSPedro Giffuni-------------- 208*4f909564SJim Jagielski 209f8b15cbbSPedro Giffuni- Wrong argument count: error via MustHaveParamCount. 210f8b15cbbSPedro Giffuni- Non-date arguments: error via GetDouble() validation + PushIllegalArgument. 211f8b15cbbSPedro Giffuni- Invalid format string: PushIllegalArgument(). 212*4f909564SJim Jagielski- Reverse dates (start > end): Err:502 (2026-11-18; Excel gives #NUM!). 213f8b15cbbSPedro Giffuni 214f8b15cbbSPedro GiffuniFiles to Modify 215f8b15cbbSPedro Giffuni--------------- 216*4f909564SJim Jagielski 217f8b15cbbSPedro Giffuni1. main/formula/inc/formula/compiler.hrc — Add SC_OPCODE_DATEDIF = 415 218f8b15cbbSPedro Giffuni2. main/formula/inc/formula/opcode.hxx — Add ocDateDif = SC_OPCODE_DATEDIF 219f8b15cbbSPedro Giffuni3. main/formula/source/core/resource/core_resource.src — Register "DATEDIF" name 220f8b15cbbSPedro Giffuni4. main/sc/source/core/tool/interpr2.cxx — Add ScInterpreter::ScDateDif() 221f8b15cbbSPedro Giffuni5. main/sc/source/core/tool/interpr4.cxx — Add dispatch case ocDateDif 222f8b15cbbSPedro Giffuni6. main/sc/source/ui/src/scfuncs.src — Function Wizard entry 223f8b15cbbSPedro Giffuni7. main/sc/inc/helpids.h — Add HID_FUNC_DATEDIF 224f8b15cbbSPedro Giffuni8. main/sc/util/hidother.src — Register Help ID 225f8b15cbbSPedro Giffuni9. test/odf-validate/calcval-cases14.tsv — Regression tests 226f8b15cbbSPedro Giffuni10. audit/openformula-function-gaps/ — Update audit records 22762e7f86eSPedro Giffuni 22862e7f86eSPedro GiffuniH.3.1 Implementation/Review Notes (2026-11-17) 22962e7f86eSPedro Giffuni---------------------------------------------- 230*4f909564SJim Jagielski 23162e7f86eSPedro GiffuniImplementation: `ScInterpreter::ScDateDif()` in 23262e7f86eSPedro Giffuni`main/sc/source/core/tool/interpr2.cxx`, declared in 23362e7f86eSPedro Giffuni`main/sc/source/core/inc/interpre.hxx`, dispatched from 23462e7f86eSPedro Giffuni`main/sc/source/core/tool/interpr4.cxx` (`ocDateDif`), with resources/help IDs 23562e7f86eSPedro Giffuniregistered and BIFF5/7 import mapped import-only (`EXC_FUNCFLAG_IMPORTONLY`) in 23662e7f86eSPedro Giffuni`main/sc/source/filter/excel/xlformula.cxx` (ID 351 → `ocDateDif`). 23762e7f86eSPedro Giffuni 23862e7f86eSPedro GiffuniSemantics as implemented (ODF 1.4 §6.10.3): 239*4f909564SJim Jagielski 24062e7f86eSPedro Giffuni- Exactly 3 arguments: StartDate, EndDate, Format (popped Format, EndDate, 24162e7f86eSPedro Giffuni StartDate). 24262e7f86eSPedro Giffuni- Date serials floored to calendar dates via `::rtl::math::approxFloor()`; time 24362e7f86eSPedro Giffuni fractions discarded. Calendar conversion uses `pFormatter->GetNullDate()`. 24462e7f86eSPedro Giffuni- Invalid/unsupported date serials (`Date::IsValid() == false`) → illegal 24562e7f86eSPedro Giffuni argument error. 24662e7f86eSPedro Giffuni- Format is case-sensitive: only `y`, `m`, `d`, `md`, `ym`, `yd` accepted; 24762e7f86eSPedro Giffuni any other value (including `Y/M/D` and empty string) → illegal argument error. 24862e7f86eSPedro Giffuni- Forward intervals (`StartDate <= EndDate`): `d` = calendar day difference; 24962e7f86eSPedro Giffuni `y` = complete calendar years; `m` = complete calendar months with 25062e7f86eSPedro Giffuni end-of-month substitution when the start day is the last day of its month; 25162e7f86eSPedro Giffuni `ym` = `m - y*12` (months remaining after complete years); 25262e7f86eSPedro Giffuni `md` = day difference ignoring months/years, borrowing from the start date's 25362e7f86eSPedro Giffuni month when end day < start day; 25462e7f86eSPedro Giffuni `yd` = days from the anniversary (start date shifted forward by complete 25562e7f86eSPedro Giffuni years, adjusted for Feb 29 in non-leap anniversary years) to end date. 25662e7f86eSPedro Giffuni- Reverse intervals (`StartDate > EndDate`): computed on the swapped forward 25762e7f86eSPedro Giffuni interval. `y`, `m`, `d` return the negated forward result; `md`, `ym`, `yd` 25862e7f86eSPedro Giffuni return a non-negative component value (Excel-compatible policy; ODF 1.4 does 25962e7f86eSPedro Giffuni not define reverse intervals). This is an implementation constraint/policy, 26062e7f86eSPedro Giffuni not a normative ODF requirement. 26162e7f86eSPedro Giffuni 26262e7f86eSPedro GiffuniValidation notes: 263*4f909564SJim Jagielski 26462e7f86eSPedro Giffuni- Source-level review/static verification only in this environment; Calc and 26562e7f86eSPedro Giffuni calcval runtime tests cannot be executed because `libsot.so` / `sc`/`scaddins` 26662e7f86eSPedro Giffuni are not built (GCC 15.2 vs AOO 510 ABI mismatch, no older GCC, no sudo). 26762e7f86eSPedro Giffuni- `SG.DATEDIF-010-md` expected value corrected to `26` (borrow from start 26862e7f86eSPedro Giffuni month January: `10 + 31 - 15`) and DATEDIF regression coverage expanded in 26962e7f86eSPedro Giffuni `test/odf-validate/calcval-cases14.tsv`. 270*4f909564SJim Jagielski 271*4f909564SJim JagielskiThe H.3.1 notes above describe the code as of 62e7f86e31. Of those, the 272*4f909564SJim Jagielskiend-of-month substitution, the reverse-interval policy, the case-sensitive 273*4f909564SJim Jagielskiformat, the "md" borrow from the start month and the `26` expectation for 274*4f909564SJim Jagielski`SG.DATEDIF-010-md` no longer apply. 275*4f909564SJim Jagielski 276*4f909564SJim Jagielski2026-11-18: as implemented 277*4f909564SJim Jagielski-------------------------- 278*4f909564SJim Jagielski 279*4f909564SJim Jagielski`ScInterpreter::ScDateDif()` in `main/sc/source/core/tool/interpr2.cxx`. 280*4f909564SJim Jagielskixls: BIFF 351 is read and written (`xlformula.cxx`, no import-only flag). 281*4f909564SJim Jagielskixlsx: read through the `DATEDIF` row of `oox/source/xls/formulabase.cxx`. 282*4f909564SJim Jagielski 283*4f909564SJim Jagielski- Arguments popped as Format, EndDate, StartDate; both dates floored with 284*4f909564SJim Jagielski `::rtl::math::approxFloor()`; an error in an argument is passed on. 285*4f909564SJim Jagielski- StartDate > EndDate: Err:502. Invalid `Date`: Err:502. Unknown format: 286*4f909564SJim Jagielski Err:502. The format is lowered with `ToLowerAscii()` first. 287*4f909564SJim Jagielski- Complete months: `(y2-y1)*12 + (m2-m1)`, minus 1 when the end day is before 288*4f909564SJim Jagielski the start day. 289*4f909564SJim Jagielski- "d": day difference. "m": complete months. "y": complete months / 12. 290*4f909564SJim Jagielski "ym": complete months % 12. 291*4f909564SJim Jagielski- "md": `d2 - d1`; when negative, add the length of the month before the end 292*4f909564SJim Jagielski month. This is Excel's rule and can be negative: Jan 31 to Mar 1, 2024 is -1 293*4f909564SJim Jagielski (Excel 16 gives -1 too). 294*4f909564SJim Jagielski- "yd": the end date is moved into the start year, or into the next year when 295*4f909564SJim Jagielski its month/day comes before the start's; Feb 29 moved into a common year 296*4f909564SJim Jagielski becomes Mar 1. Result: that date minus StartDate. This is Excel's rule: 297*4f909564SJim Jagielski May 3, 1995 to Apr 28, 2009 is 361 (Excel 361). 298*4f909564SJim Jagielski 299*4f909564SJim JagielskiChecked against Excel 16 on 630 random and hand-picked cases: one difference, 300*4f909564SJim Jagielski"yd" from Feb 29, 2020 to Mar 1, 2023 (AOO 1, Excel 0). Tests: the `DATEDIF.*` 301*4f909564SJim Jagielskirows at the end of `test/odf-validate/calcval-cases14.tsv`, all matching on 302*4f909564SJim Jagielskithe Mac and Linux builds. 303