54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
/**
|
|
* formatters.js - Data formatting utilities
|
|
*
|
|
* This module contains utility functions for formatting time, durations, and other data.
|
|
*/
|
|
|
|
/**
|
|
* Format time to GMT+4 timezone (Asia/Dubai)
|
|
* This timezone is used consistently throughout the application:
|
|
* - PostgreSQL database is set to Asia/Dubai timezone (UTC+4)
|
|
* - Backend uses UTC internally but stores in Asia/Dubai in database
|
|
* - All displayed times on frontend use this timezone for consistency
|
|
*
|
|
* @param {string} dateTimeStr - ISO date-time string
|
|
* @returns {string} Formatted time string in GMT+4
|
|
*/
|
|
export function formatTimeToGMT4(dateTimeStr) {
|
|
if (!dateTimeStr) return 'N/A';
|
|
|
|
try {
|
|
const date = new Date(dateTimeStr);
|
|
// Format the time using the Etc/GMT-4 timezone, which represents GMT+4.
|
|
// The locale 'en-US' or 'en-GB' can be used for formatting conventions like AM/PM.
|
|
return date.toLocaleTimeString('en-US', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: true,
|
|
timeZone: 'Etc/GMT-4'
|
|
}) + ' (GMT+4)';
|
|
} catch (error) {
|
|
console.error("Error formatting time to GMT+4:", error, "Input:", dateTimeStr);
|
|
return 'Invalid Date'; // Or return N/A or the original string
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convert decimal hours to hours and minutes format
|
|
* @param {number} decimalHours - Hours in decimal format
|
|
* @returns {string} Formatted duration string
|
|
*/
|
|
export function formatDurationHours(decimalHours) {
|
|
if (decimalHours === null || decimalHours === undefined) return 'N/A';
|
|
|
|
const hours = Math.floor(decimalHours);
|
|
const minutes = Math.round((decimalHours - hours) * 60);
|
|
|
|
if (hours === 0) {
|
|
return `${minutes} min`;
|
|
} else if (minutes === 0) {
|
|
return `${hours} hr`;
|
|
} else {
|
|
return `${hours} hr ${minutes} min`;
|
|
}
|
|
}
|