Files
gui-video-clipper/src/lib/utils/time.ts

37 lines
1.2 KiB
TypeScript
Raw Normal View History

export function formatTime(totalSeconds: number): string {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = Math.floor(totalSeconds % 60);
const millis = Math.round((totalSeconds % 1) * 1000);
const mm = String(minutes).padStart(2, '0');
const ss = String(seconds).padStart(2, '0');
const ms = String(millis).padStart(3, '0');
if (hours > 0) {
return `${hours}:${mm}:${ss}.${ms}`;
}
return `${mm}:${ss}.${ms}`;
}
export function parseTime(str: string): number | null {
const hmsMatch = str.match(/^(\d+):(\d{2}):(\d{2})(?:\.(\d{1,3}))?$/);
if (hmsMatch) {
const h = parseInt(hmsMatch[1], 10);
const m = parseInt(hmsMatch[2], 10);
const s = parseInt(hmsMatch[3], 10);
const ms = hmsMatch[4] ? parseInt(hmsMatch[4].padEnd(3, '0'), 10) : 0;
return h * 3600 + m * 60 + s + ms / 1000;
}
const msMatch = str.match(/^(\d{2}):(\d{2})(?:\.(\d{1,3}))?$/);
if (msMatch) {
const m = parseInt(msMatch[1], 10);
const s = parseInt(msMatch[2], 10);
const ms = msMatch[3] ? parseInt(msMatch[3].padEnd(3, '0'), 10) : 0;
return m * 60 + s + ms / 1000;
}
return null;
}