From 4e1a100b2a29c36a3559f8563bf1d1974a1c8be5 Mon Sep 17 00:00:00 2001 From: Horux Dev Date: Thu, 30 Apr 2026 04:31:40 +0000 Subject: [PATCH] fix(sat-client): format dates in Mexico City timezone for SAT formatDateForSat now uses Intl.DateTimeFormat with America/Mexico_City timezone instead of UTC local time. Fixes 'Fecha final invalida' errors when server runs in UTC and SAT interprets dates as Mexico local time. --- .../src/services/sat/sat-client.service.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/apps/api/src/services/sat/sat-client.service.ts b/apps/api/src/services/sat/sat-client.service.ts index 01307b4..fe15c54 100644 --- a/apps/api/src/services/sat/sat-client.service.ts +++ b/apps/api/src/services/sat/sat-client.service.ts @@ -239,10 +239,23 @@ export async function downloadSatPackage( } /** - * Formatea una fecha para el SAT (YYYY-MM-DD HH:mm:ss) + * Formatea una fecha para el SAT (YYYY-MM-DD HH:mm:ss) en hora de México. + * El SAT opera en zona horaria America/Mexico_City (UTC-6 estándar). */ function formatDateForSat(date: Date): string { - const pad = (n: number) => n.toString().padStart(2, '0'); - return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` + - `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`; + const pad = (n: string) => n.padStart(2, '0'); + const parts = new Intl.DateTimeFormat('en-US', { + timeZone: 'America/Mexico_City', + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false, + }).formatToParts(date); + + const get = (type: string) => pad(parts.find(p => p.type === type)?.value || '00'); + + return `${get('year')}-${get('month')}-${get('day')} ${get('hour')}:${get('minute')}:${get('second')}`; }