mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-25 02:18:38 +00:00
fix: harden format time fallback handling
This commit is contained in:
@@ -59,36 +59,40 @@ export function formatZonedTimestamp(
|
||||
date: Date,
|
||||
options?: FormatZonedTimestampOptions,
|
||||
): string | undefined {
|
||||
const intlOptions: Intl.DateTimeFormatOptions = {
|
||||
timeZone: options?.timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hourCycle: "h23",
|
||||
timeZoneName: "short",
|
||||
};
|
||||
if (options?.displaySeconds) {
|
||||
intlOptions.second = "2-digit";
|
||||
}
|
||||
const parts = new Intl.DateTimeFormat("en-US", intlOptions).formatToParts(date);
|
||||
const pick = (type: string) => parts.find((part) => part.type === type)?.value;
|
||||
const yyyy = pick("year");
|
||||
const mm = pick("month");
|
||||
const dd = pick("day");
|
||||
const hh = pick("hour");
|
||||
const min = pick("minute");
|
||||
const sec = options?.displaySeconds ? pick("second") : undefined;
|
||||
const tz = [...parts]
|
||||
.toReversed()
|
||||
.find((part) => part.type === "timeZoneName")
|
||||
?.value?.trim();
|
||||
if (!yyyy || !mm || !dd || !hh || !min) {
|
||||
try {
|
||||
const intlOptions: Intl.DateTimeFormatOptions = {
|
||||
timeZone: options?.timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hourCycle: "h23",
|
||||
timeZoneName: "short",
|
||||
};
|
||||
if (options?.displaySeconds) {
|
||||
intlOptions.second = "2-digit";
|
||||
}
|
||||
const parts = new Intl.DateTimeFormat("en-US", intlOptions).formatToParts(date);
|
||||
const pick = (type: string) => parts.find((part) => part.type === type)?.value;
|
||||
const yyyy = pick("year");
|
||||
const mm = pick("month");
|
||||
const dd = pick("day");
|
||||
const hh = pick("hour");
|
||||
const min = pick("minute");
|
||||
const sec = options?.displaySeconds ? pick("second") : undefined;
|
||||
const tz = [...parts]
|
||||
.toReversed()
|
||||
.find((part) => part.type === "timeZoneName")
|
||||
?.value?.trim();
|
||||
if (!yyyy || !mm || !dd || !hh || !min) {
|
||||
return undefined;
|
||||
}
|
||||
if (options?.displaySeconds && sec) {
|
||||
return `${yyyy}-${mm}-${dd} ${hh}:${min}:${sec}${tz ? ` ${tz}` : ""}`;
|
||||
}
|
||||
return `${yyyy}-${mm}-${dd} ${hh}:${min}${tz ? ` ${tz}` : ""}`;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
if (options?.displaySeconds && sec) {
|
||||
return `${yyyy}-${mm}-${dd} ${hh}:${min}:${sec}${tz ? ` ${tz}` : ""}`;
|
||||
}
|
||||
return `${yyyy}-${mm}-${dd} ${hh}:${min}${tz ? ` ${tz}` : ""}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user