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-09-12 22:21:56 +02:00
|
|
|
export interface TimeFormatSettings {
|
|
|
|
|
timezone: string;
|
|
|
|
|
use24HourFormat: boolean;
|
|
|
|
|
locale: string;
|
2025-10-03 16:05:22 +02:00
|
|
|
dateFormat: 'locale' | 'technical';
|
|
|
|
|
showSeconds: boolean;
|
2025-09-12 22:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class TimeFormatter {
|
|
|
|
|
private static settings: TimeFormatSettings = {
|
|
|
|
|
timezone: 'Europe/Copenhagen', // Default to Denmark
|
|
|
|
|
use24HourFormat: true, // 24-hour format standard in Denmark
|
2025-10-03 16:05:22 +02:00
|
|
|
locale: 'da-DK', // Danish locale
|
|
|
|
|
dateFormat: 'technical', // Use technical format yyyy-mm-dd hh:mm:ss
|
|
|
|
|
showSeconds: false // Don't show seconds by default
|
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) {
|
|
|
|
|
// Create a minimal config object for DateService
|
|
|
|
|
const config = {
|
|
|
|
|
getTimezone: () => TimeFormatter.settings.timezone
|
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
static configure(settings: Partial<TimeFormatSettings>): void {
|
|
|
|
|
TimeFormatter.settings = { ...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-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
|
|
|
}
|