/** * 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'; export interface ITimeFormatSettings { timezone: string; use24HourFormat: boolean; locale: string; dateFormat: 'locale' | 'technical'; showSeconds: boolean; } export class TimeFormatter { private static settings: ITimeFormatSettings = { timezone: 'Europe/Copenhagen', // Default to Denmark use24HourFormat: true, // 24-hour format standard in Denmark locale: 'da-DK', // Danish locale dateFormat: 'technical', // Use technical format yyyy-mm-dd hh:mm:ss showSeconds: false // Don't show seconds by default }; // DateService will be initialized lazily to avoid circular dependency with CalendarConfig private static dateService: DateService | null = null; private static getDateService(): DateService { if (!TimeFormatter.dateService) { // Create a minimal config object for DateService const config = { getTimezone: () => TimeFormatter.settings.timezone }; TimeFormatter.dateService = new DateService(config as any); } return TimeFormatter.dateService; } /** * Configure time formatting settings */ static configure(settings: Partial): void { TimeFormatter.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 */ private static convertToLocalTime(utcDate: Date | string): Date { 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") */ private static format24Hour(date: Date): string { const localDate = TimeFormatter.convertToLocalTime(date); return TimeFormatter.getDateService().formatTime(localDate, TimeFormatter.settings.showSeconds); } /** * Format time according to current configuration * @param date - Date to format * @returns Formatted time string */ static formatTime(date: Date): string { // 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: Date, endDate: Date): string { const localStart = TimeFormatter.convertToLocalTime(startDate); const localEnd = TimeFormatter.convertToLocalTime(endDate); return TimeFormatter.getDateService().formatTimeRange(localStart, localEnd); } }