function remainingMs(expiresAt: number, now: number): number { return Math.max(0, expiresAt - now); } function remainingSeconds(expiresAt: number, now: number): number { return Math.ceil(remainingMs(expiresAt, now) / 1000); } function progress(createdAt: number, expiresAt: number, now: number): number { const total = expiresAt - createdAt; if (total <= 0) return 0; return Math.min(1, Math.max(0, (now - createdAt) / total)); } function label(expiresAt: number, now: number): string { const seconds = remainingSeconds(expiresAt, now); return seconds >= 60 ? `${Math.floor(seconds / 60)}m ${seconds % 60}s` : `${seconds}s`; } const start = 10_000; const end = 25_000; console.log( JSON.stringify({ evt: "smoke", ok: remainingSeconds(end, start) === 15 && remainingSeconds(end, end) === 0 && remainingSeconds(end, end + 5000) === 0 && remainingMs(end, end - 1000) === 1000 && progress(start, end, start) === 0 && progress(start, end, start + 7500) === 0.5 && progress(start, end, end + 1000) === 1 && label(end, start) === "15s" && label(start + 65_000, start) === "1m 5s", atStart: remainingSeconds(end, start), halfway: progress(start, end, start + 7500), atEnd: remainingSeconds(end, end), overrun: progress(start, end, end + 1000), labels: [label(end, start), label(start + 65_000, start)], }), );