mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
FE more defensive localizeTimestamp #1147
This commit is contained in:
@@ -368,41 +368,39 @@ function getLangCode() {
|
|||||||
function localizeTimestamp(input) {
|
function localizeTimestamp(input) {
|
||||||
let tz = getSetting("TIMEZONE") || 'Europe/Berlin';
|
let tz = getSetting("TIMEZONE") || 'Europe/Berlin';
|
||||||
|
|
||||||
// Convert to string and trim
|
|
||||||
input = String(input || '').trim();
|
input = String(input || '').trim();
|
||||||
|
|
||||||
// Normalize multiple spaces and remove commas
|
|
||||||
const cleaned = input.replace(',', ' ').replace(/\s+/g, ' ');
|
const cleaned = input.replace(',', ' ').replace(/\s+/g, ' ');
|
||||||
|
|
||||||
// DD/MM/YYYY format check
|
|
||||||
const dateTimeParts = cleaned.split(' ');
|
const dateTimeParts = cleaned.split(' ');
|
||||||
if (dateTimeParts.length >= 2 && dateTimeParts[0].includes('/')) {
|
|
||||||
|
// ✅ Strict DD/MM/YYYY check
|
||||||
|
if (dateTimeParts.length >= 2 && /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateTimeParts[0])) {
|
||||||
const [day, month, year] = dateTimeParts[0].split('/');
|
const [day, month, year] = dateTimeParts[0].split('/');
|
||||||
const timePart = dateTimeParts[1];
|
const timePart = dateTimeParts[1] || "00:00:00";
|
||||||
|
|
||||||
if (day && month && year && timePart) {
|
// strip timezone offsets from timePart if present
|
||||||
const isoString = `${year}-${month}-${day}T${timePart.length === 5 ? timePart + ':00' : timePart}`;
|
const timeClean = timePart.replace(/([+-]\d{2}:?\d{2})$/, '');
|
||||||
const date = new Date(isoString);
|
const isoString = `${year}-${month.padStart(2,'0')}-${day.padStart(2,'0')}T${timeClean.length === 5 ? timeClean + ':00' : timeClean}`;
|
||||||
if (!isFinite(date)) return 'b-';
|
|
||||||
|
|
||||||
return new Intl.DateTimeFormat('default', {
|
const date = new Date(isoString);
|
||||||
timeZone: tz,
|
if (!isFinite(date)) return 'b-';
|
||||||
year: 'numeric',
|
|
||||||
month: '2-digit',
|
return new Intl.DateTimeFormat('default', {
|
||||||
day: '2-digit',
|
timeZone: tz,
|
||||||
hour: '2-digit',
|
year: 'numeric',
|
||||||
minute: '2-digit',
|
month: '2-digit',
|
||||||
second: '2-digit',
|
day: '2-digit',
|
||||||
hour12: false
|
hour: '2-digit',
|
||||||
}).format(date);
|
minute: '2-digit',
|
||||||
}
|
second: '2-digit',
|
||||||
|
hour12: false
|
||||||
|
}).format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ISO style YYYY-MM-DD HH:mm(:ss)?
|
// ✅ ISO style YYYY-MM-DD HH:mm:ss with optional timezone
|
||||||
const match = cleaned.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2})(:\d{2})?$/);
|
const match = cleaned.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2})(:\d{2})?([+-]\d{2}:?\d{2})?$/);
|
||||||
if (match) {
|
if (match) {
|
||||||
let iso = `${match[1]}T${match[2]}${match[3] || ':00'}`;
|
let iso = `${match[1]}T${match[2]}${match[3] || ':00'}${match[4] || ''}`;
|
||||||
|
|
||||||
const date = new Date(iso);
|
const date = new Date(iso);
|
||||||
if (!isFinite(date)) return 'c-';
|
if (!isFinite(date)) return 'c-';
|
||||||
|
|
||||||
@@ -418,7 +416,7 @@ function localizeTimestamp(input) {
|
|||||||
}).format(date);
|
}).format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback: try to parse any other string input
|
// ✅ Fallback
|
||||||
const date = new Date(input);
|
const date = new Date(input);
|
||||||
if (!isFinite(date)) return 'Failed conversion: ' + input;
|
if (!isFinite(date)) return 'Failed conversion: ' + input;
|
||||||
|
|
||||||
@@ -435,7 +433,6 @@ function localizeTimestamp(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------
|
// ----------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Replaces double quotes within single-quoted strings, then converts all single quotes to double quotes,
|
* Replaces double quotes within single-quoted strings, then converts all single quotes to double quotes,
|
||||||
|
|||||||
Reference in New Issue
Block a user