Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Ignore browser time zones that Intl rejects (#3609)
Luke Vella0dedcee8e3dc
diff --git a/apps/web/src/components/time-zone-picker/timezone-data.ts b/apps/web/src/components/time-zone-picker/timezone-data.tsindex c2ed8222b..f54f6e1d9 100644--- a/apps/web/src/components/time-zone-picker/timezone-data.ts+++ b/apps/web/src/components/time-zone-picker/timezone-data.ts@@ -1,4 +1,4 @@-import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";+import { detectBrowserTimeZone } from "@/lib/utils/date-time-utils";import {getIanaIdAliases,normalizeLegacyIanaId,@@ -15,7 +15,7 @@ export function getAllTimezoneIds(): string[] {return Intl.supportedValuesOf("timeZone");}const ids = new Set(curatedTimezoneIds);- const ownZone = getBrowserTimeZone();+ const ownZone = detectBrowserTimeZone();if (ownZone) {ids.add(ownZone);}diff --git a/apps/web/src/features/poll/components/manage-poll/use-csv-exporter.ts b/apps/web/src/features/poll/components/manage-poll/use-csv-exporter.tsindex 687ce8b8d..724eafeac 100644--- a/apps/web/src/features/poll/components/manage-poll/use-csv-exporter.ts+++ b/apps/web/src/features/poll/components/manage-poll/use-csv-exporter.ts@@ -2,7 +2,9 @@ import { useParticipants, usePoll } from "@/features/poll/client";import { createOptionsContextValue } from "@/features/poll/components/poll-context";import { useTranslation } from "@/i18n/client";import { useDateTimeConfig } from "@/lib/datetime/client";+import { instantToWallTime } from "@/lib/datetime/wall-time";import { dayjs } from "@/lib/dayjs";+import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";export const useCsvExporter = () => {const poll = usePoll();@@ -23,7 +25,7 @@ export const useCsvExporter = () => {// offset) as a date, so the timezone lives in the header, not the cell.// Use the IANA zone name rather than a fixed offset so the label stays// correct for rows on either side of a DST change.- const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;+ const timeZone = getBrowserTimeZone();const header = [t("name"),t("email"),@@ -39,7 +41,10 @@ export const useCsvExporter = () => {return [participant.name,participant.email,- dayjs(participant.createdAt).format("YYYY-MM-DD HH:mm:ss"),+ instantToWallTime(new Date(participant.createdAt), timeZone).replace(+ "T",+ " ",+ ),...poll.options.map((option) => {const vote = participant.votes.find((vote) => {return vote.optionId === option.id;diff --git a/apps/web/src/features/user/components/timezone-mismatch-dialog.test.tsx b/apps/web/src/features/user/components/timezone-mismatch-dialog.test.tsxindex 6b6de6ad4..2ef276a24 100644--- a/apps/web/src/features/user/components/timezone-mismatch-dialog.test.tsx+++ b/apps/web/src/features/user/components/timezone-mismatch-dialog.test.tsx@@ -10,6 +10,7 @@ import { TimeZoneMismatchDialog } from "./timezone-mismatch-dialog";let mockTimeZone = "America/New_York";vi.mock("@/lib/utils/date-time-utils", () => ({getBrowserTimeZone: () => mockTimeZone,+ detectBrowserTimeZone: () => mockTimeZone,}));const { mockMutate } = vi.hoisted(() => ({ mockMutate: vi.fn() }));diff --git a/apps/web/src/lib/datetime/client.tsx b/apps/web/src/lib/datetime/client.tsxindex 7f06ee501..bf457f77a 100644--- a/apps/web/src/lib/datetime/client.tsx+++ b/apps/web/src/lib/datetime/client.tsx@@ -11,7 +11,7 @@ import {import { getLocaleDefaults, getWeekdayNames } from "@/lib/datetime/locales";import type { DateInput, TimeFormat } from "@/lib/datetime/types";import { normalizeTimeZone } from "@/lib/datetime/utils";-import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";+import { detectBrowserTimeZone } from "@/lib/utils/date-time-utils";type DateTimeConfig = {locale: string;@@ -41,7 +41,7 @@ export function DateTimeProvider({const [browserTimeZone, setBrowserTimeZone] = React.useState<string>();React.useEffect(() => {- setBrowserTimeZone(getBrowserTimeZone());+ setBrowserTimeZone(detectBrowserTimeZone());}, []);const value = React.useMemo(diff --git a/apps/web/src/lib/datetime/timezone-sync.tsx b/apps/web/src/lib/datetime/timezone-sync.tsxindex c7398478f..8130a4b87 100644--- a/apps/web/src/lib/datetime/timezone-sync.tsx+++ b/apps/web/src/lib/datetime/timezone-sync.tsx@@ -3,7 +3,8 @@import Cookies from "js-cookie";import React from "react";import { TIME_ZONE_COOKIE_NAME } from "@/lib/datetime/constants";-import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";+import { normalizeTimeZone } from "@/lib/datetime/utils";+import { detectBrowserTimeZone } from "@/lib/utils/date-time-utils";type TimeZoneChange = {previousTimeZone: string;@@ -37,10 +38,12 @@ export function TimeZoneSync({ children }: { children: React.ReactNode }) {return;}- const currentTimeZone = getBrowserTimeZone();- const previousTimeZone = Cookies.get(TIME_ZONE_COOKIE_NAME);+ const currentTimeZone = detectBrowserTimeZone();+ const previousTimeZone = normalizeTimeZone(+ Cookies.get(TIME_ZONE_COOKIE_NAME),+ );- if (previousTimeZone === currentTimeZone) {+ if (!currentTimeZone || previousTimeZone === currentTimeZone) {return;}diff --git a/apps/web/src/lib/utils/date-time-utils.test.ts b/apps/web/src/lib/utils/date-time-utils.test.tsnew file mode 100644index 000000000..a8f9e97c9--- /dev/null+++ b/apps/web/src/lib/utils/date-time-utils.test.ts@@ -0,0 +1,34 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { detectBrowserTimeZone, getBrowserTimeZone } from "./date-time-utils";++function reportTimeZone(timeZone: string) {+ const resolvedOptions = Intl.DateTimeFormat.prototype.resolvedOptions;+ vi.spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions").mockImplementation(+ function (this: Intl.DateTimeFormat) {+ return { ...resolvedOptions.call(this), timeZone };+ },+ );+}++afterEach(() => {+ vi.restoreAllMocks();+});++describe("detectBrowserTimeZone", () => {+ it("returns the zone the device reports", () => {+ reportTimeZone("Europe/Malta");+ expect(detectBrowserTimeZone()).toBe("Europe/Malta");+ });++ it("treats a zone Intl rejects as unknown", () => {+ reportTimeZone("Etc/Unknown");+ expect(detectBrowserTimeZone()).toBeUndefined();+ });+});++describe("getBrowserTimeZone", () => {+ it("falls back to UTC when the device zone is unknown", () => {+ reportTimeZone("Etc/Unknown");+ expect(getBrowserTimeZone()).toBe("UTC");+ });+});diff --git a/apps/web/src/lib/utils/date-time-utils.ts b/apps/web/src/lib/utils/date-time-utils.tsindex 18d090228..57320cd45 100644--- a/apps/web/src/lib/utils/date-time-utils.ts+++ b/apps/web/src/lib/utils/date-time-utils.ts@@ -2,9 +2,19 @@ import type {DateTimeOption,TimeOption,} from "@/features/poll/components/forms/poll-options-form/types";+import { normalizeTimeZone } from "@/lib/datetime/utils";+/**+ * The device's zone, or undefined when the engine reports one Intl itself+ * rejects (some devices report "Etc/Unknown").+ */+export function detectBrowserTimeZone() {+ return normalizeTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone);+}++/** The device's zone, falling back to UTC when it can't be detected. */export function getBrowserTimeZone() {- return Intl.DateTimeFormat().resolvedOptions().timeZone;+ return detectBrowserTimeZone() ?? "UTC";}export const encodeDateOption = (option: DateTimeOption) => {