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:
parent
544766e5dc
commit
45a8d67362
4 changed files with 28 additions and 6 deletions
|
@ -495,7 +495,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
function 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';
|
||||
|
@ -510,6 +510,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
return reportTime.toLocaleDateString();
|
||||
}
|
||||
|
||||
// Helper function to parse UTC date
|
||||
function parseUTCDate(timestamp) {
|
||||
return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z');
|
||||
}
|
||||
|
||||
// Profanity management functions
|
||||
async function loadProfanityWords() {
|
||||
if (!profanityTableBody) return;
|
||||
|
|
|
@ -82,9 +82,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();
|
||||
const reportTime = new Date(timestamp);
|
||||
// Ensure timestamp is treated as UTC if it doesn't have timezone info
|
||||
const reportTime = parseUTCDate(timestamp);
|
||||
const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60));
|
||||
|
||||
if (diffInMinutes < 1) return 'just now';
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -74,9 +74,15 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
});
|
||||
};
|
||||
|
||||
// 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();
|
||||
const reportTime = new Date(timestamp);
|
||||
// Ensure timestamp is treated as UTC if it doesn't have timezone info
|
||||
const reportTime = parseUTCDate(timestamp);
|
||||
const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60));
|
||||
|
||||
if (diffInMinutes < 1) return 'just now';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue