pstream is dead; long live pstream
taciturnaxolotl.github.io/pstream-ng/
1// Convert `t` param to time. Supports having only seconds (like `?t=192`), but also `3:30` or `1:30:02`
2export function parseTimestamp(str: string | undefined | null): number | null {
3 const input = str ?? "";
4 const isValid = !!input.match(/^\d+(:\d+)*$/);
5 if (!isValid) return null;
6
7 const timeArr = input.split(":").map(Number).reverse();
8 const hours = timeArr[2] ?? 0;
9 const minutes = Math.min(timeArr[1] ?? 0, 59);
10 const seconds = Math.min(timeArr[0] ?? 0, minutes > 0 ? 59 : Infinity);
11
12 const timeInSeconds = hours * 60 * 60 + minutes * 60 + seconds;
13 return timeInSeconds;
14}