import { ensureCompanyInfo } from "../services/mm-api-service/company-info.js"; import { convertToLocalTime, formatDateWithTZOffset, } from "./timezone.js"; /** * Calculates the difference in hours between two timestamps * @param startTime ISO timestamp string * @param endTime ISO timestamp string * @param timezoneOffset timezone offset in hours * @returns number representing hours difference, rounded to 3 decimal places */ export function calculateTimeDifferenceInHours( startTime: string | null | undefined, endTime: string | null | undefined, timezoneOffset: number ): number { if (!startTime || !endTime) return 0; const localStartTime = convertToLocalTime(startTime, timezoneOffset); const localEndTime = convertToLocalTime(endTime, timezoneOffset); if (!localStartTime || !localEndTime) return 0; // Get hours, minutes, seconds from the Date objects const startHours = localStartTime.getHours(); const startMinutes = localStartTime.getMinutes(); const startSeconds = localStartTime.getSeconds(); const endHours = localEndTime.getHours(); const endMinutes = localEndTime.getMinutes(); const endSeconds = localEndTime.getSeconds(); // Calculate total seconds for each time const startTotalSeconds = startHours * 3600 + startMinutes * 60 + startSeconds; const endTotalSeconds = endHours * 3600 + endMinutes * 60 + endSeconds; if (endTotalSeconds > startTotalSeconds) { return Number(((endTotalSeconds - startTotalSeconds) / 3600).toFixed(3)); } return 0; } export const getSecondsOfDay = (timestamp: string | null) => { if (!timestamp) return null; const time = new Date(timestamp); const utcHours = time.getUTCHours(); const utcMinutes = time.getUTCMinutes(); const utcSeconds = time.getUTCSeconds(); return (utcHours * 3600 + utcMinutes * 60 + utcSeconds).toString(); }; /** * Apply timezone offsets to specified datetime fields in an object * @param item The object to transform * @param fields The fields to apply timezone offset to * @param timezoneOffset The timezone offset to apply * @returns The transformed object */ export const applyTimezoneOffsetsToFields = ( item: Record, fields: string[], timezoneOffset: number ): Record => { const transformed = { ...item }; fields.forEach((field) => { if (transformed[field] && transformed[field].trim()) { const formattedDate = formatDateWithTZOffset( transformed[field], timezoneOffset ); transformed[field] = formattedDate || ""; } else { transformed[field] = ""; } }); return transformed; }; /** * @deprecated Use {@link ensureCompanyInfo} from the MM API module; name retained for backward compatibility. * * Ensures company info (including IANA timezone) is loaded with retries. * Sync readers {@link getCachedTimezoneOffset} / {@link getCachedTimezoneName} resolve company info * from in-memory cache or persisted job-state (`companyInfo`); offset is derived from the IANA name at read time, not stored. * * @param params.maxRetries Maximum number of retry attempts * @param params.retryIntervalMs Time to wait between retries in milliseconds */ export const getTimezoneOffsetAndPersist = ensureCompanyInfo;