Calendar/src/utils/TimeFormatter.ts

95 lines
3.4 KiB
TypeScript
Raw Normal View History

/**
* 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';
2025-11-03 22:04:37 +01:00
import { ITimeFormatConfig } from '../configurations/TimeFormatConfig';
export class TimeFormatter {
2025-11-03 22:04:37 +01:00
private static settings: ITimeFormatConfig | null = null;
// 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.');
}
// Create a minimal config object for DateService
const config = {
2025-11-03 22:04:37 +01:00
timeFormatConfig: {
timezone: TimeFormatter.settings.timezone
}
};
TimeFormatter.dateService = new DateService(config as any);
}
return TimeFormatter.dateService;
}
/**
* Configure time formatting settings
2025-11-03 22:04:37 +01:00
* Must be called before using TimeFormatter
*/
2025-11-03 22:04:37 +01:00
static configure(settings: ITimeFormatConfig): void {
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 {
2025-11-03 22:04:37 +01:00
if (!TimeFormatter.settings) {
throw new Error('TimeFormatter must be configured before use. Call TimeFormatter.configure() first.');
}
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);
}
}