Skip to content

xlstream-eval: adjust DATE() year 0-1899, guard negative and overflow#187

Merged
cilladev merged 2 commits into
mainfrom
fix/date-year-adjust
Jun 5, 2026
Merged

xlstream-eval: adjust DATE() year 0-1899, guard negative and overflow#187
cilladev merged 2 commits into
mainfrom
fix/date-year-adjust

Conversation

@cilladev

@cilladev cilladev commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Fixes: #144

Problem

DATE(year, month, day) passed the year argument directly to ExcelDate::from_ymd without Excel's year-adjustment rule. Years 0-1899 produced massive negative serial numbers instead of the correct 1900-adjusted dates. Negative years and years producing serials past 9999-12-31 were also not guarded.

Fix

  • Add 1900 to the year when 0 <= year < 1900 (Excel's documented behavior).
  • Return #NUM! for negative years (negative serials).
  • Return #NUM! when the resulting serial exceeds EXCEL_MAX_DATE_SERIAL (2,958,465 = 9999-12-31).

Issue description correction

The issue lists DATE(24,6,15) → 45458 (2024-06-15). Excel actually returns 8933 (1924-06-15) because the rule is strictly year + 1900, not century-guessing. The fix matches Excel's actual behavior, not the issue's third expected value.

Changes

  • crates/xlstream-core/src/lib.rs: add EXCEL_MAX_DATE_SERIAL constant (2,958,465).
  • crates/xlstream-eval/src/builtins/date.rs: year 0-1899 adjustment, negative year #NUM! guard, overflow #NUM! guard.
  • 8 unit tests: boundaries (y=0, y=99, y=1899, y=1900, y=2026), negative, overflow, max valid (9999-12-31).
  • Conformance fixture with 12 Excel-populated cells covering adjustment, boundaries, #NUM! errors, and fractional years.

Test plan

  • Regression unit tests added and pass
  • Conformance fixture created (Excel-populated) and passes (12/12, 0 mismatches)
  • cargo test -p xlstream-eval passes (no regressions)
  • make check passes

Closes #144

@cilladev cilladev force-pushed the fix/date-year-adjust branch from 9a1c94a to 7c5ee9c Compare June 5, 2026 14:22
@cilladev cilladev merged commit a8bd5a7 into main Jun 5, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DATE() missing year adjustment for years 0-1899

1 participant