feat: add calcOffsetSecs utility to compute accurate elapsed seconds for broadcasts

This commit is contained in:
2026-03-11 21:21:43 +01:00
parent 477de2c49d
commit d7b21120c8
2 changed files with 12 additions and 1 deletions

View File

@@ -24,6 +24,16 @@ export function calcProgress(startAt: string, durationSecs: number): number {
return Math.min(100, Math.max(0, Math.round((elapsedSecs / durationSecs) * 100)));
}
/**
* Seconds elapsed since a slot started, computed from the current wall clock.
* Use this instead of `broadcast.offset_secs` when the broadcast may be cached —
* start_at is a fixed timestamp so the offset is always accurate regardless of
* when the broadcast response was fetched.
*/
export function calcOffsetSecs(startAt: string): number {
return Math.max(0, (Date.now() - new Date(startAt).getTime()) / 1000);
}
/** Minutes until a future timestamp (rounded, minimum 0). */
export function minutesUntil(iso: string): number {
return Math.max(0, Math.round((new Date(iso).getTime() - Date.now()) / 60_000));