feat: Standardize audit log timestamps to UTC in the backend and display local time in the frontend.

This commit is contained in:
Galindo97
2026-02-11 11:13:32 -06:00
parent a594676de7
commit dfd9c06b06
2 changed files with 15 additions and 10 deletions

View File

@@ -40,9 +40,9 @@ class AuditService:
""" """
Low-level creation of an Audit Log entry Low-level creation of an Audit Log entry
""" """
# Timezone handling set to Hermosillo (Sonora) to match user preference (-1h vs CDMX) # Timezone handling: Use UTC for consistency across regions.
tz = pytz.timezone('America/Hermosillo') # Frontend will convert to user's local time.
now = datetime.now(tz) now = datetime.now(pytz.UTC)
log = AuditLog( log = AuditLog(
reference=reference, reference=reference,
@@ -157,9 +157,8 @@ class AuditService:
# Prevent duplicate login logs (debounce 5 seconds) # Prevent duplicate login logs (debounce 5 seconds)
# This handles cases where frontend might submit twice or redirects trigger re-auth # This handles cases where frontend might submit twice or redirects trigger re-auth
try: try:
# Timezone handling set to Hermosillo (Sonora) to match user preference (-1h vs CDMX) # Timezone handling: Use UTC for consistency
tz = pytz.timezone('America/Hermosillo') now = datetime.now(pytz.UTC)
now = datetime.now(tz)
five_seconds_ago = now - datetime.timedelta(seconds=5) five_seconds_ago = now - datetime.timedelta(seconds=5)
# Check for recent login from same user # Check for recent login from same user

View File

@@ -114,13 +114,19 @@
handleFilterChange(); handleFilterChange();
} }
function formatDate(dateStr: string): string { function formatDate(dateStr: string, timestamp?: string): string {
if (timestamp) {
return new Date(timestamp).toLocaleDateString();
}
if (!dateStr) return ''; if (!dateStr) return '';
const [y, m, d] = dateStr.split('-'); const [y, m, d] = dateStr.split('-');
return `${d}/${m}/${y}`; 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 ''; if (!timeStr) return '';
try { try {
const [h, m] = timeStr.split(':'); const [h, m] = timeStr.split(':');
@@ -316,8 +322,8 @@
<Table.Cell class="text-sm">{log.procedure}</Table.Cell> <Table.Cell class="text-sm">{log.procedure}</Table.Cell>
<Table.Cell class="text-sm">{log.movement}</Table.Cell> <Table.Cell class="text-sm">{log.movement}</Table.Cell>
<Table.Cell class="text-sm">{log.username}</Table.Cell> <Table.Cell class="text-sm">{log.username}</Table.Cell>
<Table.Cell class="text-sm">{formatDate(log.date)}</Table.Cell> <Table.Cell class="text-sm">{formatDate(log.date, log.timestamp)}</Table.Cell>
<Table.Cell class="text-sm">{formatTime(log.time)}</Table.Cell> <Table.Cell class="text-sm">{formatTime(log.time, log.timestamp)}</Table.Cell>
</Table.Row> </Table.Row>
{/each} {/each}
{/if} {/if}