Skip to content

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

🐛 Fix timezone conversion (#894)

Luke Vella04d8b7128142
diff --git a/apps/web/src/components/poll/desktop-poll.tsx b/apps/web/src/components/poll/desktop-poll.tsx
index 42a6d6dc9..dcdf74732 100644
--- a/apps/web/src/components/poll/desktop-poll.tsx
+++ b/apps/web/src/components/poll/desktop-poll.tsx
@@ -222,7 +222,7 @@ const DesktopPoll: React.FunctionComponent = () => {
) : null}
</div>
</div>
- {poll.options[0].duration !== 0 ? (
+ {poll.options[0].duration !== 0 && poll.timeZone ? (
<div className="border-b bg-gray-50 p-3">
<TimesShownIn />
</div>
diff --git a/apps/web/src/components/poll/mobile-poll.tsx b/apps/web/src/components/poll/mobile-poll.tsx
index 4d2ae9a3c..e0bb5ed1e 100644
--- a/apps/web/src/components/poll/mobile-poll.tsx
+++ b/apps/web/src/components/poll/mobile-poll.tsx
@@ -152,7 +152,7 @@ const MobilePoll: React.FunctionComponent = () => {
) : null}
</div>
</div>
- {poll.options[0].duration !== 0 ? (
+ {poll.options[0].duration !== 0 && poll.timeZone ? (
<div className="flex border-b bg-gray-50 p-3">
<TimesShownIn />
</div>
diff --git a/apps/web/src/contexts/time-preferences.tsx b/apps/web/src/contexts/time-preferences.tsx
deleted file mode 100644
index 7c5f12cbb..000000000
--- a/apps/web/src/contexts/time-preferences.tsx
+++ /dev/null
@@ -1,71 +0,0 @@
-import { trpc } from "@rallly/backend";
-import { GlobeIcon } from "@rallly/icons";
-import { Popover, PopoverContent, PopoverTrigger } from "@rallly/ui/popover";
-import dayjs, { Dayjs } from "dayjs";
-import React from "react";
-
-import { TimesShownIn } from "@/components/clock";
-import { TimeZoneCommand } from "@/components/time-zone-picker/time-zone-select";
-import { usePoll } from "@/contexts/poll";
-import { useDayjs } from "@/utils/dayjs";
-
-export const TimePreferences = () => {
- const { timeZone } = useDayjs();
- const queryClient = trpc.useContext();
-
- const [open, setIsOpen] = React.useState(false);
- const { data } = trpc.userPreferences.get.useQuery();
-
- const updatePreferences = trpc.userPreferences.update.useMutation({
- onMutate: (newPreferences) => {
- queryClient.userPreferences.get.setData(undefined, (oldPreferences) => {
- if (!oldPreferences) {
- return null;
- }
- return {
- ...oldPreferences,
- timeZone: newPreferences.timeZone ?? oldPreferences?.timeZone ?? null,
- };
- });
- },
- onSuccess: () => {
- queryClient.userPreferences.get.invalidate();
- },
- });
-
- if (data === undefined) {
- return null;
- }
-
- return (
- <Popover open={open} onOpenChange={setIsOpen}>
- <PopoverTrigger className="inline-flex items-center gap-x-2 text-sm hover:underline">
- <GlobeIcon className="h-4 w-4" />
- <TimesShownIn />
- </PopoverTrigger>
- <PopoverContent align="center" className="p-0">
- <TimeZoneCommand
- value={timeZone}
- onSelect={(value) => {
- updatePreferences.mutate({
- timeZone: value,
- });
- setIsOpen(false);
- }}
- />
- </PopoverContent>
- </Popover>
- );
-};
-export const useDateFormatter = () => {
- const { timeZone } = usePoll();
- const { timeZone: preferredTimeZone } = useDayjs();
-
- return (date: Date | Dayjs) => {
- if (timeZone) {
- return dayjs(date).utc().tz(timeZone, true).tz(preferredTimeZone);
- }
-
- return dayjs(date).utc();
- };
-};
diff --git a/apps/web/src/utils/date-time-utils.ts b/apps/web/src/utils/date-time-utils.ts
index daf0784d7..187ed892d 100644
--- a/apps/web/src/utils/date-time-utils.ts
+++ b/apps/web/src/utils/date-time-utils.ts
@@ -105,9 +105,18 @@ export const parseTimeSlotOption = (
timeFormat: TimeFormat,
): ParsedTimeSlotOption => {
const adjustTimeZone = (date: Date | dayjs.Dayjs) => {
- return timeZone && targetTimeZone
- ? dayjs(date).utc().tz(timeZone, true).tz(targetTimeZone)
- : dayjs(date).utc();
+ const d = dayjs(date);
+ if (!timeZone) {
+ return d.utc();
+ }
+
+ const t = d.tz(timeZone, true);
+
+ if (targetTimeZone !== timeZone) {
+ return t.tz(targetTimeZone);
+ } else {
+ return t;
+ }
};
const startDate = adjustTimeZone(option.start);