Refactor: Extract UTC parsing logic to parseUTCDate helper function

- Add parseUTCDate helper function to handle timestamp UTC parsing consistently
- Eliminates code duplication across getTimeAgo, getTimeRemaining, and expiry functions
- Applied to admin.js, app-mapbox.js, app-google.js, and app.js
- Ensures consistent UTC timezone handling throughout all frontend JavaScript
- Addresses Copilot AI feedback for better code maintainability and DRY principles

The parseUTCDate function handles the logic:
timestamp.includes('T') ? timestamp : timestamp + 'Z'

This ensures all timestamp parsing uses the same UTC interpretation logic.
This commit is contained in:
Deco Vander 2025-07-04 13:14:27 -04:00
parent 544766e5dc
commit 45a8d67362
4 changed files with 28 additions and 6 deletions

View file

@ -93,10 +93,15 @@ document.addEventListener('DOMContentLoaded', async () => {
});
};
// Helper function to parse UTC date
const parseUTCDate = (timestamp) => {
return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z');
};
const getTimeAgo = (timestamp) => {
const now = new Date();
// Ensure timestamp is treated as UTC if it doesn't have timezone info
const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z');
const reportTime = parseUTCDate(timestamp);
const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60));
if (diffInMinutes < 1) return 'just now';
@ -174,7 +179,7 @@ document.addEventListener('DOMContentLoaded', async () => {
const now = new Date();
// Ensure timestamp is treated as UTC if it doesn't have timezone info
const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z');
const reportTime = parseUTCDate(timestamp);
const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000);
const remaining = expirationTime - now;
@ -198,7 +203,7 @@ document.addEventListener('DOMContentLoaded', async () => {
const now = new Date();
// Ensure timestamp is treated as UTC if it doesn't have timezone info
const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z');
const reportTime = parseUTCDate(timestamp);
const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000);
const remaining = expirationTime - now;
const hoursRemaining = remaining / (1000 * 60 * 60);