forked from fahrPool/fahrPool
feat: Berliner Datumshelfer fuer Kalender und Reports
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
be1fca18ac
commit
7dadb62c6f
|
|
@ -0,0 +1,49 @@
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { berlinTag, istGueltigerTag, kalenderwoche, plusTage, tageZwischen, tagesBeginnUtc, wochenStart } from "./berlin";
|
||||||
|
|
||||||
|
describe("berlinTag", () => {
|
||||||
|
it("liefert den Berliner Kalendertag", () => {
|
||||||
|
expect(berlinTag("2026-10-31T23:30:00Z")).toBe("2026-11-01");
|
||||||
|
expect(berlinTag(new Date("2026-07-01T21:59:00Z"))).toBe("2026-07-01");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("plusTage / wochenStart / tageZwischen", () => {
|
||||||
|
it("rechnet ueber Monats- und Jahresgrenzen", () => {
|
||||||
|
expect(plusTage("2026-12-31", 1)).toBe("2027-01-01");
|
||||||
|
expect(plusTage("2026-03-01", -1)).toBe("2026-02-28");
|
||||||
|
});
|
||||||
|
it("findet den Montag der Woche", () => {
|
||||||
|
expect(wochenStart("2026-11-01")).toBe("2026-10-26");
|
||||||
|
expect(wochenStart("2026-10-26")).toBe("2026-10-26");
|
||||||
|
});
|
||||||
|
it("listet alle Tage inklusive", () => {
|
||||||
|
expect(tageZwischen("2026-10-26", "2026-11-01")).toHaveLength(7);
|
||||||
|
expect(tageZwischen("2026-10-26", "2026-11-01")[6]).toBe("2026-11-01");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("tagesBeginnUtc", () => {
|
||||||
|
it("liefert Berliner Mitternacht beidseits der Zeitumstellung", () => {
|
||||||
|
expect(tagesBeginnUtc("2026-10-26").toISOString()).toBe("2026-10-25T23:00:00.000Z");
|
||||||
|
expect(tagesBeginnUtc("2026-10-25").toISOString()).toBe("2026-10-24T22:00:00.000Z");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("kalenderwoche", () => {
|
||||||
|
it("rechnet nach ISO 8601", () => {
|
||||||
|
expect(kalenderwoche("2026-10-31")).toBe(44);
|
||||||
|
expect(kalenderwoche("2027-01-01")).toBe(53);
|
||||||
|
expect(kalenderwoche("2027-01-04")).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("istGueltigerTag", () => {
|
||||||
|
it("prueft Format und Kalenderdatum", () => {
|
||||||
|
expect(istGueltigerTag("2026-11-02")).toBe(true);
|
||||||
|
expect(istGueltigerTag("2026-02-30")).toBe(false);
|
||||||
|
expect(istGueltigerTag("2026-13-40")).toBe(false);
|
||||||
|
expect(istGueltigerTag("02.11.2026")).toBe(false);
|
||||||
|
expect(istGueltigerTag(undefined)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { parseZeitplanungZeitpunkt } from "@/lib/auftraege/zeitplanung";
|
||||||
|
|
||||||
|
// Kalendertag im Format YYYY-MM-DD (Berliner Zeit).
|
||||||
|
export type Tag = string;
|
||||||
|
|
||||||
|
const TAG_FORMAT = new Intl.DateTimeFormat("sv-SE", { timeZone: "Europe/Berlin" });
|
||||||
|
|
||||||
|
export function berlinTag(zeitpunkt: Date | string): Tag {
|
||||||
|
return TAG_FORMAT.format(typeof zeitpunkt === "string" ? new Date(zeitpunkt) : zeitpunkt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reine Datumsarithmetik ueber UTC-Mittag, damit Zeitumstellungen keine Rolle spielen.
|
||||||
|
function alsUtcMittag(tag: Tag): Date {
|
||||||
|
return new Date(`${tag}T12:00:00Z`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function istGueltigerTag(wert: string | undefined): wert is Tag {
|
||||||
|
if (!wert || !/^\d{4}-\d{2}-\d{2}$/.test(wert)) return false;
|
||||||
|
const d = alsUtcMittag(wert);
|
||||||
|
return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === wert;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function plusTage(tag: Tag, n: number): Tag {
|
||||||
|
const d = alsUtcMittag(tag);
|
||||||
|
d.setUTCDate(d.getUTCDate() + n);
|
||||||
|
return d.toISOString().slice(0, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function wochenStart(tag: Tag): Tag {
|
||||||
|
const wochentag = alsUtcMittag(tag).getUTCDay() || 7;
|
||||||
|
return plusTage(tag, 1 - wochentag);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tagesBeginnUtc(tag: Tag): Date {
|
||||||
|
return parseZeitplanungZeitpunkt(`${tag}T00:00`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function kalenderwoche(tag: Tag): number {
|
||||||
|
const donnerstag = alsUtcMittag(plusTage(wochenStart(tag), 3));
|
||||||
|
const jahresBeginn = Date.UTC(donnerstag.getUTCFullYear(), 0, 1, 12);
|
||||||
|
return Math.floor((donnerstag.getTime() - jahresBeginn) / 86_400_000 / 7) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tageZwischen(von: Tag, bis: Tag): Tag[] {
|
||||||
|
const tage: Tag[] = [];
|
||||||
|
for (let tag = von; tag <= bis; tag = plusTage(tag, 1)) tage.push(tag);
|
||||||
|
return tage;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue