Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Fix floating times in ICS (#1928)

Luke Vella479b644459f0
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index 0c212fd01..460672d7d 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -818,19 +818,17 @@ export const polls = router({
email: a.email as string,
}));
- const eventEnd =
- option.duration > 0
- ? eventStart.add(option.duration, "minutes")
- : eventStart.add(1, "day");
-
const event = createIcsEvent({
uid,
sequence: 0,
title: poll.title,
location: poll.location ?? undefined,
description: poll.description ?? undefined,
- start: eventStart.toDate(),
- end: eventEnd.toDate(),
+ start: option.startTime,
+ end:
+ option.duration > 0
+ ? dayjs(option.startTime).add(option.duration, "minute").toDate()
+ : dayjs(option.startTime).add(1, "day").toDate(),
allDay: option.duration === 0,
timeZone: poll.timeZone ?? undefined,
organizer: {
diff --git a/apps/web/src/utils/ics.test.ts b/apps/web/src/utils/ics.test.ts
index 5da3901f5..705d0cc62 100644
--- a/apps/web/src/utils/ics.test.ts
+++ b/apps/web/src/utils/ics.test.ts
@@ -82,44 +82,38 @@ describe("createIcsEvent", () => {
});
describe("Floating time events", () => {
- it("should use local time format when no timezone specified", () => {
- // Create a local date using Date constructor (not ISO string)
- const localStart = new Date(2024, 2, 15, 10, 30, 0); // March 15, 2024, 10:30 AM local
- const localEnd = new Date(2024, 2, 15, 11, 30, 0); // March 15, 2024, 11:30 AM local
+ it("should preserve UTC times as floating when no timezone specified", () => {
+ const utcTime = new Date("2024-03-15T12:00:00Z");
const result = createIcsEvent({
...baseOptions,
+ start: utcTime,
+ end: new Date(utcTime.getTime() + 60 * 60 * 1000),
timeZone: undefined,
- start: localStart,
- end: localEnd,
});
expect(result.error).toBeFalsy();
-
- // For floating time, the ICS library may still add Z suffix but the important
- // part is that startInputType is "local" so times are treated as local
- const hasFloatingTime = result.value?.includes("DTSTART:20240315T103000");
- const hasUTCTime = result.value?.includes("DTSTART:20240315T103000Z");
-
- // Either floating time format (no Z) OR the library adds Z but uses local time
- expect(hasFloatingTime || hasUTCTime).toBe(true);
- expect(result.value).toContain("DTEND:20240315T113000");
+ expect(result.value).toContain("DTSTART:20240315T120000");
+ expect(result.value).toContain("DTEND:20240315T130000");
+ expect(result.value).not.toContain("DTSTART:20240315T120000Z");
+ expect(result.value).not.toContain("DTEND:20240315T130000Z");
});
- it("should maintain same clock time for floating events", () => {
- // Create date in different timezone contexts
- const mockDate = new Date(2024, 2, 15, 14, 30, 0); // March 15, 2024, 2:30 PM local
+ it("should treat empty string timezone as floating", () => {
+ const utcTime = new Date("2024-03-15T12:00:00Z");
const result = createIcsEvent({
...baseOptions,
- start: mockDate,
- end: new Date(mockDate.getTime() + 60 * 60 * 1000), // +1 hour
- timeZone: undefined, // Floating time
+ start: utcTime,
+ end: new Date(utcTime.getTime() + 60 * 60 * 1000),
+ timeZone: "",
});
- // Should use the raw hour/minute values
- expect(result.value).toContain("DTSTART:20240315T143000");
- expect(result.value).toContain("DTEND:20240315T153000");
+ expect(result.error).toBeFalsy();
+ expect(result.value).toContain("DTSTART:20240315T120000");
+ expect(result.value).toContain("DTEND:20240315T130000");
+ expect(result.value).not.toContain("DTSTART:20240315T120000Z");
+ expect(result.value).not.toContain("DTEND:20240315T130000Z");
});
});
@@ -224,18 +218,6 @@ describe("createIcsEvent", () => {
expect(result.value).toContain("DTSTART:20231231T233000Z");
expect(result.value).toContain("DTEND:20240101T003000Z");
});
-
- it("should handle single-digit months and days", () => {
- const result = createIcsEvent({
- ...baseOptions,
- start: new Date("2024-01-05T09:05:00Z"),
- end: new Date("2024-01-05T10:05:00Z"),
- timeZone: "UTC",
- });
-
- expect(result.value).toContain("DTSTART:20240105T090500Z");
- expect(result.value).toContain("DTEND:20240105T100500Z");
- });
});
describe("Error handling", () => {
diff --git a/apps/web/src/utils/ics.ts b/apps/web/src/utils/ics.ts
index c87baa8af..1a2ed0f18 100644
--- a/apps/web/src/utils/ics.ts
+++ b/apps/web/src/utils/ics.ts
@@ -28,78 +28,61 @@ export interface CreateIcsEventOptions {
* Creates an ICS event with proper date/time handling
* Handles all-day events, timezone conversions, and floating time automatically
*/
-export function createIcsEvent(options: CreateIcsEventOptions): {
+export function createIcsEvent({
+ uid,
+ start,
+ end,
+ allDay,
+ timeZone,
+ sequence = 0,
+ title,
+ description,
+ location,
+ organizer,
+ attendees,
+ method = "request",
+ status = "CONFIRMED",
+}: CreateIcsEventOptions): {
error?: Error;
value?: string;
} {
- const {
+ return ics.createEvent({
uid,
- start,
- end,
- allDay,
- timeZone,
- sequence = 0,
+ productId,
+ sequence,
title,
description,
location,
organizer,
attendees,
- method = "request",
- status = "CONFIRMED",
- } = options;
-
- let eventAttributes: ics.EventAttributes;
-
- if (allDay) {
- // All-day events: use date-only format (3 values)
- eventAttributes = {
- uid,
- productId,
- sequence,
- title,
- description,
- location,
- organizer,
- attendees,
- method,
- status,
- start: [
- start.getUTCFullYear(),
- start.getUTCMonth() + 1,
- start.getUTCDate(),
- ],
- end: [end.getUTCFullYear(), end.getUTCMonth() + 1, end.getUTCDate()],
- };
- } else {
- // Timezone-aware events: convert to UTC
- eventAttributes = {
- uid,
- productId,
- sequence,
- title,
- description,
- location,
- organizer,
- attendees,
- method,
- status,
- start: [
- start.getUTCFullYear(),
- start.getUTCMonth() + 1,
- start.getUTCDate(),
- start.getUTCHours(),
- start.getUTCMinutes(),
- ],
- end: [
- end.getUTCFullYear(),
- end.getUTCMonth() + 1,
- end.getUTCDate(),
- end.getUTCHours(),
- end.getUTCMinutes(),
- ],
- startInputType: timeZone ? "utc" : "local",
- };
- }
-
- return ics.createEvent(eventAttributes);
+ method,
+ status,
+ startInputType: timeZone ? "utc" : "local",
+ startOutputType: timeZone ? "utc" : "local",
+ ...(allDay
+ ? {
+ start: [
+ start.getUTCFullYear(),
+ start.getUTCMonth() + 1,
+ start.getUTCDate(),
+ ],
+ end: [end.getUTCFullYear(), end.getUTCMonth() + 1, end.getUTCDate()],
+ }
+ : {
+ start: [
+ start.getUTCFullYear(),
+ start.getUTCMonth() + 1,
+ start.getUTCDate(),
+ start.getUTCHours(),
+ start.getUTCMinutes(),
+ ],
+ end: [
+ end.getUTCFullYear(),
+ end.getUTCMonth() + 1,
+ end.getUTCDate(),
+ end.getUTCHours(),
+ end.getUTCMinutes(),
+ ],
+ }),
+ });
}