From dfd9c06b06d927a37de8592bcdebf70d802d2e39 Mon Sep 17 00:00:00 2001 From: Galindo97 Date: Wed, 11 Feb 2026 11:13:32 -0600 Subject: [PATCH] feat: Standardize audit log timestamps to UTC in the backend and display local time in the frontend. --- .../v1/modules/a76/audit_log/services/service.py | 11 +++++------ .../src/routes/dashboard/audit_logs/+page.svelte | 14 ++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/backend/api/v1/modules/a76/audit_log/services/service.py b/backend/api/v1/modules/a76/audit_log/services/service.py index 8da524fa..b8d3099a 100644 --- a/backend/api/v1/modules/a76/audit_log/services/service.py +++ b/backend/api/v1/modules/a76/audit_log/services/service.py @@ -40,9 +40,9 @@ class AuditService: """ Low-level creation of an Audit Log entry """ - # Timezone handling set to Hermosillo (Sonora) to match user preference (-1h vs CDMX) - tz = pytz.timezone('America/Hermosillo') - now = datetime.now(tz) + # Timezone handling: Use UTC for consistency across regions. + # Frontend will convert to user's local time. + now = datetime.now(pytz.UTC) log = AuditLog( reference=reference, @@ -157,9 +157,8 @@ class AuditService: # Prevent duplicate login logs (debounce 5 seconds) # This handles cases where frontend might submit twice or redirects trigger re-auth try: - # Timezone handling set to Hermosillo (Sonora) to match user preference (-1h vs CDMX) - tz = pytz.timezone('America/Hermosillo') - now = datetime.now(tz) + # Timezone handling: Use UTC for consistency + now = datetime.now(pytz.UTC) five_seconds_ago = now - datetime.timedelta(seconds=5) # Check for recent login from same user diff --git a/frontend/src/routes/dashboard/audit_logs/+page.svelte b/frontend/src/routes/dashboard/audit_logs/+page.svelte index 707f8563..8968b41b 100644 --- a/frontend/src/routes/dashboard/audit_logs/+page.svelte +++ b/frontend/src/routes/dashboard/audit_logs/+page.svelte @@ -114,13 +114,19 @@ handleFilterChange(); } - function formatDate(dateStr: string): string { + function formatDate(dateStr: string, timestamp?: string): string { + if (timestamp) { + return new Date(timestamp).toLocaleDateString(); + } if (!dateStr) return ''; const [y, m, d] = dateStr.split('-'); return `${d}/${m}/${y}`; } - function formatTime(timeStr: string): string { + function formatTime(timeStr: string, timestamp?: string): string { + if (timestamp) { + return new Date(timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); + } if (!timeStr) return ''; try { const [h, m] = timeStr.split(':'); @@ -316,8 +322,8 @@ {log.procedure} {log.movement} {log.username} - {formatDate(log.date)} - {formatTime(log.time)} + {formatDate(log.date, log.timestamp)} + {formatTime(log.time, log.timestamp)} {/each} {/if}