31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { fromZonedTime } from "date-fns-tz";
|
|
|
|
// Wall-clock inputs ("2026-07-20T09:00") carry no offset; anything with an
|
|
// explicit offset or Z suffix is already an absolute instant.
|
|
const EXPLICIT_OFFSET = /(?:Z|[+-]\d{2}:?\d{2})$/i;
|
|
|
|
export function isValidTimeZone(timeZone: string): boolean {
|
|
try {
|
|
new Intl.DateTimeFormat("en-US", { timeZone });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export type ParsedScheduledAt = { ok: true; date: Date | null } | { ok: false };
|
|
|
|
/**
|
|
* Resolves the campaign "scheduledAt" form value to a UTC instant.
|
|
* Wall-clock strings are interpreted in the campaign's IANA timezone —
|
|
* NOT in the server's local timezone (that was the pre-fix behavior and
|
|
* shifted scheduled sends by the server/tz offset difference).
|
|
*/
|
|
export function parseScheduledAt(value: string | null, timeZone: string): ParsedScheduledAt {
|
|
if (!value) return { ok: true, date: null };
|
|
if (!isValidTimeZone(timeZone)) return { ok: false };
|
|
const date = EXPLICIT_OFFSET.test(value) ? new Date(value) : fromZonedTime(value, timeZone);
|
|
if (Number.isNaN(date.getTime())) return { ok: false };
|
|
return { ok: true, date };
|
|
}
|