Skip to content

Commit

Permalink
More timezone fixes
Browse files Browse the repository at this point in the history
arjunkomath committed Jan 15, 2025
1 parent f3d0760 commit 7ee63af
Showing 6 changed files with 41 additions and 23 deletions.
10 changes: 7 additions & 3 deletions app/(dashboard)/[tenant]/projects/[projectId]/events/actions.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import { calendarEvent, eventInvite } from "@/drizzle/schema";
import { generateObjectDiffMessage, logActivity } from "@/lib/activity";
import { toEndOfDay } from "@/lib/utils/date";
import { toEndOfDay, toMachineDateString } from "@/lib/utils/date";
import { database } from "@/lib/utils/useDatabase";
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
import { and, eq } from "drizzle-orm";
@@ -95,9 +95,11 @@ export async function createEvent(payload: FormData) {
projectId: +projectId,
});

const timezone = await getTimezone();

revalidatePath(`/${orgSlug}/projects/${projectId}/events`);
redirect(
`/${orgSlug}/projects/${projectId}/events?on=${start.toISOString()}`,
`/${orgSlug}/projects/${projectId}/events?on=${toMachineDateString(start, timezone)}`,
);
}

@@ -163,9 +165,11 @@ export async function updateEvent(payload: FormData) {
projectId: +projectId,
});

const timezone = await getTimezone();

revalidatePath(`/${orgSlug}/projects/${projectId}/events`);
redirect(
`/${orgSlug}/projects/${projectId}/events?on=${start.toISOString()}`,
`/${orgSlug}/projects/${projectId}/events?on=${toMachineDateString(start, timezone)}`,
);
}

Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
toDateStringWithDay,
toEndOfDay,
toStartOfDay,
toTimeZone,
} from "@/lib/utils/date";
import { database } from "@/lib/utils/useDatabase";
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
@@ -43,7 +44,7 @@ export default async function EventDetails(props: Props) {

const timezone = await getTimezone();

const selectedDate = on ? new Date(on) : new Date();
const selectedDate = toTimeZone(on ? new Date(on) : new Date(), "UTC");
const dayCommentId = `${projectId}${selectedDate.getFullYear()}${selectedDate.getMonth()}${selectedDate.getDay()}`;

const startOfDay = toStartOfDay(selectedDate);
@@ -62,6 +63,8 @@ export default async function EventDetails(props: Props) {
gt(calendarEvent.end, endOfDay),
),
isNotNull(calendarEvent.repeatRule),
eq(calendarEvent.start, startOfDay),
eq(calendarEvent.end, endOfDay),
),
),
orderBy: [desc(calendarEvent.start), asc(calendarEvent.allDay)],
8 changes: 1 addition & 7 deletions components/project/events/date-time-picker.tsx
Original file line number Diff line number Diff line change
@@ -220,12 +220,6 @@ export function DateTimePicker({
const handleSelect = (newDay: Date | undefined) => {
if (!newDay) return;

if (dateOnly) {
onSelect?.(toStartOfDay(new Date(newDay)));
setDate(newDay);
return;
}

if (!date) {
setDate(toStartOfDay(newDay));
return;
@@ -235,7 +229,7 @@ export function DateTimePicker({
const diffInDays = diff / (1000 * 60 * 60 * 24);
const newDateFull = add(date, { days: Math.ceil(diffInDays) });
setDate(newDateFull);
onSelect?.(new Date(newDateFull));
onSelect?.(newDateFull);
};

return (
6 changes: 3 additions & 3 deletions components/project/events/events-calendar.tsx
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import { Calendar } from "@/components/ui/calendar";
import type { EventWithInvites } from "@/drizzle/types";
import { toDateString } from "@/lib/utils/date";
import { toMachineDateString } from "@/lib/utils/date";
import { useRouter } from "next/navigation";
import EventsList from "./events-list";

@@ -25,7 +25,7 @@ export default function EventsCalendar({
}) {
const router = useRouter();

const currentDate = toDateString(
const currentDate = toMachineDateString(
selectedDate ? new Date(selectedDate) : new Date(),
timezone,
);
@@ -38,7 +38,7 @@ export default function EventsCalendar({
selected={new Date(currentDate)}
onDayClick={(date) => {
router.push(
`/${orgSlug}/projects/${projectId}/events?on=${date.toISOString()}`,
`/${orgSlug}/projects/${projectId}/events?on=${toMachineDateString(date, timezone)}`,
);
}}
/>
6 changes: 3 additions & 3 deletions components/project/events/events-list.tsx
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import {
import type { EventWithInvites } from "@/drizzle/types";
import { cn } from "@/lib/utils";
import {
toDateString,
toDateStringWithDay,
toDateTimeString,
toEndOfDay,
toStartOfDay,
@@ -83,12 +83,12 @@ export default function EventsList({
suppressHydrationWarning
>
{event.allDay
? toDateString(event.start, timezone)
? toDateStringWithDay(event.start, timezone)
: toDateTimeString(event.start, timezone)}
{event.end
? ` - ${
event.allDay
? toDateString(event.end, timezone)
? toDateStringWithDay(event.end, timezone)
: toDateTimeString(event.end, timezone)
}`
: null}
29 changes: 23 additions & 6 deletions lib/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
export function toStartOfDay(date: Date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

export function toTimeZone(date: Date, timeZone: string) {
return new Date(date.toLocaleString("en-US", { timeZone }));
}

export function toStartOfDay(date: Date) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(),
0,
0,
0,
0,
);
}

export function toEndOfDay(date: Date) {
return new Date(
date.getFullYear(),
@@ -22,8 +30,8 @@ export function toDateString(date: Date, timeZone: string) {
return date.toLocaleDateString("en-US", {
timeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
month: "short",
day: "numeric",
});
}

@@ -49,6 +57,15 @@ export function toDateStringWithDay(date: Date, timeZone: string) {
});
}

export function toMachineDateString(date: Date, timeZone: string) {
return date.toLocaleDateString("en-US", {
timeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
}

export function isSameDate(a: Date, b: Date) {
return (
a.getFullYear() === b.getFullYear() &&

0 comments on commit 7ee63af

Please sign in to comment.