Datum- & Zeit-Formatierung: Serial-Zahlen, Auto-Erkennung, DATE/TIME/TODAY/NOW

Wie in jeder Tabellenkalkulation ist ein Datum intern einfach eine Zahl
("Serial": Tage seit 1899-12-30, Uhrzeit als Bruchteil eines Tages) --
Datums-Arithmetik (=fällig - rechnungsdatum, =rechnungsdatum + 30)
funktioniert dadurch automatisch, ganz ohne Änderung an Operatoren.

- Neues Modul engine/dates.ts: dateToSerial/timeSerial (Bausteine für
  DATE()/TIME(), Überlauf wie Excel erlaubt), todaySerial/nowSerial
  (lokales Kalenderdatum), serialToUtcDate (Anzeige), parseDateTimeLiteral
  (Eingabe-Erkennung -- bewusst nur ISO/Punkt-Daten + Uhrzeit, KEINE
  Schrägstrich-Daten, da Tag-/Monat-zuerst über die unterstützten Locales
  nicht eindeutig auflösbar ist; ein zu einer ungültigen/rollierten
  Kalenderdatum führender Kandidat wird strikt verworfen statt lautlos
  zu überlaufen).
- workbook.ts: literalValue() erkennt Datum/Zeit vor dem Zahl-Test und
  liefert direkt den Serial.
- functions.ts: neue eingebaute Funktionen DATE(jahr;monat;tag),
  TIME(std;min;sek), TODAY(), NOW() -- TODAY/NOW sind bewusst nicht
  volatil (dokumentierte Grenze: diese Engine rechnet rein
  vorgänger-getrieben neu, ohne Timer).
- store/types.ts + format.ts: CellNumFmt um 'date'/'time'/'datetime'
  erweitert; formatValue rendert über Intl.DateTimeFormat (UTC-Zeitzone,
  pro Locale), 'plain' bekommt eine manuelle, locale-neutrale
  Formatierung statt der Browser-Locale.
- store/document.ts: setCellInput erkennt eine getippte Datums-/
  Zeit-Eingabe automatisch und setzt das passende Zellformat -- wie
  Excel/Numbers --, überschreibt aber nie ein bereits explizit gesetztes
  Format.
- Zahlenformat-Dropdown (SettingsPanel) und JSON-Validierung
  (isCellNumFmt) brauchten keine Änderung -- beide sind bereits generisch
  über NUM_FORMATS.

Tests: 32 neue (dates.spec.ts neu; workbook.spec.ts: literalValue +
DATE/TIME/TODAY/NOW-Formeln inkl. Datums-Arithmetik; format.spec.ts neu:
formatValue über mehrere Locales; document.spec.ts: Auto-Format beim
Eintippen, überschreibt nie ein gesetztes Format, Namenskollision mit
den neuen Funktionen). 265 -> 297 Tests, alle grün. vue-tsc/eslint/build
sauber. Live per Playwright verifiziert (Auto-Erkennung, Formel-Ergebnisse
ohne Auto-Format, Datums-Arithmetik, Schrägstrich-Daten bleiben Text).

READMEs aktualisiert (Funktionen, Zahlenformat, neuer "Datum & Zeit"-
Abschnitt in engine/README.md, Bekannte Grenzen, Testzahl; nebenbei eine
veraltete Aussage zu Vergleichsoperatoren in "Bekannte Grenzen" korrigiert
-- die gibt es seit der IF-Funktion bereits).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
wkh
2026-09-02 23:24:39 +02:00
co-authored by Claude Sonnet 5
parent ec85c371e8
commit e6cb18ab3c
13 changed files with 636 additions and 11 deletions
+42
View File
@@ -347,6 +347,39 @@ describe('cell formatting, number formats, row height', () => {
expect(s.cellDisplay(id, 6, 0)).toBe('0');
});
it('typing a recognizable date auto-applies the date format', () => {
const s = fresh();
const id = tableId(s, 'Umsatz');
s.setCellInput(id, 6, 0, '02.09.2026'); // G1, still 'auto' beforehand
expect(s.cellFormat(id, 6, 0)).toEqual({ numFmt: 'date' });
expect(s.cellDisplay(id, 6, 0)).toBe('02.09.2026');
});
it('typing a recognizable time auto-applies the time format', () => {
const s = fresh();
const id = tableId(s, 'Umsatz');
s.setCellInput(id, 6, 0, '14:30');
expect(s.cellFormat(id, 6, 0)).toEqual({ numFmt: 'time' });
expect(s.cellDisplay(id, 6, 0)).toBe('14:30');
});
it('date auto-recognition never overrides an explicit numFmt already on the cell', () => {
const s = fresh();
const id = tableId(s, 'Umsatz');
s.setRangeFormat(id, { col: 6, row: 0 }, { col: 6, row: 0 }, { numFmt: 'int' });
s.setCellInput(id, 6, 0, '02.09.2026');
expect(s.cellFormat(id, 6, 0)).toEqual({ numFmt: 'int' }); // untouched
});
it('a plain number or unrecognized text does not touch numFmt', () => {
const s = fresh();
const id = tableId(s, 'Umsatz');
s.setCellInput(id, 6, 0, '1234.5');
expect(s.cellFormat(id, 6, 0)).toEqual({});
s.setCellInput(id, 6, 1, '=1+1');
expect(s.cellFormat(id, 6, 1)).toEqual({});
});
it('setRangeFormat applies to a whole range and clears with falsy/auto', () => {
const s = fresh();
const id = tableId(s, 'Umsatz');
@@ -728,6 +761,15 @@ describe('custom functions (document-level library)', () => {
expect(s.doc.customFunctions).toHaveLength(0);
});
it('rejects a name colliding with a built-in date/time function', () => {
const s = fresh();
for (const name of ['DATE', 'time', 'Today', 'NOW']) {
const result = s.addCustomFunction(name, ['x'], 'x') as { error: string };
expect(result.error).toBeTruthy();
}
expect(s.doc.customFunctions).toHaveLength(0);
});
it('rejects a name colliding with another custom function (case-insensitive)', () => {
const s = fresh();
s.addCustomFunction('Marge', ['x'], 'x');