/**
 * Formats a time string (e.g., "14:30:00" or "14:30") to 12-hour format (e.g., "02:30 PM").
 * Omits seconds.
 */
export function formatTo12Hour(timeStr: string | undefined | null): string {
    if (!timeStr) return '';

    const [h, m] = timeStr.split(':');
    if (h === undefined || m === undefined) return timeStr;

    let hours = parseInt(h, 10);
    const minutes = m;

    const ampm = hours >= 12 ? 'PM' : 'AM';

    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'

    // Ensure 2-digit minutes and return
    return `${hours.toString().padStart(2, '0')}:${minutes.substring(0, 2)} ${ampm}`;
}
