Fix landing 500 from invalid Asia/Esfahan timezone.
Normalize Iran city aliases to Asia/Tehran, harden the clock, and make settings timezone a safe select. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1 +1,2 @@
|
||||
export * from "./brand";
|
||||
export * from "./timezone";
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const FALLBACK_TZ = "Asia/Tehran";
|
||||
|
||||
/** Common mistaken city zones → valid IANA */
|
||||
const TZ_ALIASES: Record<string, string> = {
|
||||
"Asia/Esfahan": FALLBACK_TZ,
|
||||
"Asia/Isfahan": FALLBACK_TZ,
|
||||
"Asia/Mashhad": FALLBACK_TZ,
|
||||
"Asia/Shiraz": FALLBACK_TZ,
|
||||
"Asia/Tabriz": FALLBACK_TZ,
|
||||
"Iran": FALLBACK_TZ,
|
||||
};
|
||||
|
||||
export function resolveTimezone(input?: string | null): string {
|
||||
const raw = (input || FALLBACK_TZ).trim() || FALLBACK_TZ;
|
||||
const candidate = TZ_ALIASES[raw] || raw;
|
||||
try {
|
||||
// Throws RangeError if invalid
|
||||
Intl.DateTimeFormat("en-US", { timeZone: candidate });
|
||||
return candidate;
|
||||
} catch {
|
||||
return FALLBACK_TZ;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatClock(date: Date, timezone?: string | null, locale = "fa-IR") {
|
||||
const tz = resolveTimezone(timezone);
|
||||
return new Intl.DateTimeFormat(locale, {
|
||||
timeZone: tz,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: false,
|
||||
}).format(date);
|
||||
}
|
||||
Reference in New Issue
Block a user