From 87211c9cc3f530dc0eecf8893db7f90f91bf6e93 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Mon, 1 Dec 2025 01:15:24 +0100 Subject: [PATCH] =?UTF-8?q?Date=20et=20heure=20compl=C3=A8te=20=C3=A0=20la?= =?UTF-8?q?=20cr=C3=A9ation=20d'un=20fichier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/add_lego.js | 6 +++--- tools/add_link.js | 5 +++-- tools/lib/datetime.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tools/lib/datetime.js diff --git a/tools/add_lego.js b/tools/add_lego.js index 26e2a9ee..d1032f40 100644 --- a/tools/add_lego.js +++ b/tools/add_lego.js @@ -22,6 +22,7 @@ const path = require('path'); const https = require('https'); const readline = require('readline'); const { loadToolsConfig } = require('./lib/config'); +const { formatDateTime } = require('./lib/datetime'); const ROOT = process.cwd(); const LEGO_ROOT = path.join(ROOT, 'content', 'collections', 'lego'); @@ -366,8 +367,7 @@ async function main() { const indexExists = fsSync.existsSync(indexPath); if (!indexExists) { const pageTitle = setDetails.name || selected.name; - const today = new Date(); - const date = today.toISOString().slice(0, 10); + const createdAt = formatDateTime(); // Collect images present in imagesDir (existing or just downloaded) let imageFiles = []; @@ -417,7 +417,7 @@ async function main() { const coverLine = coverFile ? `cover: images/${coverFile}\n` : ''; const md = `---\n` + `title: "${pageTitle.replace(/"/g, '\\"')}"\n` + - `date: ${date}\n` + + `date: ${createdAt}\n` + coverLine + `---\n\n` + body; diff --git a/tools/add_link.js b/tools/add_link.js index 2013faf2..9dd498b1 100644 --- a/tools/add_link.js +++ b/tools/add_link.js @@ -9,6 +9,7 @@ const YAML = require("yaml"); const { buildUserAgent, checkUrl } = require("./lib/http"); const { getArchiveUrl, saveToArchive } = require("./lib/archive"); const { scrapePage } = require("./lib/puppeteer"); +const { formatDateTime } = require("./lib/datetime"); const LINKS_ROOT = path.join("content", "interets", "liens-interessants"); @@ -81,7 +82,7 @@ if (duplicateBundlePath) { console.log(`📂 Archive URL ${archiveUrl}...`); - // Determine the entry date + // Déterminer la date et l'heure d'enregistrement let entryDate = customDate ? new Date(customDate) : new Date(); if (isNaN(entryDate.getTime())) { console.error("❌ Invalid date format. Use YYYY-MM-DD."); @@ -89,7 +90,7 @@ if (duplicateBundlePath) { } const now = new Date(); // Current date for status - const formattedEntryDate = entryDate.toISOString().split("T")[0]; // YYYY-MM-DD + const formattedEntryDate = formatDateTime(entryDate); // ISO 8601 local avec offset const formattedStatusDate = now.toISOString(); // ISO format const formattedDateFrench = entryDate.toLocaleDateString("fr-FR", { year: "numeric", diff --git a/tools/lib/datetime.js b/tools/lib/datetime.js new file mode 100644 index 00000000..3ed8d7d3 --- /dev/null +++ b/tools/lib/datetime.js @@ -0,0 +1,34 @@ +/** + * 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. + */ +function pad(value) { + return String(value).padStart(2, '0'); +} + +/** + * 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. + */ +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()); + + 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); + + return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${sign}${offsetHours}:${offsetMins}`; +} + +module.exports = { + formatDateTime, +};