import { viewerDiffersFromPractice, viewerTimeZone } from '../practiceTimezone'; import { pickedTimeInstant } from './patientPickedTime'; /** * The viewer's zone name for one visit, or `null` when its time needs no label. Labelled only when the * viewer's offset differs from the visit's own site (`practiceZone`) at the visit's own instant, so two * sites or a daylight-saving change can label one visit and not another on the same day. */ export function visitZoneLabel( dateStr: string, timeStr: string, practiceZone: string | null | undefined, viewerZone: string = viewerTimeZone(), ): string | null { if (!dateStr || !timeStr || !practiceZone) return null; const instant = pickedTimeInstant(dateStr, timeStr, viewerZone); if (Number.isNaN(instant.getTime()) || !viewerDiffersFromPractice(instant, practiceZone, viewerZone)) return null; return new Intl.DateTimeFormat('en-US', { timeZone: viewerZone, timeZoneName: 'short' }) .formatToParts(instant).find((part) => part.type === 'timeZoneName')?.value ?? viewerZone; }