diff --git a/src/components/UptimeStatusIsland.jsx b/src/components/UptimeStatusIsland.jsx index b1b4209..5a78e80 100644 --- a/src/components/UptimeStatusIsland.jsx +++ b/src/components/UptimeStatusIsland.jsx @@ -47,16 +47,73 @@ function getUptime24hBg(uptime) { function formatLocalTime(rawTime, zone = 'utc') { if (!rawTime) return ''; - const dt = DateTime.fromFormat(rawTime, 'yyyy-MM-dd HH:mm:ss.SSS', { zone: 'utc' }); - const localDt = dt.isValid ? dt.setZone(zone) : null; - return localDt && localDt.isValid - ? localDt.toFormat('dd-MM-yyyy, HH:mm:ss') - : 'Invalid DateTime'; + + let dt = null; + + // First try ISO parsing (most reliable for UTC timestamps) + try { + dt = DateTime.fromISO(rawTime, { zone: 'utc' }); + if (dt.isValid) { + // If target zone is UTC, return as-is + if (zone === 'utc') { + return dt.toFormat('dd-MM-yyyy, HH:mm:ss'); + } + // Otherwise convert to target timezone + const converted = dt.setZone(zone); + return converted.isValid ? converted.toFormat('dd-MM-yyyy, HH:mm:ss') : 'Invalid DateTime'; + } + } catch (e) { + // Continue to format parsing if ISO fails + } + + // Try various formats, always assuming incoming time is UTC + const formats = [ + 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'', + 'yyyy-MM-dd\'T\'HH:mm:ss\'Z\'', + 'yyyy-MM-dd\'T\'HH:mm:ss.SSS', + 'yyyy-MM-dd\'T\'HH:mm:ss', + 'yyyy-MM-dd HH:mm:ss.SSS', + 'yyyy-MM-dd HH:mm:ss' + ]; + + for (const format of formats) { + try { + dt = DateTime.fromFormat(rawTime, format, { zone: 'utc' }); + if (dt.isValid) break; + } catch (e) { + continue; + } + } + + // Last resort: try JavaScript Date parsing + if (!dt || !dt.isValid) { + try { + const jsDate = new Date(rawTime); + if (!isNaN(jsDate.getTime())) { + dt = DateTime.fromJSDate(jsDate, { zone: 'utc' }); + } + } catch (e) { + // Give up + } + } + + if (!dt || !dt.isValid) { + return 'Invalid DateTime'; + } + + // Convert to target timezone + if (zone === 'utc') { + return dt.toFormat('dd-MM-yyyy, HH:mm:ss'); + } + + const targetDt = dt.setZone(zone); + return targetDt.isValid ? targetDt.toFormat('dd-MM-yyyy, HH:mm:ss') : 'Invalid DateTime'; } function HeartbeatPopup({ hb, userZone, monitor }) { const localTime = hb ? formatLocalTime(hb.time, userZone) : ''; const utcTime = hb ? formatLocalTime(hb.time, 'utc') : ''; + return (
diff --git a/src/pages/api/uptime.ts b/src/pages/api/uptime.ts index 4118cc5..c590dc7 100644 --- a/src/pages/api/uptime.ts +++ b/src/pages/api/uptime.ts @@ -58,7 +58,13 @@ const fetchWithTimeout = async (url: string, options: RequestInit, timeout = 100 // Helper function to ensure a date string is in UTC ISO format function ensureUTC(dateString: string): string { - const date = new Date(dateString); + // If it's already in ISO format with Z, return as-is + if (dateString.endsWith('Z') || dateString.includes('+') || dateString.includes('T') && dateString.includes(':')) { + return dateString; + } + + // If it's a simple date string, force UTC interpretation + const date = new Date(dateString + 'Z'); return date.toISOString(); } @@ -163,4 +169,4 @@ export const GET: APIRoute = async () => { }, }); } -}; \ No newline at end of file +}; \ No newline at end of file