forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockParse.ts
More file actions
43 lines (41 loc) · 1.65 KB
/
clockParse.ts
File metadata and controls
43 lines (41 loc) · 1.65 KB
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
/** Parse time string (H:MM:SS, MM:SS, M:SS, SS, or SS.t for tenths) to centis. Returns undefined if invalid. */
export function parseTimeToCentis(str: string): number | undefined {
const s = str.trim();
if (s === '' || s === '--:--') return undefined;
const parts = s.split(':').map(p => p.trim());
if (parts.some(p => p === '')) return undefined;
const lastPart = parts[parts.length - 1];
const lastNum = parseFloat(lastPart);
if (isNaN(lastNum) || lastNum < 0) return undefined;
const wholeParts = parts.slice(0, -1).map(p => parseInt(p, 10));
if (wholeParts.some(n => isNaN(n) || n < 0)) return undefined;
let seconds = 0;
if (parts.length === 1) {
seconds = lastNum;
} else if (parts.length === 2) {
seconds = parseInt(parts[0], 10) * 60 + lastNum;
} else if (parts.length === 3) {
seconds = parseInt(parts[0], 10) * 3600 + parseInt(parts[1], 10) * 60 + lastNum;
} else {
return undefined;
}
return Math.round(seconds * 100);
}
const pad2 = (num: number): string => (num < 10 ? '0' : '') + num;
/** Format centis as H:MM:SS, MM:SS, or SS.t for display in input (tenths when non-integer). */
export function formatClockFromCentis(centis: number): string {
if (centis <= 0) return '0:00:00';
const date = new Date(centis * 10),
hours = Math.floor(centis / 360000),
mins = date.getUTCMinutes(),
secs = date.getUTCSeconds(),
tenths = Math.round((centis % 100) / 10);
const secStr = tenths > 0 ? `${pad2(secs)}.${tenths}` : pad2(secs);
if (hours > 0) {
return `${hours}:${pad2(mins)}:${secStr}`;
}
if (mins > 0) {
return `${mins}:${secStr}`;
}
return tenths > 0 ? `${secs}.${tenths}` : `${secs}`;
}