49 lines · 1.4 KB · TypeScript
RawDownload
import { type TimestampFormat } from "./appSettings"; export function getTimestampFormatOptions(  timestampFormat: TimestampFormat,  includeSeconds: boolean,): Intl.DateTimeFormatOptions {  const baseOptions: Intl.DateTimeFormatOptions = {    hour: "numeric",    minute: "2-digit",    ...(includeSeconds ? { second: "2-digit" } : {}),  };   if (timestampFormat === "locale") {    return baseOptions;  }   return {    ...baseOptions,    hour12: timestampFormat === "12-hour",  };} const timestampFormatterCache = new Map<string, Intl.DateTimeFormat>(); function getTimestampFormatter(  timestampFormat: TimestampFormat,  includeSeconds: boolean,): Intl.DateTimeFormat {  const cacheKey = `${timestampFormat}:${includeSeconds ? "seconds" : "minutes"}`;  const cachedFormatter = timestampFormatterCache.get(cacheKey);  if (cachedFormatter) {    return cachedFormatter;  }   const formatter = new Intl.DateTimeFormat(    undefined,    getTimestampFormatOptions(timestampFormat, includeSeconds),  );  timestampFormatterCache.set(cacheKey, formatter);  return formatter;} export function formatTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {  return getTimestampFormatter(timestampFormat, true).format(new Date(isoDate));} export function formatShortTimestamp(isoDate: string, timestampFormat: TimestampFormat): string {  return getTimestampFormatter(timestampFormat, false).format(new Date(isoDate));}