Enhance date formatting in UptimeStatusIsland and ensure UTC compliance in API
- Improved the formatLocalTime function to handle various date formats and ensure accurate timezone conversion. - Updated the ensureUTC function to check for ISO format and append 'Z' for UTC interpretation when necessary.
This commit is contained in:
@@ -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 (
|
||||
<div style={{ minWidth: 220 }}>
|
||||
<div style={{ display: 'flex', alignItems: 'center', marginBottom: 4 }}>
|
||||
|
@@ -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 () => {
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user