/** * Shared utility functions for the Great Lakes Ice Report frontend */ /** * Helper function to parse UTC date consistently across all frontend scripts * Ensures timestamp is treated as UTC if it doesn't have timezone info * @param {string} timestamp - The timestamp string to parse * @returns {Date} - Parsed Date object with proper UTC interpretation */ const parseUTCDate = (timestamp) => { return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); }; /** * Calculate human-readable time ago string * @param {string} timestamp - The timestamp to calculate from * @returns {string} - Human-readable time difference */ const getTimeAgo = (timestamp) => { const now = new Date(); const reportTime = parseUTCDate(timestamp); const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60)); if (diffInMinutes < 1) return 'just now'; if (diffInMinutes < 60) return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''} ago`; const diffInHours = Math.floor(diffInMinutes / 60); if (diffInHours < 24) return `${diffInHours} hour${diffInHours !== 1 ? 's' : ''} ago`; const diffInDays = Math.floor(diffInHours / 24); if (diffInDays < 7) return `${diffInDays} day${diffInDays !== 1 ? 's' : ''} ago`; return reportTime.toLocaleDateString(); }; /** * Calculate time remaining until 48-hour expiration * @param {string} timestamp - The creation timestamp * @param {boolean} persistent - Whether the report is persistent * @returns {string} - Time remaining string */ const getTimeRemaining = (timestamp, persistent = false) => { if (persistent) { return 'Persistent'; } const now = new Date(); const reportTime = parseUTCDate(timestamp); const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000); const remaining = expirationTime - now; if (remaining <= 0) return 'Expired'; const hours = Math.floor(remaining / (1000 * 60 * 60)); const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60)); if (hours > 0) { return `${hours}h ${minutes}m`; } else { return `${minutes}m`; } }; /** * Get CSS class for time remaining styling * @param {string} timestamp - The creation timestamp * @param {boolean} persistent - Whether the report is persistent * @returns {string} - CSS class name */ const getRemainingClass = (timestamp, persistent = false) => { if (persistent) { return 'normal'; // Use normal styling for persistent reports } const now = new Date(); const reportTime = parseUTCDate(timestamp); const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000); const remaining = expirationTime - now; const hoursRemaining = remaining / (1000 * 60 * 60); if (hoursRemaining <= 1) return 'urgent'; if (hoursRemaining <= 6) return 'warning'; return 'normal'; }; // Export functions for module usage (if using ES6 modules in the future) // For now, functions are available globally when script is included if (typeof module !== 'undefined' && module.exports) { module.exports = { parseUTCDate, getTimeAgo, getTimeRemaining, getRemainingClass }; }