Skip to content

fix: number dates before 1900-03-01 as Excel does, its 1900-02-29 included - #683

Merged
isamu merged 6 commits into
mainfrom
fix/excel-1900-serials
Dec 6, 2026
Merged

isamu merged 6 commits into
mainfrom
fix/excel-1900-serials

Conversation

@isamu

@isamu isamu commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Dates before 1900-03-01 are now numbered as Excel numbers them, its fictitious 1900-02-29 included. This follows the decision on #661 and reverses c2ae160. Serial 1 is 1900-01-01, 59 is 1900-02-28, 60 is 1900-02-29 and 61 is 1900-03-01. From 61 on nothing changes.

The conversions now live in one pure module, src/engine/serialCalendar.ts. It uses integer days-from-civil arithmetic and no Date, and every caller goes through it:

  • DATE / DATEVALUE / typed date text / criteria;
  • YEAR…SECOND, TEXT and the card's number formats;
  • WEEKDAY, WEEKNUM, ISOWEEKNUM, WORKDAY and NETWORKDAYS;
  • EDATE / EOMONTH, DAYS360, DATEDIF, YEARFRAC and the bond day counts;
  • QUERY date literals, NOW/TODAY and ETS (which keeps its real-calendar epoch).

The #650 conversion in the Excel download (excelDateSerial) is gone, because the engine and the file now number alike. The plugin does not read .xlsx files, so there is no import side to change.

Changed behaviour

  • DATE(1900,2,28)=59, DATE(1900,2,29)=DATE(1900,3,0)=60, DATE(1900,1,1)=1. DATEVALUE("1900-02-29") and typed 1900-02-29 are 60.
  • YEAR/MONTH/DAY(0) are 1900 / 1 / 0, and TEXT(0,"yyyy/m/d") is 1900/1/0.
  • Serial 60 shows as 1900-02-29.
  • Weekdays are counted off the serial (serial 1 is a Sunday, 60 a Wednesday), which is what Excel records below 61.
  • EDATE and EOMONTH use Gregorian month lengths, so EOMONTH(DATE(1900,2,1),0) is 59. A result before 1900 is #NUM! (EOMONTH(0,-1)).
  • DAYS360 and DATEDIF read February 1900 as 29 days.
  • DAYS360 and the bond functions read serial 0 as 1899-12-31: DAYS360(,5)=5 and DAYS360(0.9999999,31)=30. This covers the coordinator's extra case from fix: an empty argument in a scalar place of a built-in function reads as Excel reads it #679, and the two NOT_YET entries are removed.
  • YEARFRAC and the bond actual/actual year lengths stay Gregorian.

Tests changed because they pinned the old numbering

  • tests/engine/test_serialCalendar.ts: rewritten for the module. It is tested both ways over a wide range around the boundary, against Date.UTC's real calendar from 61 on, and with generated dates and times (seed printed).
  • tests/engine/test_dateUtils.ts: serialToDate was removed. dateToSerial is pinned around the boundary and round-tripped with serialMoment.
  • tests/engine/test_serialFromParts.ts: 1900-01-01 is 1; 59 / 60 / 61 are added.
  • tests/engine/test_serialParts.ts: 0.9999999 is 1900-01-01.
  • tests/engine/test_serialTimeZones.ts and tests/engine/test_weekNumbers.ts: these go through the module instead of serialToDate.
  • tests/vue/test_xlsxCells.ts: the excelDateSerial block was removed.
  • tests/engine/test_emptyScalarArgument.ts: the DAYS360 serial-0 NOT_YET entries were removed.

New tests

  • tests/engine/fixtures/excel-1900-serials.json holds Excel's recorded answers (formulon goldens, en_US and ja_JP) for serials 0–61. tests/engine/test_excel1900Serials.ts runs them.

