/** * TimeFormatter - Centralized time formatting with timezone support * Now uses DateService internally for all date/time operations * * Handles conversion from UTC/Zulu time to configured timezone (default: Europe/Copenhagen) * Supports both 12-hour and 24-hour format configuration * * All events in the system are stored in UTC and must be converted to local timezone */ import { DateService } from './DateService'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; // Enable day.js plugins for timezone formatting dayjs.extend(utc); dayjs.extend(timezone); export class TimeFormatter { static getDateService() { if (!TimeFormatter.dateService) { if (!TimeFormatter.settings) { throw new Error('TimeFormatter must be configured before use. Call TimeFormatter.configure() first.'); } // Create a minimal config object for DateService const config = { timeFormatConfig: { timezone: TimeFormatter.settings.timezone } }; TimeFormatter.dateService = new DateService(config); } return TimeFormatter.dateService; } /** * Configure time formatting settings * Must be called before using TimeFormatter */ static configure(settings) { TimeFormatter.settings = settings; // Reset DateService to pick up new timezone TimeFormatter.dateService = null; } /** * Convert UTC date to configured timezone (internal helper) * @param utcDate - Date in UTC (or ISO string) * @returns Date object adjusted to configured timezone */ static convertToLocalTime(utcDate) { if (typeof utcDate === 'string') { return TimeFormatter.getDateService().fromUTC(utcDate); } // If it's already a Date object, convert to UTC string first, then back to local const utcString = utcDate.toISOString(); return TimeFormatter.getDateService().fromUTC(utcString); } /** * Format time in 24-hour format using DateService (internal helper) * @param date - Date to format * @returns Formatted time string (e.g., "09:00") */ static format24Hour(date) { if (!TimeFormatter.settings) { throw new Error('TimeFormatter must be configured before use. Call TimeFormatter.configure() first.'); } // Use day.js directly to format with timezone awareness const pattern = TimeFormatter.settings.showSeconds ? 'HH:mm:ss' : 'HH:mm'; return dayjs.utc(date).tz(TimeFormatter.settings.timezone).format(pattern); } /** * Format time according to current configuration * @param date - Date to format * @returns Formatted time string */ static formatTime(date) { // Always use 24-hour format (12-hour support removed as unused) return TimeFormatter.format24Hour(date); } /** * Format time range (start - end) using DateService * @param startDate - Start date * @param endDate - End date * @returns Formatted time range string (e.g., "09:00 - 10:30") */ static formatTimeRange(startDate, endDate) { const localStart = TimeFormatter.convertToLocalTime(startDate); const localEnd = TimeFormatter.convertToLocalTime(endDate); return TimeFormatter.getDateService().formatTimeRange(localStart, localEnd); } } TimeFormatter.settings = null; // DateService will be initialized lazily to avoid circular dependency with CalendarConfig TimeFormatter.dateService = null; //# sourceMappingURL=TimeFormatter.js.map