// Eingaben aus sind deutsche Ortszeit; der Server läuft in UTC.
const ZONE = "Europe/Berlin";
function offsetMs(d: Date) {
const parts = Object.fromEntries(
new Intl.DateTimeFormat("en-US", { timeZone: ZONE, hourCycle: "h23", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit" })
.formatToParts(d)
.map((p) => [p.type, p.value]),
);
return Date.UTC(+parts.year, +parts.month - 1, +parts.day, +parts.hour, +parts.minute, +parts.second) - d.getTime();
}
/** "2026-11-27T00:00" (deutsche Zeit) → Date. Leer oder ungültig → null. */
export function parseBerlinLocal(value: string): Date | null {
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(value)) return null;
const guess = Date.parse(`${value}:00Z`);
if (Number.isNaN(guess)) return null;
return new Date(guess - offsetMs(new Date(guess)));
}
/** Date → Wert für in deutscher Zeit. */
export function toBerlinLocal(d: Date | null | undefined) {
if (!d) return "";
return new Date(d.getTime() + offsetMs(d)).toISOString().slice(0, 16);
}