-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeFormats.ts
57 lines (50 loc) · 1.58 KB
/
timeFormats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// parses a hms range (ie 5h2m20.5s-5m2m30s) to a pair of seconds (ie [18140.5, 18150])
export function hmsRangeToSeconds(range: string): [number?, number?] {
const split = range.split(":");
if (split.length !== 2) {
throw new Error(`invalid time range format ${range}, should be "XhYmZs:XhYmZs"`);
}
const [start, end] = (
split.map((time) => time.trim())
.map((time) => time === "" ? undefined : hmsToSeconds(time))
);
return [start, end];
}
// parses a clock time (ie "05:20:20.5") to a number of seconds (ie 18140.5)
export function clockToSeconds(time: string): number {
let [hours, minutes, seconds] = time.split(":").map((str) => parseFloat(str));
return hours * 60 * 60 + minutes * 60 + seconds;
}
// parses an hms time (ie "5h2m20.5s") to a number of seconds (ie 18140.5)
export function hmsToSeconds(time: string): number {
let hourStr = "";
let minuteStr = "";
let secondStr = "";
let currentNumber = "";
for (const c of time) {
const cCode = c.charCodeAt(0);
const isNumber = cCode >= "0".charCodeAt(0) && cCode <= "9".charCodeAt(0);
if (isNumber || c === ".") {
currentNumber += c;
} else {
switch (c) {
case "h":
hourStr = currentNumber;
break;
case "m":
minuteStr = currentNumber;
break;
case "s":
secondStr = currentNumber;
break;
default:
throw new Error("unexpected timestamp unit: " + c);
}
currentNumber = "";
}
}
let [hours, minutes, seconds] = [hourStr, minuteStr, secondStr]
.map((str) => str || "0")
.map((str) => parseFloat(str));
return hours * 60 * 60 + minutes * 60 + seconds;
}