@cryptodevops/n8n-nodes-santiment
Version:
n8n node for Santiment cryptocurrency API
48 lines (47 loc) • 1.7 kB
JavaScript
;
/**
* Utilitaires de date pour le nœud Santiment
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToSantimentFormat = convertToSantimentFormat;
/**
* Convertit une date au format YYYY-MM-DD vers le format ISO accepté par Santiment
* @param {string} dateString - Date au format YYYY-MM-DD ou autre format
* @param {boolean} isEndDate - Si true, ajoute 23:59:59 pour la date de fin
* @returns {string} Date au format ISO avec Z
*/
function convertToSantimentFormat(dateString, isEndDate = false) {
// Vérifier si c'est déjà un format ISO
if (dateString.includes('T') && (dateString.includes('Z') || dateString.includes('+'))) {
return dateString;
}
// Vérifier si c'est un format relatif
if (dateString.startsWith('utc_now')) {
return dateString;
}
// Convertir YYYY-MM-DD vers ISO
if (dateString.match(/^\d{4}-\d{2}-\d{2}$/)) {
const time = isEndDate ? 'T23:59:59Z' : 'T00:00:00Z';
return dateString + time;
}
// Si c'est déjà un autre format, essayer de le parser
try {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
throw new Error(`Format de date invalide: ${dateString}`);
}
if (isEndDate) {
date.setHours(23, 59, 59, 999);
}
else {
date.setHours(0, 0, 0, 0);
}
// Retourner au format ISO avec Z
return date.toISOString().replace(/\.\d{3}Z$/, 'Z');
}
catch (error) {
// En cas d'erreur, retourner la date originale
console.error(`Erreur de conversion de date: ${error}`);
return dateString;
}
}