109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
|
|
import { loadUiPrefs } from "./i18n/prefs";
|
|||
|
|
import { translate } from "./i18n/messages";
|
|||
|
|
import type { AppLocale } from "./i18n/types";
|
|||
|
|
|
|||
|
|
/** Unix nanoseconds (UTC), matching product contract. */
|
|||
|
|
export function nowUnixNano(): number {
|
|||
|
|
return Date.now() * 1_000_000;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function currentLocale(): AppLocale {
|
|||
|
|
return loadUiPrefs().locale;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function t(key: string, params?: Record<string, string | number>): string {
|
|||
|
|
return translate(currentLocale(), key, params);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function formatLocalDateTime(nano: number | null | undefined): string {
|
|||
|
|
if (!nano) return "—";
|
|||
|
|
const ms = Math.floor(nano / 1_000_000);
|
|||
|
|
const locale = currentLocale() === "en" ? "en-US" : "zh-TW";
|
|||
|
|
return new Date(ms).toLocaleString(locale, {
|
|||
|
|
month: "2-digit",
|
|||
|
|
day: "2-digit",
|
|||
|
|
hour: "2-digit",
|
|||
|
|
minute: "2-digit",
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export type SessionExpiryKind = "unknown" | "ok" | "soon" | "expired";
|
|||
|
|
|
|||
|
|
/** soon = 48h 內到期 */
|
|||
|
|
export function sessionExpiryKind(
|
|||
|
|
expiresAt: number | null | undefined,
|
|||
|
|
now = nowUnixNano(),
|
|||
|
|
): SessionExpiryKind {
|
|||
|
|
if (expiresAt == null || expiresAt <= 0) return "unknown";
|
|||
|
|
if (expiresAt <= now) return "expired";
|
|||
|
|
const soonMs = 48 * 60 * 60 * 1000;
|
|||
|
|
if (expiresAt - now <= soonMs * 1_000_000) return "soon";
|
|||
|
|
return "ok";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function formatRelativeFromNow(
|
|||
|
|
nano: number | null | undefined,
|
|||
|
|
now = nowUnixNano(),
|
|||
|
|
): string {
|
|||
|
|
if (nano == null || nano <= 0) return "—";
|
|||
|
|
const diffMs = Math.floor((nano - now) / 1_000_000);
|
|||
|
|
const abs = Math.abs(diffMs);
|
|||
|
|
const min = Math.round(abs / 60_000);
|
|||
|
|
const hour = Math.round(abs / 3_600_000);
|
|||
|
|
const day = Math.round(abs / 86_400_000);
|
|||
|
|
let span: string;
|
|||
|
|
if (min < 60) span = t("time.min", { n: Math.max(1, min) });
|
|||
|
|
else if (hour < 48) span = t("time.hour", { n: hour });
|
|||
|
|
else span = t("time.day", { n: day });
|
|||
|
|
if (diffMs < 0) return t("time.expired", { span });
|
|||
|
|
return t("time.remaining", { span });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 過去時間:通知列表用(剛剛/N 分前) */
|
|||
|
|
export function formatTimeAgo(nano: number | null | undefined, now = nowUnixNano()): string {
|
|||
|
|
if (nano == null || nano <= 0) return "—";
|
|||
|
|
const diffMs = Math.floor((now - nano) / 1_000_000);
|
|||
|
|
if (diffMs < 45_000) return t("time.justNow");
|
|||
|
|
const min = Math.floor(diffMs / 60_000);
|
|||
|
|
if (min < 60) return t("time.minAgo", { n: min });
|
|||
|
|
const hour = Math.floor(diffMs / 3_600_000);
|
|||
|
|
if (hour < 48) return t("time.hourAgo", { n: hour });
|
|||
|
|
const day = Math.floor(diffMs / 86_400_000);
|
|||
|
|
if (day < 14) return t("time.dayAgo", { n: day });
|
|||
|
|
return formatLocalDateTime(nano);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function formatSessionExpiry(expiresAt: number | null | undefined): {
|
|||
|
|
kind: SessionExpiryKind;
|
|||
|
|
absolute: string;
|
|||
|
|
relative: string;
|
|||
|
|
label: string;
|
|||
|
|
} {
|
|||
|
|
const kind = sessionExpiryKind(expiresAt);
|
|||
|
|
const absolute = formatLocalDateTime(expiresAt);
|
|||
|
|
const relative = formatRelativeFromNow(expiresAt);
|
|||
|
|
const label =
|
|||
|
|
kind === "unknown"
|
|||
|
|
? t("time.sessionUnknown")
|
|||
|
|
: kind === "expired"
|
|||
|
|
? t("time.sessionExpired", { absolute })
|
|||
|
|
: kind === "soon"
|
|||
|
|
? t("time.sessionSoon", { relative, absolute })
|
|||
|
|
: t("time.sessionOk", { relative, absolute });
|
|||
|
|
return { kind, absolute, relative, label };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function fromDatetimeLocalValue(value: string): number {
|
|||
|
|
if (!value) return nowUnixNano();
|
|||
|
|
const ms = new Date(value).getTime();
|
|||
|
|
if (Number.isNaN(ms)) return nowUnixNano();
|
|||
|
|
return ms * 1_000_000;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export function toDatetimeLocalValue(nano: number | null | undefined): string {
|
|||
|
|
if (!nano) return "";
|
|||
|
|
const d = new Date(Math.floor(nano / 1_000_000));
|
|||
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|||
|
|
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|||
|
|
}
|