Correction de l'heure affectée aux nouveaux posts
This commit is contained in:
@@ -1,34 +1,107 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const YAML = require("yaml");
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
const HUGO_CONFIG_PATH = path.join(process.cwd(), "config", "_default", "config.yaml");
|
||||
|
||||
let cachedTimeZone = null;
|
||||
|
||||
/**
|
||||
* Ajoute un zéro devant les valeurs inférieures à 10 pour conserver le formatage.
|
||||
* @param {number|string} value Valeur numérique à normaliser.
|
||||
* @returns {string} Représentation sur deux caractères.
|
||||
* Récupère le fuseau horaire configuré pour Hugo.
|
||||
* @returns {string} Identifiant IANA du fuseau horaire de Hugo.
|
||||
*/
|
||||
function pad(value) {
|
||||
return String(value).padStart(2, '0');
|
||||
function getHugoTimeZone() {
|
||||
if (cachedTimeZone) {
|
||||
return cachedTimeZone;
|
||||
}
|
||||
|
||||
const rawConfig = fs.readFileSync(HUGO_CONFIG_PATH, "utf8");
|
||||
const parsedConfig = YAML.parse(rawConfig);
|
||||
|
||||
if (!parsedConfig || !parsedConfig.timeZone) {
|
||||
throw new Error("Aucun fuseau horaire Hugo n'a été trouvé dans config/_default/config.yaml.");
|
||||
}
|
||||
|
||||
cachedTimeZone = String(parsedConfig.timeZone).trim();
|
||||
|
||||
if (!cachedTimeZone) {
|
||||
throw new Error("Le fuseau horaire Hugo est vide ou invalide.");
|
||||
}
|
||||
|
||||
return cachedTimeZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le timestamp ISO local formatté avec offset.
|
||||
* @param {Date} value Date à formater (par défaut : maintenant).
|
||||
* @returns {string} Timestamp ISO 8601 avec fuseau horaire local.
|
||||
* Convertit une valeur vers un DateTime positionné sur le fuseau horaire Hugo.
|
||||
* @param {Date|import("luxon").DateTime|string|number|null} value Valeur à convertir (null : maintenant).
|
||||
* @returns {import("luxon").DateTime} Instance DateTime alignée sur le fuseau de Hugo.
|
||||
*/
|
||||
function formatDateTime(value = new Date()) {
|
||||
const year = value.getFullYear();
|
||||
const month = pad(value.getMonth() + 1);
|
||||
const day = pad(value.getDate());
|
||||
const hours = pad(value.getHours());
|
||||
const minutes = pad(value.getMinutes());
|
||||
const seconds = pad(value.getSeconds());
|
||||
function toHugoDateTime(value = null) {
|
||||
const zone = getHugoTimeZone();
|
||||
|
||||
const offsetMinutes = value.getTimezoneOffset();
|
||||
const sign = offsetMinutes <= 0 ? '+' : '-';
|
||||
const absOffset = Math.abs(offsetMinutes);
|
||||
const offsetHours = pad(Math.floor(absOffset / 60));
|
||||
const offsetMins = pad(absOffset % 60);
|
||||
if (value === null || value === undefined) {
|
||||
const now = DateTime.now().setZone(zone);
|
||||
if (!now.isValid) {
|
||||
throw new Error(now.invalidReason || "Date actuelle invalide pour le fuseau Hugo.");
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${sign}${offsetHours}:${offsetMins}`;
|
||||
if (DateTime.isDateTime(value)) {
|
||||
const zoned = value.setZone(zone);
|
||||
if (!zoned.isValid) {
|
||||
throw new Error(zoned.invalidReason || "DateTime invalide après application du fuseau Hugo.");
|
||||
}
|
||||
return zoned;
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
const zoned = DateTime.fromJSDate(value, { zone });
|
||||
if (!zoned.isValid) {
|
||||
throw new Error(zoned.invalidReason || "Date JS invalide pour le fuseau Hugo.");
|
||||
}
|
||||
return zoned;
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
const parsed = DateTime.fromISO(value, { setZone: true }).setZone(zone);
|
||||
if (!parsed.isValid) {
|
||||
throw new Error(parsed.invalidReason || `Chaîne de date invalide : ${value}`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
if (typeof value === "number") {
|
||||
const parsed = DateTime.fromMillis(value, { zone }).setZone(zone);
|
||||
if (!parsed.isValid) {
|
||||
throw new Error(parsed.invalidReason || "Horodatage numérique invalide pour le fuseau Hugo.");
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
throw new Error("Type de date non pris en charge pour le fuseau horaire Hugo.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Formate une date en ISO 8601 avec l'offset du fuseau horaire Hugo.
|
||||
* @param {Date|import("luxon").DateTime|string|number|null} value Valeur à formater.
|
||||
* @returns {string} Timestamp ISO 8601 avec offset.
|
||||
*/
|
||||
function formatDateTime(value = null) {
|
||||
const zoned = toHugoDateTime(value);
|
||||
const normalized = zoned.set({ millisecond: 0 });
|
||||
const formatted = normalized.toISO({ suppressMilliseconds: true });
|
||||
|
||||
if (!formatted) {
|
||||
throw new Error("Impossible de formater la date avec le fuseau Hugo.");
|
||||
}
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatDateTime,
|
||||
formatDateTime,
|
||||
getHugoTimeZone,
|
||||
toHugoDateTime,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user