mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-11-14 06:15:15 +00:00
Fixes #4844 with more defensive date parsing
All checks were successful
Close stale issues and PRs / stale (push) Successful in 23s
All checks were successful
Close stale issues and PRs / stale (push) Successful in 23s
This commit is contained in:
42
frontend/src/locale/Utils.ts
Normal file
42
frontend/src/locale/Utils.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { fromUnixTime, intlFormat, parseISO } from "date-fns";
|
||||
|
||||
const isUnixTimestamp = (value: unknown): boolean => {
|
||||
if (typeof value !== "number" && typeof value !== "string") return false;
|
||||
const num = Number(value);
|
||||
if (!Number.isFinite(num)) return false;
|
||||
// Check plausible Unix timestamp range: from 1970 to ~year 3000
|
||||
// Support both seconds and milliseconds
|
||||
if (num > 0 && num < 10000000000) return true; // seconds (<= 10 digits)
|
||||
if (num >= 10000000000 && num < 32503680000000) return true; // milliseconds (<= 13 digits)
|
||||
return false;
|
||||
};
|
||||
|
||||
const parseDate = (value: string | number): Date | null => {
|
||||
if (typeof value !== "number" && typeof value !== "string") return null;
|
||||
try {
|
||||
return isUnixTimestamp(value) ? fromUnixTime(+value) : parseISO(`${value}`);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const formatDateTime = (value: string | number): string => {
|
||||
const d = parseDate(value);
|
||||
if (!d) return `${value}`;
|
||||
try {
|
||||
return intlFormat(d, {
|
||||
weekday: "long",
|
||||
year: "numeric",
|
||||
month: "numeric",
|
||||
day: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
second: "numeric",
|
||||
hour12: true,
|
||||
});
|
||||
} catch {
|
||||
return `${value}`;
|
||||
}
|
||||
};
|
||||
|
||||
export { formatDateTime, parseDate, isUnixTimestamp };
|
||||
Reference in New Issue
Block a user