Skip to content

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

♻️ Migrate event email datetime formatting to Intl (#2530)

Luke Vella77bafac90782
diff --git a/apps/web/src/features/scheduled-event/utils.test.ts b/apps/web/src/features/scheduled-event/utils.test.ts
new file mode 100644
index 000000000..e74d84974
--- /dev/null
+++ b/apps/web/src/features/scheduled-event/utils.test.ts
@@ -0,0 +1,86 @@
+import { describe, expect, it } from "vitest";
+import { formatEventDateTime } from "./utils";
+
+// 26 Jun 2026, 13:00–14:00 UTC.
+const start = new Date(Date.UTC(2026, 5, 26, 13));
+const end = new Date(Date.UTC(2026, 5, 26, 14));
+
+describe("formatEventDateTime", () => {
+ it("formats all-day events in UTC with no time", () => {
+ const result = formatEventDateTime({
+ start: new Date(Date.UTC(2026, 5, 26)),
+ end: new Date(Date.UTC(2026, 5, 27)),
+ allDay: true,
+ timeZone: null,
+ inviteeTimeZone: "America/New_York",
+ });
+ expect(result).toEqual({
+ date: "June 26, 2026",
+ day: "26",
+ dow: "Fri",
+ time: undefined,
+ });
+ });
+
+ it("renders fixed events in the invitee's zone with a zone name", () => {
+ const result = formatEventDateTime({
+ start,
+ end,
+ allDay: false,
+ timeZone: "Europe/London",
+ inviteeTimeZone: "America/New_York",
+ });
+ expect(result.date).toBe("June 26, 2026");
+ expect(result.time).toContain("9:00");
+ expect(result.time).toMatch(/10:00\sAM/);
+ expect(result.time).toContain("EDT");
+ });
+
+ it("falls back to the event zone when the invitee zone is unknown", () => {
+ const result = formatEventDateTime({
+ start,
+ end,
+ allDay: false,
+ timeZone: "Europe/London",
+ });
+ expect(result.time).toContain("2:00");
+ expect(result.time).toMatch(/3:00\sPM/);
+ });
+
+ it("renders floating events as UTC wall time with no zone name", () => {
+ const result = formatEventDateTime({
+ start,
+ end,
+ allDay: false,
+ timeZone: null,
+ inviteeTimeZone: "America/New_York",
+ });
+ expect(result.time).toContain("1:00");
+ expect(result.time).toMatch(/2:00\sPM/);
+ expect(result.time).not.toMatch(/GMT|UTC|[A-Z]{3,4}$/);
+ });
+
+ it("respects the recipient's preferred time format", () => {
+ const result = formatEventDateTime({
+ start,
+ end,
+ allDay: false,
+ timeZone: "Europe/London",
+ timeFormat: "hours24",
+ });
+ expect(result.time).toContain("14:00");
+ expect(result.time).not.toMatch(/PM/i);
+ });
+
+ it("formats in the recipient's locale", () => {
+ const result = formatEventDateTime({
+ start,
+ end,
+ allDay: false,
+ timeZone: "Europe/Berlin",
+ locale: "de",
+ });
+ expect(result.date).toBe("26. Juni 2026");
+ expect(result.time).toContain("15:00");
+ });
+});
diff --git a/apps/web/src/features/scheduled-event/utils.ts b/apps/web/src/features/scheduled-event/utils.ts
index 30392586a..8ca60c8a8 100644
--- a/apps/web/src/features/scheduled-event/utils.ts
+++ b/apps/web/src/features/scheduled-event/utils.ts
@@ -1,10 +1,16 @@
-import { dayjs } from "@/lib/dayjs";
+import type { TimeFormat } from "@rallly/database";
+import {
+ formatDateParts,
+ formatDateTime,
+ formatDateTimeRange,
+} from "@/lib/datetime/format";
export interface FormattedEventDateTime {
date: string;
day: string;
dow: string;
- time: string;
+ /** Undefined for all-day events; the email templates render a localized "All day" label. */
+ time?: string;
}
interface FormatEventDateTimeOptions {
@@ -13,15 +19,23 @@ interface FormatEventDateTimeOptions {
allDay: boolean;
timeZone?: string | null;
inviteeTimeZone?: string | null;
+ /** Must match the locale the email template renders in. */
+ locale?: string;
+ /** The recipient's preferred hour cycle; the locale decides when unset. */
+ timeFormat?: TimeFormat | null;
}
/**
- * Formats event date and time based on event type and timezone settings
+ * Builds the date/time display strings for the finalized/canceled email
+ * templates.
*
- * Handles three scenarios:
- * 1. All-day events (same date for everyone)
- * 2. Events with a timezone (adjust to invitee's timezone if available)
- * 3. Events without a timezone (floating time - show in UTC)
+ * Zone semantics:
+ * - All-day events are stored as UTC midnight and format in UTC, so every
+ * recipient sees the same date.
+ * - Fixed events (with a zone) render in the recipient's zone when known,
+ * falling back to the event's zone, with the zone name appended.
+ * - Floating events (no zone) render their wall time, stored as UTC, with no
+ * zone name.
*/
export const formatEventDateTime = ({
start,
@@ -29,40 +43,32 @@ export const formatEventDateTime = ({
allDay,
timeZone,
inviteeTimeZone,
+ locale = "en",
+ timeFormat,
}: FormatEventDateTimeOptions): FormattedEventDateTime => {
- if (allDay) {
- // For all-day events, show the same date to everyone
- const eventDate = dayjs(start).utc();
- return {
- date: eventDate.format("MMMM D, YYYY"),
- day: eventDate.format("D"),
- dow: eventDate.format("ddd"),
- time: "All day",
- };
- }
-
- if (timeZone) {
- // If event has a timezone, adjust to invitee's timezone if available
- const targetTimeZone = inviteeTimeZone || timeZone;
-
- const startTime = dayjs(start).tz(targetTimeZone);
- const endTime = dayjs(end).tz(targetTimeZone);
-
- return {
- date: startTime.format("MMMM D, YYYY"),
- day: startTime.format("D"),
- dow: startTime.format("ddd"),
- time: `${startTime.format("HH:mm")} - ${endTime.format("HH:mm z")}`,
- };
- }
-
- const startTime = dayjs(start).utc();
- const endTime = dayjs(end).utc();
+ const displayTimeZone =
+ allDay || !timeZone ? "UTC" : inviteeTimeZone || timeZone;
+ const { weekday, day } = formatDateParts(start, {
+ locale,
+ timeZone: displayTimeZone,
+ });
return {
- date: startTime.format("MMMM D, YYYY"),
- day: startTime.format("D"),
- dow: startTime.format("ddd"),
- time: `${startTime.format("HH:mm")} - ${endTime.format("HH:mm")}`,
+ date: formatDateTime(start, {
+ preset: "dateLong",
+ locale,
+ timeZone: displayTimeZone,
+ }),
+ day,
+ dow: weekday,
+ time: allDay
+ ? undefined
+ : formatDateTimeRange(start, end, {
+ preset: "time",
+ locale,
+ timeFormat: timeFormat ?? undefined,
+ timeZone: displayTimeZone,
+ showTimeZone: Boolean(timeZone),
+ }),
};
};
diff --git a/apps/web/src/trpc/routers/events.ts b/apps/web/src/trpc/routers/events.ts
index 21b89abc5..756777395 100644
--- a/apps/web/src/trpc/routers/events.ts
+++ b/apps/web/src/trpc/routers/events.ts
@@ -175,11 +175,13 @@ export const events = router({
allDay: updatedEvent.allDay,
timeZone: updatedEvent.timeZone,
inviteeTimeZone: invite.inviteeTimeZone,
+ locale: invite.inviteeLocale ?? undefined,
});
after(async () =>
sendEventCanceledEmail({
to: invite.inviteeEmail,
+ locale: invite.inviteeLocale ?? undefined,
branding: await getInstanceBranding(),
icalEvent: {
filename: "cancel.ics",
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.ts
index ce386f0a0..bc729ee40 100644
--- a/apps/web/src/trpc/routers/polls.ts
+++ b/apps/web/src/trpc/routers/polls.ts
@@ -731,6 +731,7 @@ export const polls = router({
name: true,
email: true,
locale: true,
+ timeFormat: true,
},
},
participants: {
@@ -966,16 +967,19 @@ export const polls = router({
});
}
+ const hostEmail = poll.user.email;
+ const hostName = poll.user.name;
+ const hostLocale = poll.user.locale ?? undefined;
+
const { date, day, dow, time } = formatEventDateTime({
start: scheduledEvent.start,
end: scheduledEvent.end,
allDay: scheduledEvent.allDay,
timeZone: scheduledEvent.timeZone,
+ locale: hostLocale,
+ timeFormat: poll.user.timeFormat,
});
- const hostEmail = poll.user.email;
- const hostName = poll.user.name;
- const hostLocale = poll.user.locale ?? undefined;
const space = poll.space;
after(async () =>
sendFinalizeHostEmail({
@@ -1014,6 +1018,7 @@ export const polls = router({
allDay: scheduledEvent.allDay,
timeZone: scheduledEvent.timeZone,
inviteeTimeZone: p.timeZone,
+ locale: p.locale,
});
after(async () =>
sendFinalizeParticipantEmail({
diff --git a/packages/emails/locales/en/emails.json b/packages/emails/locales/en/emails.json
index 309c6f521..202dc9061 100644
--- a/packages/emails/locales/en/emails.json
+++ b/packages/emails/locales/en/emails.json
@@ -1,4 +1,5 @@
{
+ "allDay": "All day",
"changeEmail_codeValid": "This code is valid for 15 minutes",
"changeEmail_content2": "You're receiving this email because a request was made to change the email address of an account on <domain />. If this wasn't you contact <a>{supportEmail}</a>.",
"changeEmail_heading": "Verify your new email address",
diff --git a/packages/emails/src/templates/event-canceled.tsx b/packages/emails/src/templates/event-canceled.tsx
index 308c9c532..17faa8151 100644
--- a/packages/emails/src/templates/event-canceled.tsx
+++ b/packages/emails/src/templates/event-canceled.tsx
@@ -33,7 +33,7 @@ export type EventCanceledEmailProps = {
day: string;
dow: string;
date: string;
- time: string;
+ time?: string;
};
async function EventCanceledEmail({
@@ -114,7 +114,7 @@ async function EventCanceledEmail({
<Column style={{ paddingLeft: 16 }} align="left">
<Text style={{ margin: 0, fontWeight: "bold" }}>{date}</Text>
<Text light={true} style={{ margin: 0 }}>
- {time}
+ {time ?? t("allDay", { defaultValue: "All day" })}
</Text>
</Column>
</Row>
diff --git a/packages/emails/src/templates/finalized-host.tsx b/packages/emails/src/templates/finalized-host.tsx
index fa8d0d8bb..63233b359 100644
--- a/packages/emails/src/templates/finalized-host.tsx
+++ b/packages/emails/src/templates/finalized-host.tsx
@@ -32,7 +32,7 @@ type FinalizeHostEmailProps = {
date: string;
day: string;
dow: string;
- time: string;
+ time?: string;
name: string;
title: string;
location: string | null;
@@ -118,7 +118,7 @@ async function FinalizeHostEmail({
<Column style={{ paddingLeft: 16 }} align="left">
<Text style={{ margin: 0, fontWeight: "bold" }}>{date}</Text>
<Text light={true} style={{ margin: 0 }}>
- {time}
+ {time ?? t("allDay", { defaultValue: "All day" })}
</Text>
</Column>
</Row>
diff --git a/packages/emails/src/templates/finalized-participant.tsx b/packages/emails/src/templates/finalized-participant.tsx
index a89f30e56..1e0d34cc7 100644
--- a/packages/emails/src/templates/finalized-participant.tsx
+++ b/packages/emails/src/templates/finalized-participant.tsx
@@ -32,7 +32,7 @@ type FinalizeParticipantEmailProps = {
date: string;
day: string;
dow: string;
- time: string;
+ time?: string;
title: string;
hostName: string;
pollUrl: string;
@@ -117,7 +117,7 @@ async function FinalizeParticipantEmail({
<Column style={{ paddingLeft: 16 }} align="left">
<Text style={{ margin: 0, fontWeight: "bold" }}>{date}</Text>
<Text light={true} style={{ margin: 0 }}>
- {time}
+ {time ?? t("allDay", { defaultValue: "All day" })}
</Text>
</Column>
</Row>