File size: 1,730 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /**
* Shared helpers for asserting envelope timestamp normalization in channel tests.
*/
import {
formatUtcTimestamp,
formatZonedTimestamp,
} from "../../infra/format-time/format-datetime.js";
export { escapeRegExp } from "../../utils.js";
type EnvelopeTimestampZone = string;
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
const trimmedZone = zone.trim();
const normalized = trimmedZone.toLowerCase();
const weekday = (() => {
try {
if (normalized === "utc" || normalized === "gmt") {
return new Intl.DateTimeFormat("en-US", { timeZone: "UTC", weekday: "short" }).format(date);
}
if (normalized === "local" || normalized === "host") {
return new Intl.DateTimeFormat("en-US", { weekday: "short" }).format(date);
}
return new Intl.DateTimeFormat("en-US", { timeZone: trimmedZone, weekday: "short" }).format(
date,
);
} catch {
return undefined;
}
})();
if (normalized === "utc" || normalized === "gmt") {
const ts = formatUtcTimestamp(date, { displaySeconds: true });
return weekday ? `${weekday} ${ts}` : ts;
}
if (normalized === "local" || normalized === "host") {
const ts =
formatZonedTimestamp(date, { displaySeconds: true }) ??
formatUtcTimestamp(date, { displaySeconds: true });
return weekday ? `${weekday} ${ts}` : ts;
}
const ts =
formatZonedTimestamp(date, { timeZone: trimmedZone, displaySeconds: true }) ??
formatUtcTimestamp(date, { displaySeconds: true });
return weekday ? `${weekday} ${ts}` : ts;
}
export function formatLocalEnvelopeTimestamp(date: Date): string {
return formatEnvelopeTimestamp(date, "local");
}
|