forked from fahrPool/fahrPool
82 lines
3.3 KiB
TypeScript
82 lines
3.3 KiB
TypeScript
export const ZEITPLANUNG_TYPEN = ["ab_sofort", "am", "ab", "diese_woche", "sobald_moeglich"] as const;
|
|
export type ZeitplanungTyp = (typeof ZEITPLANUNG_TYPEN)[number];
|
|
|
|
export function formatZeitplanung(typ: ZeitplanungTyp, zeitpunkt: string | null): string {
|
|
if (typ === "ab_sofort") return "Ab sofort";
|
|
if (typ === "diese_woche") return "Diese Woche";
|
|
if (typ === "sobald_moeglich") return "Sobald als möglich";
|
|
|
|
// DB-CHECK-Constraint garantiert zeitpunkt bei am/ab, TS kennt das nicht.
|
|
const datum = new Date(zeitpunkt!);
|
|
const formatiertesDatum = datum.toLocaleDateString("de-DE", {
|
|
day: "2-digit",
|
|
month: "2-digit",
|
|
year: "numeric",
|
|
timeZone: "Europe/Berlin",
|
|
});
|
|
const formatierteUhrzeit = datum.toLocaleTimeString("de-DE", {
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
timeZone: "Europe/Berlin",
|
|
});
|
|
const praefix = typ === "am" ? "Am" : "Ab";
|
|
return `${praefix} ${formatiertesDatum}, ${formatierteUhrzeit} Uhr`;
|
|
}
|
|
|
|
export function berechneVorbefuelltenZeitpunkt(jetzt: Date = new Date()): string {
|
|
const morgen = new Date(jetzt);
|
|
morgen.setDate(morgen.getDate() + 1);
|
|
morgen.setHours(15, 0, 0, 0);
|
|
const jahr = morgen.getFullYear();
|
|
const monat = String(morgen.getMonth() + 1).padStart(2, "0");
|
|
const tag = String(morgen.getDate()).padStart(2, "0");
|
|
return `${jahr}-${monat}-${tag}T15:00`;
|
|
}
|
|
|
|
// Ermittelt den UTC-Offset (in Minuten) von Europe/Berlin fuer einen gegebenen
|
|
// Zeitpunkt, ohne von der Zeitzone des ausfuehrenden Prozesses abzuhaengen.
|
|
// Nutzt Intl.DateTimeFormat mit explizit gesetzter timeZone -- das liefert
|
|
// deterministisch die Berliner Wanduhrzeit fuer diesen Moment, unabhaengig
|
|
// davon, in welcher Zeitzone der Node-Prozess selbst laeuft.
|
|
function ermittleBerlinerOffsetMinuten(momentaneUtcSchaetzung: Date): number {
|
|
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
timeZone: "Europe/Berlin",
|
|
hourCycle: "h23",
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
second: "2-digit",
|
|
});
|
|
const teile = formatter.formatToParts(momentaneUtcSchaetzung).reduce<Record<string, string>>((acc, teil) => {
|
|
acc[teil.type] = teil.value;
|
|
return acc;
|
|
}, {});
|
|
const alsUtcInterpretiertesBerlinDatum = Date.UTC(
|
|
Number(teile.year),
|
|
Number(teile.month) - 1,
|
|
Number(teile.day),
|
|
Number(teile.hour),
|
|
Number(teile.minute),
|
|
Number(teile.second)
|
|
);
|
|
return (alsUtcInterpretiertesBerlinDatum - momentaneUtcSchaetzung.getTime()) / 60000;
|
|
}
|
|
|
|
// Interpretiert einen "datetime-local"-Wert (z.B. "2026-08-21T15:00") als
|
|
// Wanduhrzeit in Europe/Berlin, unabhaengig von der Zeitzone des Servers --
|
|
// new Date(zeitpunktRaw) allein wuerde die Server-Prozess-Zeitzone annehmen,
|
|
// was in Produktion (Docker-Container ohne TZ, laeuft in UTC) zu einer
|
|
// stillen Zeitverschiebung fuehren wuerde (siehe Code-Review-Finding zur
|
|
// Zeitplanung: Eingabe 15:00 wurde unter TZ=UTC faelschlich als 17:00
|
|
// zurueckformatiert).
|
|
export function parseZeitplanungZeitpunkt(datetimeLocal: string): Date {
|
|
const probeweiseUtc = new Date(`${datetimeLocal}:00.000Z`);
|
|
if (Number.isNaN(probeweiseUtc.getTime())) {
|
|
return probeweiseUtc; // Invalid Date durchreichen, Aufrufer prueft bereits auf NaN.
|
|
}
|
|
const offsetMinuten = ermittleBerlinerOffsetMinuten(probeweiseUtc);
|
|
return new Date(probeweiseUtc.getTime() - offsetMinuten * 60000);
|
|
}
|