codex-proxy / shared /utils /format.ts
icebear0828
refactor: extract startServer() + path abstraction for Electron reuse
4a940a5
raw
history blame
1.46 kB
export function formatNumber(n: number): string {
if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + "M";
if (n >= 1_000) return (n / 1_000).toFixed(1) + "k";
return String(n);
}
export function formatWindowDuration(seconds: number, isZh: boolean): string {
if (seconds >= 86400) {
const days = Math.floor(seconds / 86400);
return isZh ? `${days}\u5929` : `${days}d`;
}
if (seconds >= 3600) {
const hours = Math.floor(seconds / 3600);
return isZh ? `${hours}\u5c0f\u65f6` : `${hours}h`;
}
const minutes = Math.floor(seconds / 60);
return isZh ? `${minutes}\u5206\u949f` : `${minutes}m`;
}
export function formatResetTime(unixSec: number, isZh: boolean): string {
const d = new Date(unixSec * 1000);
const now = new Date();
const time = d.toLocaleTimeString(undefined, {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
if (
d.getFullYear() === now.getFullYear() &&
d.getMonth() === now.getMonth() &&
d.getDate() === now.getDate()
) {
return time;
}
const tomorrow = new Date(now);
tomorrow.setDate(tomorrow.getDate() + 1);
if (
d.getFullYear() === tomorrow.getFullYear() &&
d.getMonth() === tomorrow.getMonth() &&
d.getDate() === tomorrow.getDate()
) {
return (isZh ? "\u660e\u5929 " : "Tomorrow ") + time;
}
const date = d.toLocaleDateString(undefined, {
month: "short",
day: "numeric",
});
return date + " " + time;
}