Your favorite shift management application!
feat: Implement server timezone handling for iCalendar exports (#114)
panteLe3c723339724
diff --git a/app/api/calendars/[id]/export/ics/route.ts b/app/api/calendars/[id]/export/ics/route.tsindex 02d8c4a..a929b36 100644--- a/app/api/calendars/[id]/export/ics/route.ts+++ b/app/api/calendars/[id]/export/ics/route.ts@@ -5,6 +5,7 @@ import { eq } from "drizzle-orm";import ICAL from "ical.js";import { getSessionUser } from "@/lib/auth/sessions";import { canViewCalendar } from "@/lib/auth/permissions";+import { getServerTimezone } from "@/lib/date-utils";export async function GET(request: Request,@@ -41,6 +42,9 @@ export async function GET(orderBy: (shifts, { asc }) => [asc(shifts.date)],});+ // Get server timezone for proper time conversion+ const serverTimezone = getServerTimezone();+// Create iCalendarconst cal = new ICAL.Component(["vcalendar", [], []]);cal.updatePropertyWithValue(@@ -51,7 +55,7 @@ export async function GET(cal.updatePropertyWithValue("calscale", "GREGORIAN");cal.updatePropertyWithValue("method", "PUBLISH");cal.updatePropertyWithValue("x-wr-calname", calendar.name);- cal.updatePropertyWithValue("x-wr-timezone", "UTC");+ cal.updatePropertyWithValue("x-wr-timezone", serverTimezone);// Add shifts as eventsfor (const shift of calendarShifts) {@@ -97,6 +101,7 @@ export async function GET(const [startHour, startMinute] = shift.startTime.split(":").map(Number);const [endHour, endMinute] = shift.endTime.split(":").map(Number);+ // Create date objects in server timezoneconst startDateTime = new Date(shiftDate);startDateTime.setHours(startHour, startMinute, 0, 0);@@ -108,8 +113,13 @@ export async function GET(endDateTime.setDate(endDateTime.getDate() + 1);}- event.startDate = ICAL.Time.fromJSDate(startDateTime, false);- event.endDate = ICAL.Time.fromJSDate(endDateTime, false);+ // Convert to UTC for iCalendar standard compliance+ // JavaScript Date objects are timezone-aware and will be correctly converted+ const startIcalTime = ICAL.Time.fromJSDate(startDateTime, true);+ const endIcalTime = ICAL.Time.fromJSDate(endDateTime, true);++ event.startDate = startIcalTime;+ event.endDate = endIcalTime;}// Set color (using X-APPLE-CALENDAR-COLOR for Apple Calendar compatibility)diff --git a/lib/date-utils.ts b/lib/date-utils.tsindex 3f69c6a..a305a01 100644--- a/lib/date-utils.ts+++ b/lib/date-utils.ts@@ -71,3 +71,30 @@ export function formatDuration(minutes: number): string {const mins = minutes % 60;return `${hours}:${String(mins).padStart(2, "0")}`;}++/**+ * Gets the server timezone from environment variable or system default+ * Works in both Docker and non-Docker environments+ * @returns Timezone string (e.g., "Europe/Berlin", "America/New_York")+ */+export function getServerTimezone(): string {+ // Try to get from environment variable first (Docker or manual config)+ const envTimezone = process.env.TZ;+ if (envTimezone) {+ return envTimezone;+ }++ // Fallback: Try to detect system timezone+ // This works by checking the resolved timezone from Date+ try {+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;+ if (timezone) {+ return timezone;+ }+ } catch (error) {+ console.warn("Could not detect system timezone:", error);+ }++ // Last fallback: UTC+ return "UTC";+}