♻️ Use HoursMinSecondsToS to parse time for fading/trimming
This commit is contained in:
parent
d175360362
commit
4057d00c46
3 changed files with 40 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
p "path"
|
||||
"strings"
|
||||
|
|
@ -14,8 +15,11 @@ func GetShortPath(path string) string {
|
|||
return fmt.Sprintf("%s/%s", dir, base)
|
||||
}
|
||||
|
||||
func HourMinSecToSeconds(time string) float64 {
|
||||
func HourMinSecToSeconds(time string) (float64, error) {
|
||||
parts := strings.Split(time, ":")
|
||||
if len(parts) > 3 {
|
||||
return 0, errors.New(fmt.Sprintf("Can't parse %s. Must be in HH:MM:SS format", time))
|
||||
}
|
||||
units := []string{"h", "m", "s"}
|
||||
units = units[len(units)-len(parts):]
|
||||
f := ""
|
||||
|
|
@ -23,5 +27,5 @@ func HourMinSecToSeconds(time string) float64 {
|
|||
f = f + part + units[idx]
|
||||
}
|
||||
dur, _ := t.ParseDuration(f)
|
||||
return dur.Seconds()
|
||||
return dur.Seconds(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,48 @@
|
|||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func hmsTest(t *testing.T, time string, expected float64) {
|
||||
found := HourMinSecToSeconds(time)
|
||||
if found != expected {
|
||||
t.Fatalf(`HourMinSecToSeconds("%s") = %v, want %v`, time, found, expected)
|
||||
func hmsTest(t *testing.T, time string, expected float64, expecterr error) {
|
||||
found, err := HourMinSecToSeconds(time)
|
||||
var expecterrmsg, errmsg string
|
||||
if err != nil {
|
||||
errmsg = err.Error()
|
||||
}
|
||||
if expecterr != nil {
|
||||
expecterrmsg = expecterr.Error()
|
||||
}
|
||||
|
||||
if found != expected || errmsg != expecterrmsg {
|
||||
t.Fatalf(`HourMinSecToSeconds("%s") = %v, %v, want %v, %v`, time, found, err, expected, expecterr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoManyParts(t *testing.T) {
|
||||
val := "2:3:4:5:6"
|
||||
err := fmt.Sprintf("Can't parse %s. Must be in HH:MM:SS format", val)
|
||||
hmsTest(t, val, 0, errors.New(err))
|
||||
}
|
||||
|
||||
func TestJustSeconds(t *testing.T) {
|
||||
hmsTest(t, "4.23", 4.23)
|
||||
hmsTest(t, "4.23", 4.23, nil)
|
||||
}
|
||||
|
||||
func TestMinuteSeconds(t *testing.T) {
|
||||
hmsTest(t, "2:3.45", 123.45)
|
||||
hmsTest(t, "2:3.45", 123.45, nil)
|
||||
}
|
||||
|
||||
func TestHourMinuteSeconds(t *testing.T) {
|
||||
hmsTest(t, "3:5:12.43", 11112.43)
|
||||
hmsTest(t, "3:5:12.43", 11112.43, nil)
|
||||
}
|
||||
|
||||
func TestLeadingZeros(t *testing.T) {
|
||||
hmsTest(t, "03:05:02.43", 11102.43)
|
||||
hmsTest(t, "03:05:02.43", 11102.43, nil)
|
||||
}
|
||||
|
||||
func TestWholeSecond(t *testing.T) {
|
||||
hmsTest(t, "4:2", 242)
|
||||
hmsTest(t, "4:2", 242, nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue