109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
// Kalender-Abo (26.11.45): iCalendar nach RFC 5545, rein und ohne Datenbank,
|
|
// damit Escapen, Falten und Zeitzone im Test stehen.
|
|
|
|
export type CalendarEvent = {
|
|
uid: string;
|
|
/** YYYY-MM-DD */
|
|
day: string;
|
|
/** HH:MM, Ortszeit Europe/Berlin */
|
|
start: string;
|
|
minutes: number;
|
|
summary: string;
|
|
description: string;
|
|
url: string | null;
|
|
};
|
|
|
|
const TZ = "Europe/Berlin";
|
|
|
|
// Sommer- und Winterzeit wie seit 1996: letzter Sonntag im März und im Oktober.
|
|
const VTIMEZONE = [
|
|
"BEGIN:VTIMEZONE",
|
|
`TZID:${TZ}`,
|
|
"BEGIN:DAYLIGHT",
|
|
"TZOFFSETFROM:+0100",
|
|
"TZOFFSETTO:+0200",
|
|
"TZNAME:CEST",
|
|
"DTSTART:19700329T020000",
|
|
"RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU",
|
|
"END:DAYLIGHT",
|
|
"BEGIN:STANDARD",
|
|
"TZOFFSETFROM:+0200",
|
|
"TZOFFSETTO:+0100",
|
|
"TZNAME:CET",
|
|
"DTSTART:19701025T030000",
|
|
"RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU",
|
|
"END:STANDARD",
|
|
"END:VTIMEZONE",
|
|
];
|
|
|
|
/** Text nach RFC 5545 3.3.11: Backslash, Semikolon, Komma, Zeilenumbruch. */
|
|
export function escapeText(value: string): string {
|
|
return value.replace(/\\/g, "\\\\").replace(/;/g, "\;").replace(/,/g, "\\,").replace(/\r?\n/g, "\\n");
|
|
}
|
|
|
|
/** Zeilen über 75 Oktette falten (3.1), ohne ein UTF-8-Zeichen zu teilen. */
|
|
export function fold(line: string): string {
|
|
const bytes = (s: string) => Buffer.byteLength(s, "utf8");
|
|
if (bytes(line) <= 75) return line;
|
|
const parts: string[] = [];
|
|
let current = "";
|
|
let limit = 75;
|
|
for (const ch of line) {
|
|
if (bytes(current + ch) > limit) {
|
|
parts.push(current);
|
|
current = "";
|
|
limit = 74; // Fortsetzungszeilen beginnen mit einem Leerzeichen
|
|
}
|
|
current += ch;
|
|
}
|
|
parts.push(current);
|
|
return parts.join("\r\n ");
|
|
}
|
|
|
|
function local(day: string, time: string): string {
|
|
return `${day.replace(/-/g, "")}T${time.replace(":", "").slice(0, 4)}00`;
|
|
}
|
|
|
|
function addMinutes(day: string, time: string, minutes: number): string {
|
|
// Ortszeit als reine Wanduhr rechnen: UTC dient nur als Rechenbrett.
|
|
const [h, m] = time.split(":").map(Number);
|
|
const d = new Date(`${day}T00:00:00Z`);
|
|
d.setUTCMinutes(h! * 60 + m! + minutes);
|
|
const iso = d.toISOString();
|
|
return `${iso.slice(0, 10).replace(/-/g, "")}T${iso.slice(11, 13)}${iso.slice(14, 16)}00`;
|
|
}
|
|
|
|
function stamp(now: Date): string {
|
|
return now.toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");
|
|
}
|
|
|
|
export function buildCalendar(name: string, events: CalendarEvent[], now = new Date()): string {
|
|
const lines = [
|
|
"BEGIN:VCALENDAR",
|
|
"VERSION:2.0",
|
|
"PRODID:-//foodplanhero//Lunch//DE",
|
|
"CALSCALE:GREGORIAN",
|
|
"METHOD:PUBLISH",
|
|
`X-WR-CALNAME:${escapeText(name)}`,
|
|
`X-WR-TIMEZONE:${TZ}`,
|
|
"REFRESH-INTERVAL;VALUE=DURATION:PT1H",
|
|
"X-PUBLISHED-TTL:PT1H",
|
|
...VTIMEZONE,
|
|
];
|
|
for (const e of events) {
|
|
lines.push(
|
|
"BEGIN:VEVENT",
|
|
`UID:${e.uid}`,
|
|
`DTSTAMP:${stamp(now)}`,
|
|
`DTSTART;TZID=${TZ}:${local(e.day, e.start)}`,
|
|
`DTEND;TZID=${TZ}:${addMinutes(e.day, e.start, e.minutes)}`,
|
|
`SUMMARY:${escapeText(e.summary)}`,
|
|
);
|
|
if (e.description) lines.push(`DESCRIPTION:${escapeText(e.description)}`);
|
|
if (e.url) lines.push(`URL:${e.url}`);
|
|
lines.push("TRANSP:TRANSPARENT", "END:VEVENT");
|
|
}
|
|
lines.push("END:VCALENDAR");
|
|
return lines.map(fold).join("\r\n") + "\r\n";
|
|
}
|