1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Fix timetoint

01:02 was translated to 01h02m instead of 01m02s
This commit is contained in:
Chocobozzz 2023-10-30 10:20:25 +01:00
parent 22e05d15db
commit 4fa78cda92
No known key found for this signature in database
GPG key ID: 583A612D890159BE
2 changed files with 52 additions and 13 deletions

View file

@ -45,21 +45,42 @@ function isLastWeek (d: Date) {
// ---------------------------------------------------------------------------
export const timecodeRegexString = `((\\d+)[h:])?((\\d+)[m:])?((\\d+)s?)`
export const timecodeRegexString = `(\\d+[h:])?(\\d+[m:])?\\d+s?`
function timeToInt (time: number | string) {
if (!time) return 0
if (typeof time === 'number') return time
const reg = new RegExp(`^${timecodeRegexString}$`)
// Try with 00h00m00s format first
const reg = new RegExp(`^(\\d+h)?(\\d+m)?(\\d+)s?$`)
const matches = time.match(reg)
if (!matches) return 0
const hours = parseInt(matches[2] || '0', 10)
const minutes = parseInt(matches[4] || '0', 10)
const seconds = parseInt(matches[6] || '0', 10)
if (matches) {
const hours = parseInt(matches[1] || '0', 10)
const minutes = parseInt(matches[2] || '0', 10)
const seconds = parseInt(matches[3] || '0', 10)
return hours * 3600 + minutes * 60 + seconds
return hours * 3600 + minutes * 60 + seconds
}
// ':' format fallback
const parts = time.split(':').reverse()
const iMultiplier = {
0: 1,
1: 60,
2: 3600
}
let result = 0
for (let i = parts.length - 1; i >= 0; i--) {
const partInt = parseInt(parts[i], 10)
if (isNaN(partInt)) return 0
result += iMultiplier[i] * partInt
}
return result
}
function secondsToTime (seconds: number, full = false, symbol?: string) {