Items to Confirm / Review

  • Inferred, not recorded: DATE(1900,1,60)=60. It follows from the recorded DATE(1900,3,0)=60 and from IronCalc's Excel1900 mode.
  • EDATE / EOMONTH on Gregorian lengths: this comes from the formulon and IronCalc implementations. Only EOMONTH(0,0) / (0,7) / (32,0) are recorded.
  • ETS keeps the real calendar: formulon steps ETS timelines without the phantom day.
  • Negative serials (reachable only through day arithmetic) map to the real day before.
  • QUERY date literals (date '1900-01-01') now use Excel numbering and accept date '1900-02-29'. A literal with a year below 100 used to fail because Date.UTC read it as 19xx; it is now read as written.
  • Coupon dates step months on the real calendar, as EDATE does: from a maturity on the 29th, the February 1900 coupon is the 28th (serial 59). This is not recorded. Inside 1900, Excel's recorded coupon day counts use serial differences (COUPDAYS(1,DATE(1900,7,1),2,1) is 182), and so does this engine.
  • DAYS360 (US) with a start on Excel's 1900-02-29: it moves to the 30th as any month-end start does, so DAYS360(60,60) is -1. This is the same rule that gives DAYS360(DATE(2011,2,28),DATE(2011,2,28)) = -2.
  • Left open, out of scope:
    • DATEDIF "MD" over a month end. Excel's rule differs for every year; three fixture cases are on NOT_YET.
    • Coupon periods that start before 1900. Excel answers these by a rule the engine does not follow. One golden, coupdays_before_f2_b1, used to pass only because the real calendar happened to give Excel's answer; it now misses by the phantom day. The rest of that cluster fails before and after this change.

Verification

  • yarn format && yarn lint && yarn build && yarn typecheck && yarn test && yarn test:engine && yarn test:fixtures && yarn quality, chained with &&.
  • Formulon goldens, before and after: run with the scratch golden-check.ts over the en_US and ja_JP targets.
    • More cases pass in both locales.
    • The only case that passed before and fails now is coupdays_before_f2_b1, explained above.
  • Excel's recorded bond answers in 1900 (COUPPCD, COUPNCD, COUPNUM, COUPDAYBS, COUPDAYS, COUPDAYSNC, DURATION, MDURATION, PRICE, YIELD):
    • About twice as many pass as before.
    • The one case that passed before and fails now is coupdays_before_f2_b1.
  • Old vs new engine (merge base 27abdc2 vs this head): three seeds, each over tests/engine/formulaGenerator.ts formulas plus a generator of date formulas, run once with serials spanning 0–130 and once with serials at or above 61.
    • The generic formulas did not change.
    • Every changed answer traces to a serial below 61: an argument, the result, a pre-March-1900 date text or DATE, or (for COUPDAYS basis 1) a previous coupon date below 61, checked with COUPPCD.
    • On the card's number formats, no serial at or above 61 changed.
  • Browser: the demo on yarn dev in Chrome (Playwright), with =DATE(1900,2,28) and =DATE(1900,3,1) formatted yyyy-mm-dd.
    • The card shows 1900-02-28 and 1900-03-01, =DATE(1900,2,29) shows 1900-02-29 Wednesday, and the difference is 2.
    • There is no uncaught error. One 404 appeared, for a resource not requested by the page load (favicon).
    • In the downloaded .xlsx, the cells hold <v>59</v>, <v>61</v> and <v>60</v>, and openpyxl reads A1 and B1 as 1900-02-28 and 1900-03-01.

User Prompt

  • AI時代のスプレッドシートとしてExcelやGoogle Sheetsを超えるよう、Codexと並列で改善を続けて
  • 1900年3月より前の日付の数え方を Excel に合わせる

Closes #661

work in GUIChatPluginSpreadsheet

🤖 Generated with Claude Code

@@ -0,0 +1,119 @@
/**
return typeof recorded.expect === "number" && typeof got === "number" ? Math.abs(got - recorded.expect) <= TOLERANCE : got === recorded.expect;
};

describe("serials 0 to 61 against Excel's recorded answers", () => {
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@isamu

isamu commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

Local Codex review ledger

Round 1 (head before d4d74b3): three findings.

  1. DAYS360(60,60) is -1. Kept. US DAYS360 moves a month-end start to the 30th and has no end-of-February rule for the end. The engine already answers DAYS360(DATE(2011,2,28),DATE(2011,2,28)) = -2, Excel's known behaviour, and serial 60 is February's last day on Excel's calendar.
  2. COUPDAYS(DATE(1900,3,1),DATE(1900,8,29),2,1) is 183. Kept. Coupon months step on the real calendar, as EDATE and EOMONTH do (recorded EOMONTH(32,0) = 59). No recorded answer contradicts it, and inside 1900 the recorded coupon day counts use serial differences. Listed under Items to Confirm.
  3. QUERY rejected date '1900-02-29'. Fixed in d4d74b3.

Round 2 (d4d74b3): LGTM.
Round 3 (d4d74b3, unchanged): LGTM.

@isamu
isamu merged commit c271efc into main Dec 6, 2026
17 checks passed
@isamu
isamu deleted the fix/excel-1900-serials branch December 6, 2026 11:55
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 serials before 1900-03-01 differ from Excel's by one

2 participants