fix: ISO countdown date, IntlDateFormatter and UTF-8 truncation
This commit is contained in:
+50
-1
@@ -2,9 +2,58 @@
|
||||
* Script de compte à rebours pour la page de maintenance
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convertit une date ISO 8601 en timestamp, compatible Safari.
|
||||
*
|
||||
* Safari renvoie Invalid Date pour les formats non strictement ISO
|
||||
* (ex. « 2025-10-11 00:00:00 » émis historiquement par PHP) : la chaîne
|
||||
* est donc analysée manuellement. Sans fuseau explicite, la date est
|
||||
* interprétée en UTC — PHP émet de toute façon toujours un décalage.
|
||||
*
|
||||
* @param {string|number} value Date ISO 8601 (ou timestamp déjà numérique)
|
||||
* @returns {number} Timestamp en millisecondes
|
||||
*/
|
||||
function parseTargetDate(value) {
|
||||
if (typeof value === 'number') {
|
||||
return new Date(value).getTime();
|
||||
}
|
||||
|
||||
const match = String(value).match(
|
||||
/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?(?:\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/
|
||||
);
|
||||
|
||||
if (!match) {
|
||||
// Repli sur l'analyse native pour les formats inattendus
|
||||
return new Date(value).getTime();
|
||||
}
|
||||
|
||||
const [, year, month, day, hours = '0', minutes = '0', seconds = '0', offset] = match;
|
||||
|
||||
let timestamp = Date.UTC(
|
||||
parseInt(year, 10),
|
||||
parseInt(month, 10) - 1,
|
||||
parseInt(day, 10),
|
||||
parseInt(hours, 10),
|
||||
parseInt(minutes, 10),
|
||||
parseInt(seconds, 10)
|
||||
);
|
||||
|
||||
if (offset && offset !== 'Z') {
|
||||
// Un décalage « +02:00 » signifie « 2 h d'avance sur UTC » : on le soustrait
|
||||
const sign = offset[0] === '+' ? 1 : -1;
|
||||
const offsetDigits = offset.slice(1).replace(':', '');
|
||||
const offsetMinutes = sign * (
|
||||
parseInt(offsetDigits.slice(0, 2), 10) * 60 + parseInt(offsetDigits.slice(2, 4), 10)
|
||||
);
|
||||
timestamp -= offsetMinutes * 60 * 1000;
|
||||
}
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
class CountdownTimer {
|
||||
constructor(targetDate) {
|
||||
this.targetDate = new Date(targetDate).getTime();
|
||||
this.targetDate = parseTargetDate(targetDate);
|
||||
this.elements = {
|
||||
days: document.getElementById('countdown-days'),
|
||||
hours: document.getElementById('countdown-hours'),
|
||||
|
||||
Reference in New Issue
Block a user