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
commit f8b15cbbe30249a9fbf72dc4ebd904ff5e78e14a Author: Pedro Giffuni <[email protected]> AuthorDate: Tue Nov 17 14:13:18 2026 -0500 impl: Add DATEDIF function (H.3) Implement ODF 1.4 §6.10.3 DATEDIF natively in Calc: - New opcode SC_OPCODE_DATEDIF = 415 (existing opcodes unchanged; STOP_2_PAR moved to 416, LAST_OPCODE_ID to 415) - ScInterpreter::ScDateDif() evaluator in interpr2.cxx implementing all six formats (y, m, d, md, ym, yd) using calendar arithmetic (not day-count approximation) - Document-aware null date via pFormatter->GetNullDate() - Floor-based time fraction handling - Case-sensitive format matching with invalid format rejection - Reverse-date handling: y/m/d negated, md/ym/yd absolute - Function Wizard entry, help ID, and 15 regression test cases - Semantic design documented in audit/openformula-function-gaps/ No build or runtime testing performed (GCC 15.2 ABI incompatibility blocks AOO compilation). Runtime validation deferred. --- .../datedif-semantic-design.md | 195 +++++++++++++++++++++ .../function-gap-inventory.md | 2 +- .../function-gap-summary.tsv | 2 +- .../function-implementation-roadmap.md | 67 ++++--- .../function-source-audit.md | 72 ++++---- main/formula/inc/formula/compiler.hrc | 5 +- main/formula/inc/formula/opcode.hxx | 1 + .../formula/source/core/resource/core_resource.src | 6 + main/sc/inc/helpids.h | 1 + main/sc/source/core/tool/interpr2.cxx | 150 ++++++++++++++++ main/sc/source/core/tool/interpr4.cxx | 1 + main/sc/source/ui/src/scfuncs.src | 41 +++++ main/sc/util/hidother.src | 1 + test/odf-validate/calcval-cases14.tsv | 15 ++ 14 files changed, 494 insertions(+), 65 deletions(-) diff --git a/audit/openformula-function-gaps/datedif-semantic-design.md b/audit/openformula-function-gaps/datedif-semantic-design.md new file mode 100644 index 0000000000..8f78d0f9d0 --- /dev/null +++ b/audit/openformula-function-gaps/datedif-semantic-design.md @@ -0,0 +1,195 @@ +DATEDIF Semantic Design — ODF 1.4 §6.10.3 +========================================== + +Branch: openformula +HEAD: 0224e3dc6c (H.1/H.2 complete) + +This document specifies the semantics for the DATEDIF function implementation +in Apache OpenOffice Calc, conforming to ODF 1.4 Part 4, Section 6.10.3. + +Normative Reference +------------------- +ODF 1.4 OpenFormula, Section 6.10.3 — DATEDIF: Difference between two dates. + +Function Signature +------------------ +DATEDIF( StartDate ; EndDate ; Format ) + +- StartDate: Date — the start of the interval. +- EndDate: Date — the end of the interval. +- Format: Text — one of "y", "m", "d", "md", "ym", "yd". + +Result: Number — the difference between the two dates in the units specified +by Format. + +ODF 1.4 §6.10.3 Defined Formats +------------------------------- +| Format | Meaning | +|--------|----------------------------| +| y | Number of complete years | +| m | Number of complete months | +| d | Number of days | +| md | Days ignoring months/years | +| ym | Months ignoring years | +| yd | Days ignoring years | + +Semantic Decisions +------------------ + +1. Start date earlier than end date: Normal forward interval. Compute the + difference normally for all formats. + +2. Start date equal to end date: Zero-length interval. All formats return 0. + +3. Start date later than end date (reverse interval): The ODF specification + does not explicitly define this case. Excel returns a negative value for + forward-calculated formats ("d", "m", "y") computed as if the dates were + swapped then negated. For component formats ("md", "ym", "yd"), Excel + computes the component difference without sign consideration, which can + produce unexpected results. Policy: For forward formats ("y", "m", "d"), + swap dates, compute forward result, negate. For component formats ("md", + "ym", "yd"), compute the absolute component difference (consistent with + Excel's observed behavior). This is an Excel-compatibility choice, not an + ODF normative requirement. + +4. End-of-month dates: When computing complete months ("m"), a month is + complete only when the end date's day is >= the start date's day within the + same month context. If the end date's day is less than the start date's day + (e.g., Jan 31 to Feb 28), the last partial month is not counted. Policy: + Follow Excel's documented behavior — a month is complete when the end day + is >= the start day, OR when the end date is the last day of its month and + the start date was the last day of its month. + +5. February 28/29 and leap years: Leap-day anniversaries are handled by + comparing month/day pairs. Feb 29 to Feb 28 in a non-leap year counts as + one complete year if the anniversary date (Feb 29) would fall on Feb 28 in + the target year. Policy: For year computations, compare month/day pairs + directly; Feb 29 matches Feb 28 in non-leap years for anniversary purposes. + +6. Fractional time components: Date serials may contain fractional parts + representing time of day. DATEDIF operates on calendar dates by discarding + time fractions (floor to midnight). Time components do not affect the + result. Policy: Floor both dates to integer serials before computation. + +7. Null date: The document's configured null date is used via + pFormatter->GetNullDate(). No assumption is made about the serial epoch. + +8. Invalid format strings: Any format string other than "y", "m", "d", "md", + "ym", "yd" (case-sensitive) produces an error result. + +9. Missing arguments / invalid argument types: Fewer than 3 arguments, or + non-date arguments, produce an error result. + +10. Date serials outside supported range: Serials that produce invalid Date + objects produce an error result. + +Algorithm Specification +----------------------- + +Helper: date_from_serial(serial) -> Date + null = pFormatter->GetNullDate() + return null + floor(serial) + +Helper: is_last_day_of_month(date) -> bool + return date.GetDay() == date.GetDaysInMonth() + +Format "d" — Calendar day difference: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + return (d2 - d1) in days (serial difference, floored) + For reverse: compute forward, negate. + +Format "m" — Complete calendar months: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + months = (d2.GetYear() - d1.GetYear()) * 12 + (d2.GetMonth() - d1.GetMonth()) + if d2.GetDay() < d1.GetDay(): + months -= 1 + elif d2.GetDay() >= d1.GetDay() and is_last_day_of_month(d1) and is_last_day_of_month(d2): + # both end-of-month: count the full month + pass + # clamp to non-negative for forward intervals + For reverse: compute forward, negate. + +Format "y" — Complete calendar years: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + years = d2.GetYear() - d1.GetYear() + if (d2.GetMonth(), d2.GetDay()) < (d1.GetMonth(), d1.GetDay()): + years -= 1 + # Feb 29 anniversary in non-leap year: month/day (2,28) < (2,29) handled + # naturally by tuple comparison above + For reverse: compute forward, negate. + +Format "md" — Days ignoring months/years: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + # Compare day-of-month, adjusting for month boundaries + day1 = d1.GetDay() + day2 = d2.GetDay() + # If end day < start day, borrow from previous month + if day2 < day1: + # Compute the last day of the month before d2's month + prev_month = d2.GetMonth() - 1 + prev_year = d2.GetYear() + if prev_month < 1: + prev_month = 12 + prev_year -= 1 + Date prev_last(prev_month's last day, prev_month, prev_year) + day_diff = day2 + prev_last.GetDaysInMonth() - day1 + else: + day_diff = day2 - day1 + return abs(day_diff) for reverse-date compatibility + +Format "ym" — Months ignoring years: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + months = d2.GetMonth() - d1.GetMonth() + # If end month < start month, this is a partial year + # The month component ignoring years: if we've passed the start month + # in the end year, count it; otherwise count from previous year + if months < 0: + months += 12 + return abs(months) for reverse-date compatibility + +Format "yd" — Days ignoring years: + d1 = date_from_serial(StartDate) + d2 = date_from_serial(EndDate) + # Compute day-of-year difference, wrapping at year boundary + doy1 = d1.GetDayOfYear() + doy2 = d2.GetDayOfYear() + day_diff = doy2 - doy1 + return abs(day_diff) for reverse-date compatibility + +Implementation Notes +-------------------- +- Use ::rtl::math::approxFloor() for flooring serials (consistent with + ScGetDiffDate360 in interpr2.cxx). +- Use pFormatter->GetNullDate() for the document's null date. +- Use Date class methods: GetDay(), GetMonth(), GetYear(), GetDaysInMonth(), + IsLeapYear(), GetDayOfYear(), operator+=, comparison operators. +- Do not use fabs() to conceal negative results; handle sign explicitly. +- Do not approximate months as 30 days or years as 365 days; use calendar + arithmetic. +- Validate exactly 3 arguments via MustHaveParamCount. +- Format argument is case-sensitive per ODF; "Y", "M", "D" etc. are invalid. + +Error Handling +-------------- +- Wrong argument count: error via MustHaveParamCount. +- Non-date arguments: error via GetDouble() validation + PushIllegalArgument. +- Invalid format string: PushIllegalArgument(). +- Reverse dates (start > end): Not an error; handled per policy above. + +Files to Modify +--------------- +1. main/formula/inc/formula/compiler.hrc — Add SC_OPCODE_DATEDIF = 415 +2. main/formula/inc/formula/opcode.hxx — Add ocDateDif = SC_OPCODE_DATEDIF +3. main/formula/source/core/resource/core_resource.src — Register "DATEDIF" name +4. main/sc/source/core/tool/interpr2.cxx — Add ScInterpreter::ScDateDif() +5. main/sc/source/core/tool/interpr4.cxx — Add dispatch case ocDateDif +6. main/sc/source/ui/src/scfuncs.src — Function Wizard entry +7. main/sc/inc/helpids.h — Add HID_FUNC_DATEDIF +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 diff --git a/audit/openformula-function-gaps/function-gap-inventory.md b/audit/openformula-function-gaps/function-gap-inventory.md index c64a3d8657..6869e2a75b 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 | Missing (import→#NAME!) | ocNoName | None | None | No | N/A | Yes (BIFF5/7, ID 351, →#NAME!) | No | No | No | Import maps to ocNoName → #NAME! | P1 | HIGH | +| DATEDIF | Present | ocDateDif (415) | ScDateDif() | Yes (HID_FUNC_DATEDIF) | Yes | Large Group | Yes(BIFF5/7,ID351,→#NAME!) | No | No | No | None — implementation complete at source level | Resolved (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 b41a28fd4d..fb03ac8752 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 No N/A ocNoName None None None Yes(BIFF5/7,ID351,→#NAME!) No No No Import maps to ocNoName → #NAME! P1 HIGH xlformula.cxx:342, EXC_FUNCFLAG_IMPORTONLY; comment says "Unsupported: DATEDIF"; no evaluator +DATEDIF Yes Large Group ocDateDif(415) DATEDIF ScDateDif() Yes (HID_FUNC_DATEDIF) Yes(BIFF5/7,ID351,→#NAME!) No No No None — implementation complete at source level Resolved (was P1) HIGH compiler.hrc:415; interpr4.cxx:3775 dispatch, interpr2.cxx:443 evaluator; ODF 6.10.3 conformant; BIFF import still maps to ocNoName 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-implementation-roadmap.md b/audit/openformula-function-gaps/function-implementation-roadmap.md index ddb7aecd51..b010a3a89c 100644 --- a/audit/openformula-function-gaps/function-implementation-roadmap.md +++ b/audit/openformula-function-gaps/function-implementation-roadmap.md @@ -49,32 +49,46 @@ Rationale: These three functions were entirely missing from the byte- B-function patterns. -Group 2 — Date/Time Compatibility (DATEDIF, ISOWEEKNUM) -------------------------------------------------------- +Group 2 — Date/Time Compatibility (DATEDIF, ISOWEEKNUM) — DATEDIF COMPLETED +-------------------------------------------------------------------------------- -Recommended sequence: - 1. DATEDIF: - a. Add SC_OPCODE_DATEDIF to compiler.hrc, core_resource.src. - b. Implement ScDateDif() in interpr*.cxx with full unit code - support ("d", "m", "y", "md", "yd", "ym") and edge cases - (Feb 29, month boundaries, end<start). - c. Register help entry (English name). - d. Add Excel BIFF/OOXML import/export mappings. - e. Add test cases. - 2. ISOWEEKNUM: - a. Verify ScGetWeekOfYear() (interpr2.cxx:227) does NOT implement - ISO 8601 (only SUN/MON start via Date::GetWeekOfYear()). - b. Implement ISO 8601 week numbering (week 1 = first Thursday, - Monday start, 52/53 week years) — either in ScGetWeekOfYear() - with mode=21 or as a separate code path. - c. Add English help entry (currently only German KALENDERWOCHE). - d. Verify Excel BIFF import mapping (currently not in BIFF tables). - e. Add test cases. - -Rationale: DATEDIF is entirely missing. ISOWEEKNUM has an opcode but - wrong semantics (not ISO 8601). These belong together as a date/time - compatibility group. DATEDIF is not ODF-defined but is an ODF Large - Group equivalent (Excel function). +DATEDIF Status: Implemented in H.3. + +Changes: + 1. Added SC_OPCODE_DATEDIF = 415 to compiler.hrc. Moved STOP_2_PAR to 416 + and LAST_OPCODE_ID to 415. Existing opcodes unchanged. + 2. Added ocDateDif to opcode.hxx enum. + 3. Registered "DATEDIF" name in core_resource.src (ODF and Excel sections, + plus localized en-US text). + 4. Added HID_FUNC_DATEDIF to helpids.h and hidother.src. + 5. Added dispatch case in interpr4.cxx (line 3775). + 6. Implemented ScInterpreter::ScDateDif() in interpr2.cxx (line 443) + implementing ODF 1.4 §6.10.3 with all six formats (y, m, d, md, ym, yd). + 7. Added Function Wizard entry in scfuncs.src (Start_date, End_date, Format + parameters, ID_FUNCTION_GRP_DATETIME group). + 8. Added 15 regression test cases to calcval-cases14.tsv covering + basic days/months/years, same date, month boundaries, leap year dates, + component formats (md/ym/yd), reverse dates, and invalid formats. + +Semantic design: See datedif-semantic-design.md for detailed algorithm +specification including reverse-date policy, time fraction handling, and +month/year boundary semantics. + +ISOWEEKNUM: Still pending (see below). + +Recommended sequence for ISOWEEKNUM: + 1. Verify ScGetWeekOfYear() (interpr2.cxx:227) does NOT implement + ISO 8601 (only SUN/MON start via Date::GetWeekOfYear()). + 2. Implement ISO 8601 week numbering (week 1 = first Thursday, + Monday start, 52/53 week years) — either in ScGetWeekOfYear() + with mode=21 or as a separate code path. + 3. Add English help entry (currently only German KALENDERWOCHE). + 4. Verify Excel BIFF import mapping (currently not in BIFF tables). + 5. Add test cases. + +Rationale: DATEDIF was entirely missing from the date/time compatibility + group. Now implemented following ODF 1.4 §6.10.3 with documented + Excel-compatibility edge cases. ISOWEEKNUM remains pending. Group 3 — Financial Odd-Period Functions (ODDFPRICE, ODDFYIELD) @@ -229,11 +243,10 @@ P1 (Critical — ODF-required behavior missing): - ERROR.TYPE: Error-code mapping to ODF return values unverified - NUMBERVALUE: RESOLVED — evaluator implemented in H.1.1 - FINDB/REPLACEB/SEARCHB: RESOLVED — evaluators implemented in H.2 - - NUMBERVALUE: RESOLVED — evaluator implemented in H.1.1 + - DATEDIF: RESOLVED — evaluator implemented in H.3 P1 (High — semantic gaps): - ISOWEEKNUM: ISO 8601 week numbering not implemented - - DATEDIF: Entirely missing (import → #NAME!) P2 (Excel compatibility): - ODDFPRICE/ODDFYIELD: Analysis AddIn only, not native diff --git a/audit/openformula-function-gaps/function-source-audit.md b/audit/openformula-function-gaps/function-source-audit.md index fb102bd6d6..a1e357f318 100644 --- a/audit/openformula-function-gaps/function-source-audit.md +++ b/audit/openformula-function-gaps/function-source-audit.md @@ -144,49 +144,53 @@ Confidence: HIGH — source evidence complete; runtime verification 4. DATEDIF ---------- -ODF 1.4: Not defined by ODF 1.4 Part 4. Excel-specific function. -ODF group: Not applicable (Excel Large Group equivalent). +ODF 1.4: Defined by ODF 1.4 Part 4, Section 6.10.3. +ODF group: Large Group (Excel compatibility function). A. Function registration - - Native opcode: NONE. No SC_OPCODE_DATEDIF in compiler.hrc. - - Parser name: None registered as a native function. - - Function-name-to-opcode mapping: Mapped to ocNoName in - main/sc/source/filter/excel/xlformula.cxx line 342: - { ocNoName, 351, 3, 3, V, { VR }, EXC_FUNCFLAG_IMPORTONLY, 0 }, // DATEDIF - This is in saFuncTable_5 (BIFF5/BIFF7), marked IMPORTONLY. - - Evaluator implementation: None. No ScDateDif() in interpr*.cxx. - - Function parameter metadata: None (native). - - Help entry: None. No HID_FUNC_DATEDIF. - - ODF formula namespace mapping: None. - - Excel import mapping: BIFF5/BIFF7 import only, function ID 351, - import-only flag. The comment at xlformula.cxx:324 states: - "Unsupported functions: DATEDIF, DATESTRING, NUMBERSTRING." - - Excel export mapping: None (import-only). - -B. Actual implementation: Absent. Import maps to ocNoName which - produces #NAME! error at evaluation time. + - Native opcode: SC_OPCODE_DATEDIF = 415 (compiler.hrc line 415). + - Parser name: "DATEDIF" registered in core_resource.src (ODF and + Excel sections). + - Function-name-to-opcode mapping: ocDateDif → DATEDIF (opcode.hxx + line 309). + - Evaluator implementation: ScInterpreter::ScDateDif() at + main/sc/source/core/tool/interpr2.cxx:443. Dispatch via + `case ocDateDif : ScDateDif();` at interpr4.cxx:3775. + - Function parameter metadata: Via native function resource. + - Help entry: HID_FUNC_DATEDIF (helpids.h line 768+, hidother.src). + - ODF formula namespace mapping: DATEDIF in ODF section of + core_resource.src. + - Excel import mapping: BIFF5/BIFF7 function ID 351 still mapped to + ocNoName in xlformula.cxx line 342 (import-only, produces #NAME!). + Native opcode not yet wired to BIFF import table. + - Excel export mapping: None. + +B. Actual implementation: ScInterpreter::ScDateDif() at interpr2.cxx:443. + Implements ODF 1.4 §6.10.3 with all six formats (y, m, d, md, ym, yd). + Uses pFormatter->GetNullDate() for document-aware serial conversion. + Calendar arithmetic (not day-count approximation) for month/year + computations. Floor-based handling of time fractions. Case-sensitive + format matching. Reverse-date handling: y/m/d negated, md/ym/yd + absolute (Excel compatibility policy). + Detailed algorithm in audit/openformula-function-gaps/datedif-semantic-design.md. C. Format compatibility - - XLS import: Recognized in BIFF5/BIFF7 as function ID 351 but - mapped to ocNoName → #NAME! error. Not evaluated. + - XLS import: BIFF5/BIFF7 function ID 351 maps to ocNoName → #NAME!. + Native opcode not yet wired to BIFF import table. - XLS import (BIFF8): Not in saFuncTable_8. Not recognized. - XLSX import: Not in saFuncTable_Oox. Not recognized. - - XLS export: None (import-only). + - XLS export: None. - XLSX export: None. -D. Documentation: Absent. - -Related infrastructure: AOO has date difference AddIn functions - registered in odffmap.cxx lines 32–37: - WEEKS, MONTHS, YEARS (ORG.OPENOFFICE.* namespace) via - DateFunctions AddIn (getDiffWeeks, getDiffMonths, getDiffYears). - These are not DATEDIF but provide related date-difference - functionality. DAYS (ocGetDiffDate) and DAYS360 - (ocGetDiffDate360) are native. +D. Documentation: Help entry exists (HID_FUNC_DATEDIF). + Function Wizard entry exists in main/sc/source/ui/src/scfuncs.src + (SC_OPCODE_DATEDIF resource block with parameter names and descriptions). + Help topic not yet added under main/helpcontent2/. -Failure type: Function entirely missing; import produces #NAME!. -Priority: P1 (ODF Large Group equivalent; Excel compatibility gap). -Confidence: HIGH. +Failure type: None — implementation complete at source level. +Priority: Resolved (was P1). +Confidence: HIGH — source evidence complete; runtime verification + deferred (no build available). 5. ISOWEEKNUM diff --git a/main/formula/inc/formula/compiler.hrc b/main/formula/inc/formula/compiler.hrc index c3499fa5a8..53d19ef4fd 100644 --- a/main/formula/inc/formula/compiler.hrc +++ b/main/formula/inc/formula/compiler.hrc @@ -408,10 +408,11 @@ #define SC_OPCODE_FINDB 412 #define SC_OPCODE_SEARCHB 413 #define SC_OPCODE_REPLACEB 414 +#define SC_OPCODE_DATEDIF 415 -#define SC_OPCODE_STOP_2_PAR 415 +#define SC_OPCODE_STOP_2_PAR 416 -#define SC_OPCODE_LAST_OPCODE_ID 414 /* last OpCode */ +#define SC_OPCODE_LAST_OPCODE_ID 415 /* last OpCode */ /*** Interna ***/ #define SC_OPCODE_INTERNAL_BEGIN 9999 diff --git a/main/formula/inc/formula/opcode.hxx b/main/formula/inc/formula/opcode.hxx index 5f87bae16e..cbf6df92e6 100644 --- a/main/formula/inc/formula/opcode.hxx +++ b/main/formula/inc/formula/opcode.hxx @@ -306,6 +306,7 @@ enum OpCodeEnum ocFindB = SC_OPCODE_FINDB, ocSearchB = SC_OPCODE_SEARCHB, ocReplaceB = SC_OPCODE_REPLACEB, + ocDateDif = SC_OPCODE_DATEDIF, // Matrix functions ocMatValue = SC_OPCODE_MAT_VALUE, ocMatDet = SC_OPCODE_MAT_DET, diff --git a/main/formula/source/core/resource/core_resource.src b/main/formula/source/core/resource/core_resource.src index 55d29f1fb8..1fa8dd0359 100644 --- a/main/formula/source/core/resource/core_resource.src +++ b/main/formula/source/core/resource/core_resource.src @@ -268,6 +268,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH_ODFF String SC_OPCODE_FINDB { Text = "FINDB" ; }; String SC_OPCODE_SEARCHB { Text = "SEARCHB" ; }; String SC_OPCODE_REPLACEB { Text = "REPLACEB" ; }; + String SC_OPCODE_DATEDIF { Text = "DATEDIF" ; }; String SC_OPCODE_TEXT { Text = "TEXT" ; }; String SC_OPCODE_SUBSTITUTE { Text = "SUBSTITUTE" ; }; String SC_OPCODE_REPT { Text = "REPT" ; }; @@ -616,6 +617,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH String SC_OPCODE_FINDB { Text = "FINDB" ; }; String SC_OPCODE_SEARCHB { Text = "SEARCHB" ; }; String SC_OPCODE_REPLACEB { Text = "REPLACEB" ; }; + String SC_OPCODE_DATEDIF { Text = "DATEDIF" ; }; String SC_OPCODE_TEXT { Text = "TEXT" ; }; String SC_OPCODE_SUBSTITUTE { Text = "SUBSTITUTE" ; }; String SC_OPCODE_REPT { Text = "REPT" ; }; @@ -1611,6 +1613,10 @@ Resource RID_STRLIST_FUNCTION_NAMES String SC_OPCODE_REPLACEB { Text [ en-US ] = "REPLACEB" ; + }; + String SC_OPCODE_DATEDIF + { + Text [ en-US ] = "DATEDIF" ; }; String SC_OPCODE_TEXT { diff --git a/main/sc/inc/helpids.h b/main/sc/inc/helpids.h index 24d329dfff..cc10072778 100644 --- a/main/sc/inc/helpids.h +++ b/main/sc/inc/helpids.h @@ -765,6 +765,7 @@ #define HID_FUNC_FINDB "SC_HID_FUNC_FINDB" #define HID_FUNC_SEARCHB "SC_HID_FUNC_SEARCHB" #define HID_FUNC_REPLACEB "SC_HID_FUNC_REPLACEB" +#define HID_FUNC_DATEDIF "SC_HID_FUNC_DATEDIF" #define HID_FUNC_WIEDERHOLEN "SC_HID_FUNC_WIEDERHOLEN" #define HID_FUNC_WECHSELN "SC_HID_FUNC_WECHSELN" #define HID_FUNC_BASIS "SC_HID_FUNC_BASIS" diff --git a/main/sc/source/core/tool/interpr2.cxx b/main/sc/source/core/tool/interpr2.cxx index 710544ffc6..58cb927c7d 100644 --- a/main/sc/source/core/tool/interpr2.cxx +++ b/main/sc/source/core/tool/interpr2.cxx @@ -440,6 +440,156 @@ void ScInterpreter::ScGetDiffDate360() } } +void ScInterpreter::ScDateDif() +{ + RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDateDif" ); + if ( MustHaveParamCount( GetByte(), 3 ) ) + { + // Arguments are popped in reverse order: Format, EndDate, StartDate + String aFormat = GetString(); + double nEndDate = GetDouble(); + double nStartDate = GetDouble(); + + if ( nGlobalError ) + { + PushError( nGlobalError ); + return; + } + + // Floor to calendar dates (discard time fractions) + nStartDate = ::rtl::math::approxFloor( nStartDate ); + nEndDate = ::rtl::math::approxFloor( nEndDate ); + + const Date* pNullDate = pFormatter->GetNullDate(); + Date aStart = *pNullDate; + aStart += (long) nStartDate; + Date aEnd = *pNullDate; + aEnd += (long) nEndDate; + + if ( !aStart.IsValid() || !aEnd.IsValid() ) + { + PushIllegalArgument(); + return; + } + + double fResult = 0.0; + bool bValidFormat = true; + + if ( aFormat == "d" ) + { + // Calendar day difference + fResult = (double)(aEnd - aStart); + } + 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(); + + 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; + } + else + { + nDays = (long)nDay2 - (long)nDay1; + } + fResult = (double)nDays; + } + 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; + } + 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; + } + else + { + bValidFormat = false; + } + + if ( !bValidFormat ) + { + PushIllegalArgument(); + return; + } + + // Handle reverse dates (start > end) + if ( aStart > aEnd ) + { + 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; + } + } + + PushDouble( fResult ); + } +} + void ScInterpreter::ScGetTimeValue() { RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetTimeValue" ); diff --git a/main/sc/source/core/tool/interpr4.cxx b/main/sc/source/core/tool/interpr4.cxx index b99746ebf8..480e025e02 100644 --- a/main/sc/source/core/tool/interpr4.cxx +++ b/main/sc/source/core/tool/interpr4.cxx @@ -3772,6 +3772,7 @@ StackVar ScInterpreter::Interpret() case ocFindB : ScFindB(); break; case ocSearchB : ScSearchB(); break; case ocReplaceB : ScReplaceB(); break; + case ocDateDif : ScDateDif(); break; case ocUnicode : ScUnicode(); break; case ocUnichar : ScUnichar(); break; case ocTTT : ScTTT(); break; diff --git a/main/sc/source/ui/src/scfuncs.src b/main/sc/source/ui/src/scfuncs.src index 4ec67f7156..0a69dcf4da 100644 --- a/main/sc/source/ui/src/scfuncs.src +++ b/main/sc/source/ui/src/scfuncs.src @@ -9528,6 +9528,47 @@ Resource RID_SC_FUNCTION_DESCRIPTIONS2 }; }; + // -=*# Resource for function DATEDIF #*=- + Resource SC_OPCODE_DATEDIF + { + String 1 // Description + { + Text [ en-US ] = "Calculates the difference between two dates in years, months, or days." ; + }; + ExtraData = + { + 0; + ID_FUNCTION_GRP_DATETIME; + U2S( HID_FUNC_DATEDIF ); + 3; 0; 0; 0; + 0; + }; + String 2 // Name of Parameter 1 + { + Text [ en-US ] = "Start_date" ; + }; + String 3 // Description of Parameter 1 + { + Text [ en-US ] = "The start date of the interval." ; + }; + String 4 // Name of Parameter 2 + { + Text [ en-US ] = "End_date" ; + }; + String 5 // Description of Parameter 2 + { + Text [ en-US ] = "The end date of the interval." ; + }; + String 6 // Name of Parameter 3 + { + Text [ en-US ] = "Format" ; + }; + String 7 // Description of Parameter 3 + { + Text [ en-US ] = "The unit for the result: \"y\" years, \"m\" months, \"d\" days, \"md\" days ignoring months and years, \"ym\" months ignoring years, \"yd\" days ignoring years." ; + }; + }; + // -=*# Resource for function FINDB #*=- Resource SC_OPCODE_FINDB { diff --git a/main/sc/util/hidother.src b/main/sc/util/hidother.src index b517894f25..65dbdeb60c 100644 --- a/main/sc/util/hidother.src +++ b/main/sc/util/hidother.src @@ -356,6 +356,7 @@ hidspecial HID_FUNC_MIDB { HelpID = HID_FUNC_MIDB; }; hidspecial HID_FUNC_FINDB { HelpID = HID_FUNC_FINDB; }; hidspecial HID_FUNC_SEARCHB { HelpID = HID_FUNC_SEARCHB; }; hidspecial HID_FUNC_REPLACEB { HelpID = HID_FUNC_REPLACEB; }; +hidspecial HID_FUNC_DATEDIF { HelpID = HID_FUNC_DATEDIF; }; hidspecial HID_FUNC_WIEDERHOLEN { HelpID = HID_FUNC_WIEDERHOLEN; }; hidspecial HID_FUNC_WECHSELN { HelpID = HID_FUNC_WECHSELN; }; hidspecial HID_FUNC_BASIS { HelpID = HID_FUNC_BASIS; }; diff --git a/test/odf-validate/calcval-cases14.tsv b/test/odf-validate/calcval-cases14.tsv index 0c88cf270b..bf49559bb2 100644 --- a/test/odf-validate/calcval-cases14.tsv +++ b/test/odf-validate/calcval-cases14.tsv @@ -299,3 +299,18 @@ SG.CONV-003-ref =N(D1) cell 10 6.13.26 N dereferences a Reference to a scalar (D SG.CONV-004-logical =N(TRUE()) cell 1 6.13.26 N of TRUE is 1 SG.CONV-004-logical2 =IF(0;TRUE();FALSE()) cell 0 6.3.12 the Number 0 converts to FALSE SG.CONV-005-error =ISERR(1/0) cell 1 6.13.15 ISERR of a division by zero (TRUE) +SG.DATEDIF-001-d-days =DATEDIF(DATE(2024;1;1);DATE(2024;12;31);"d") cell 365 6.10.3 days between Jan 1 and Dec 31 in leap year +SG.DATEDIF-002-m-months =DATEDIF(DATE(2024;1;1);DATE(2025;1;1);"m") cell 12 6.10.3 complete months between same day consecutive years +SG.DATEDIF-003-y-years =DATEDIF(DATE(2024;1;1);DATE(2025;1;1);"y") cell 1 6.10.3 complete years between same day consecutive years +SG.DATEDIF-004-same-date =DATEDIF(DATE(2024;1;1);DATE(2024;1;1);"d") cell 0 6.10.3 zero interval returns 0 +SG.DATEDIF-005-m-jan-feb =DATEDIF(DATE(2024;1;1);DATE(2024;2;1);"m") cell 1 6.10.3 one complete month Jan to Feb +SG.DATEDIF-006-d-jan-feb29 =DATEDIF(DATE(2024;1;1);DATE(2024;2;29);"d") cell 59 6.10.3 days from Jan 1 to Feb 29 in leap year +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-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
