import { InvalidIntervalError, NonWholeMinuteIntervalError } from "./errors.generated"; function isWholeMinute(instant: Date): boolean { return instant.getUTCSeconds() === 0 && instant.getUTCMilliseconds() === 0; } /** * Function: validateReportedInterval * Description: Shared well-formedness guard for a ReportedTimeBlock's [startAt, endAt] interval, * applied by every writer (manual declaration, correction, import, punch-derived formation) so the * same rules hold whatever the source (M08). Every block must span a positive interval * (endAt strictly after startAt). Declared/imported blocks additionally must sit on whole-minute * boundaries — punch-derived blocks carry raw device timestamps and skip that check. Returns the * domain error to surface, or null when the interval is valid. */ export function validateReportedInterval( identifier: string, startAt: Date, endAt: Date, opts: { requireWholeMinute: boolean }, ): | InstanceType | InstanceType | null { if (endAt.getTime() <= startAt.getTime()) { return new InvalidIntervalError(identifier); } if (opts.requireWholeMinute && (!isWholeMinute(startAt) || !isWholeMinute(endAt))) { return new NonWholeMinuteIntervalError(identifier); } return null; }