2025-09-12 22:21:56 +02:00
|
|
|
/**
|
|
|
|
|
* TimeFormatter - Centralized time formatting with timezone support
|
2025-10-03 16:47:42 +02:00
|
|
|
* Now uses DateService internally for all date/time operations
|
2025-09-12 22:21:56 +02:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-03 16:47:42 +02:00
|
|
|
import { DateService } from './DateService';
|
2025-11-03 22:04:37 +01:00
|
|
|
import { ITimeFormatConfig } from '../configurations/TimeFormatConfig';
|
2025-09-12 22:21:56 +02:00
|
|
|
|
|
|
|
|
export class TimeFormatter {
|
2025-11-03 22:04:37 +01:00
|
|
|
private static settings: ITimeFormatConfig | null = null;
|
2025-09-12 22:21:56 +02:00
|
|
|
|
2025-10-30 23:47:30 +01:00
|
|
|
// DateService will be initialized lazily to avoid circular dependency with CalendarConfig
|
|
|
|
|
private static dateService: DateService | null = null;
|
|
|
|
|
|
|
|
|
|
private static getDateService(): DateService {
|
|
|
|
|
if (!TimeFormatter.dateService) {
|
2025-11-03 22:04:37 +01:00
|
|
|
if (!TimeFormatter.settings) {
|
|
|
|
|
throw new Error('TimeFormatter must be configured before use. Call TimeFormatter.configure() first.');
|
|
|
|
|
}
|
2025-10-30 23:47:30 +01:00
|
|
|
// Create a minimal config object for DateService
|
|
|
|
|
const config = {
|
2025-11-03 22:04:37 +01:00
|
|
|
timeFormatConfig: {
|
|
|
|
|
timezone: TimeFormatter.settings.timezone
|
|
|
|
|
}
|
2025-10-30 23:47:30 +01:00
|
|
|
};
|
|
|
|
|
TimeFormatter.dateService = new DateService(config as any);
|
|
|
|
|
}
|
|
|
|
|
return TimeFormatter.dateService;
|
|
|
|
|
}
|
2025-10-03 16:47:42 +02:00
|
|
|
|
2025-09-12 22:21:56 +02:00
|
|
|
/**
|
|
|
|
|
* Configure time formatting settings
|
2025-11-03 22:04:37 +01:00
|
|
|
* Must be called before using TimeFormatter
|
2025-09-12 22:21:56 +02:00
|
|
|
*/
|
2025-11-03 22:04:37 +01:00
|
|
|
static configure(settings: ITimeFormatConfig): void {
|
|
|
|
|
TimeFormatter.settings = settings;
|
2025-10-30 23:47:30 +01:00
|
|
|
// Reset DateService to pick up new timezone
|
|
|
|
|
TimeFormatter.dateService = null;
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-30 23:47:30 +01:00
|
|
|
* Convert UTC date to configured timezone (internal helper)
|
2025-10-03 16:47:42 +02:00
|
|
|
* @param utcDate - Date in UTC (or ISO string)
|
2025-09-12 22:21:56 +02:00
|
|
|
* @returns Date object adjusted to configured timezone
|
|
|
|
|
*/
|
2025-10-30 23:47:30 +01:00
|
|
|
private static convertToLocalTime(utcDate: Date | string): Date {
|
2025-10-03 16:47:42 +02:00
|
|
|
if (typeof utcDate === 'string') {
|
2025-10-30 23:47:30 +01:00
|
|
|
return TimeFormatter.getDateService().fromUTC(utcDate);
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
2025-10-30 23:47:30 +01:00
|
|
|
|
2025-10-03 16:47:42 +02:00
|
|
|
// If it's already a Date object, convert to UTC string first, then back to local
|
|
|
|
|
const utcString = utcDate.toISOString();
|
2025-10-30 23:47:30 +01:00
|
|
|
return TimeFormatter.getDateService().fromUTC(utcString);
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-30 23:47:30 +01:00
|
|
|
* Format time in 24-hour format using DateService (internal helper)
|
2025-09-12 22:21:56 +02:00
|
|
|
* @param date - Date to format
|
|
|
|
|
* @returns Formatted time string (e.g., "09:00")
|
|
|
|
|
*/
|
2025-10-30 23:47:30 +01:00
|
|
|
private static format24Hour(date: Date): string {
|
2025-11-03 22:04:37 +01:00
|
|
|
if (!TimeFormatter.settings) {
|
|
|
|
|
throw new Error('TimeFormatter must be configured before use. Call TimeFormatter.configure() first.');
|
|
|
|
|
}
|
2025-09-12 22:21:56 +02:00
|
|
|
const localDate = TimeFormatter.convertToLocalTime(date);
|
2025-10-30 23:47:30 +01:00
|
|
|
return TimeFormatter.getDateService().formatTime(localDate, TimeFormatter.settings.showSeconds);
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format time according to current configuration
|
|
|
|
|
* @param date - Date to format
|
|
|
|
|
* @returns Formatted time string
|
|
|
|
|
*/
|
|
|
|
|
static formatTime(date: Date): string {
|
2025-10-30 23:47:30 +01:00
|
|
|
// Always use 24-hour format (12-hour support removed as unused)
|
|
|
|
|
return TimeFormatter.format24Hour(date);
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-03 16:47:42 +02:00
|
|
|
* Format time range (start - end) using DateService
|
2025-09-12 22:21:56 +02:00
|
|
|
* @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 {
|
2025-10-03 16:47:42 +02:00
|
|
|
const localStart = TimeFormatter.convertToLocalTime(startDate);
|
|
|
|
|
const localEnd = TimeFormatter.convertToLocalTime(endDate);
|
2025-10-30 23:47:30 +01:00
|
|
|
return TimeFormatter.getDateService().formatTimeRange(localStart, localEnd);
|
2025-10-03 16:47:42 +02:00
|
|
|
}
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|