Improve timestamp handling and event navigation
This commit is contained in:
@@ -1,7 +1,34 @@
|
||||
export function fmtDate(iso?: string | null): string {
|
||||
export const DEFAULT_TIME_ZONE = "UTC";
|
||||
|
||||
export function normalizeTimeZone(timeZone?: string | null): string {
|
||||
const value = timeZone?.trim();
|
||||
if (!value) return DEFAULT_TIME_ZONE;
|
||||
try {
|
||||
new Intl.DateTimeFormat("en-US", { timeZone: value }).format(new Date(0));
|
||||
return value;
|
||||
} catch {
|
||||
return DEFAULT_TIME_ZONE;
|
||||
}
|
||||
}
|
||||
|
||||
export function fmtDate(iso?: string | null, timeZone = DEFAULT_TIME_ZONE): string {
|
||||
if (!iso) return "—";
|
||||
const d = new Date(iso);
|
||||
return Number.isNaN(d.getTime()) ? iso : d.toISOString().replace("T", " ").replace(/\..+$/, "Z");
|
||||
if (Number.isNaN(d.getTime())) return iso;
|
||||
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone: normalizeTimeZone(timeZone),
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: false,
|
||||
timeZoneName: "short",
|
||||
}).formatToParts(d);
|
||||
const get = (type: string) => parts.find((p) => p.type === type)?.value ?? "";
|
||||
return `${get("year")}-${get("month")}-${get("day")} ${get("hour")}:${get("minute")}:${get("second")} ${get("timeZoneName")}`;
|
||||
}
|
||||
|
||||
export function statusBadgeClass(s: string): string {
|
||||
|
||||
7
web/src/lib/timezone.ts
Normal file
7
web/src/lib/timezone.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import "server-only";
|
||||
|
||||
import { DEFAULT_TIME_ZONE, normalizeTimeZone } from "@/lib/format";
|
||||
|
||||
export function getUiTimeZone(): string {
|
||||
return normalizeTimeZone(process.env.MONLET_WEB_TIME_ZONE ?? process.env.TZ ?? DEFAULT_TIME_ZONE);
|
||||
}
|
||||
Reference in New Issue
Block a user