refactor: 优化管理端配置页紧凑布局与时间展示
This commit is contained in:
@@ -1,7 +1,26 @@
|
||||
import type { AdminApiLocale } from "@/lib/admin-locale";
|
||||
|
||||
function pad2(n: number): string {
|
||||
return String(n).padStart(2, "0");
|
||||
function formatParts(date: Date, timeZone?: string): string {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hourCycle: "h23",
|
||||
}).formatToParts(date);
|
||||
|
||||
const map = new Map(parts.map((part) => [part.type, part.value]));
|
||||
const year = map.get("year") ?? "0000";
|
||||
const month = map.get("month") ?? "00";
|
||||
const day = map.get("day") ?? "00";
|
||||
const hour = map.get("hour") ?? "00";
|
||||
const minute = map.get("minute") ?? "00";
|
||||
const second = map.get("second") ?? "00";
|
||||
|
||||
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,12 +40,30 @@ export function formatAdminInstant(
|
||||
if (Number.isNaN(ms)) {
|
||||
return "—";
|
||||
}
|
||||
const date = new Date(ms);
|
||||
const y = date.getFullYear();
|
||||
const m = pad2(date.getMonth() + 1);
|
||||
const d = pad2(date.getDate());
|
||||
const h = pad2(date.getHours());
|
||||
const min = pad2(date.getMinutes());
|
||||
const s = pad2(date.getSeconds());
|
||||
return `${y}-${m}-${d} ${h}:${min}:${s}`;
|
||||
return formatParts(new Date(ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将接口返回的 ISO 时间串格式化到指定时区,便于并列展示多个地区的时间。
|
||||
*/
|
||||
export function formatAdminInstantInTimeZone(
|
||||
iso: string | null | undefined,
|
||||
options: {
|
||||
locale: AdminApiLocale;
|
||||
timeZone: string;
|
||||
},
|
||||
): string {
|
||||
void options.locale;
|
||||
if (iso == null || iso === "") {
|
||||
return "—";
|
||||
}
|
||||
const ms = Date.parse(iso);
|
||||
if (Number.isNaN(ms)) {
|
||||
return "—";
|
||||
}
|
||||
try {
|
||||
return formatParts(new Date(ms), options.timeZone);
|
||||
} catch {
|
||||
return formatParts(new Date(ms));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user