Refactor: centralize timezone utilities in src/utils/timezone.ts (#491)
Deduplicate resolveTimezone, resolveTimezoneId, convertEventDateTimeToISO, and getTimezoneOffset into a single canonical module, removing copies from EventApi, eventUtils, TimezoneSelector, EventUpdateModal, and calendarUtils. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
// Ensure ICAL is imported before using this module.
|
||||
import ICAL from "ical.js";
|
||||
import moment from "moment-timezone";
|
||||
import { detectDateTimeFormat } from "@/components/Event/utils/dateTimeHelpers";
|
||||
|
||||
// TIMEZONES data must be imported or defined separately.
|
||||
import { TIMEZONES } from "./timezone-data";
|
||||
@@ -39,3 +41,61 @@ function findTimezone(tzid: string): any {
|
||||
|
||||
throw new Error(`Unknown timezone alias: ${tzid}`);
|
||||
}
|
||||
|
||||
export function resolveTimezone(tzName: string): string {
|
||||
if (TIMEZONES.zones[tzName]) {
|
||||
return tzName;
|
||||
}
|
||||
if (TIMEZONES.aliases[tzName]) {
|
||||
return TIMEZONES.aliases[tzName].aliasTo;
|
||||
}
|
||||
return tzName;
|
||||
}
|
||||
|
||||
export function resolveTimezoneId(tzid?: string): string | undefined {
|
||||
if (!tzid) return undefined;
|
||||
return resolveTimezone(tzid);
|
||||
}
|
||||
|
||||
export function convertEventDateTimeToISO(
|
||||
datetime: string,
|
||||
timezone: string,
|
||||
options?: { isAllDay?: boolean }
|
||||
): string | undefined {
|
||||
if (!datetime || !timezone) return undefined;
|
||||
if (options?.isAllDay) return undefined;
|
||||
|
||||
if (datetime.includes("Z") || datetime.match(/[+-]\d{2}:\d{2}$/)) {
|
||||
return datetime;
|
||||
}
|
||||
|
||||
const dateOnlyRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
||||
if (dateOnlyRegex.test(datetime)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const format = detectDateTimeFormat(datetime);
|
||||
const momentDate = moment.tz(datetime, format, timezone);
|
||||
if (!momentDate.isValid()) {
|
||||
console.warn(
|
||||
`[convertEventDateTimeToISO] Invalid datetime: "${datetime}" with format "${format}" in timezone "${timezone}"`
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
return momentDate.toISOString();
|
||||
}
|
||||
|
||||
export function getTimezoneOffset(
|
||||
tzName: string,
|
||||
date: Date = new Date()
|
||||
): string {
|
||||
const fmt = new Intl.DateTimeFormat(undefined, {
|
||||
timeZone: tzName,
|
||||
timeZoneName: "shortOffset",
|
||||
});
|
||||
|
||||
const currentDate = moment(date).isValid() ? date : new Date();
|
||||
const parts = fmt.formatToParts(currentDate);
|
||||
const offsetPart = parts.find((p) => p.type === "timeZoneName");
|
||||
return offsetPart?.value.replace("GMT", "UTC") ?? "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user