2019-02-09 12:30:49 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-05-25 01:25:26 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2021-05-25 01:25:26 +00:00
|
|
|
// from stdlib's time.go
|
|
|
|
func norm(hi, lo, base int) (nhi, nlo int) {
|
|
|
|
if lo < 0 {
|
|
|
|
n := (-lo-1)/base + 1
|
|
|
|
hi -= n
|
|
|
|
lo += n * base
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-05-25 01:25:26 +00:00
|
|
|
if lo >= base {
|
|
|
|
n := lo / base
|
|
|
|
hi += n
|
|
|
|
lo -= n * base
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-05-25 01:25:26 +00:00
|
|
|
return hi, lo
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-05-25 01:25:26 +00:00
|
|
|
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
|
|
|
|
func GetVTTTime(fracSeconds float64) string {
|
|
|
|
if fracSeconds < 0 || math.IsNaN(fracSeconds) || math.IsInf(fracSeconds, 0) {
|
|
|
|
return "00:00:00.000"
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 22:47:29 +00:00
|
|
|
var msec, sec, mnt, hour int
|
2021-05-25 01:25:26 +00:00
|
|
|
msec = int(fracSeconds * 1000)
|
|
|
|
sec, msec = norm(sec, msec, 1000)
|
2024-11-12 22:47:29 +00:00
|
|
|
mnt, sec = norm(mnt, sec, 60)
|
|
|
|
hour, mnt = norm(hour, mnt, 60)
|
2021-05-25 01:25:26 +00:00
|
|
|
|
2024-11-12 22:47:29 +00:00
|
|
|
return fmt.Sprintf("%02d:%02d:%02d.%03d", hour, mnt, sec, msec)
|
2019-12-13 20:41:46 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|