strip-beats/utils/utils.go

39 lines
717 B
Go
Raw Normal View History

2023-09-06 18:46:09 -05:00
package utils
import (
"errors"
2023-09-06 18:46:09 -05:00
"fmt"
p "path"
"strings"
t "time"
2023-09-06 18:46:09 -05:00
)
func GetShortPath(path string) string {
base := p.Base(path)
dir := p.Dir(path)
dir = p.Base(dir)
return fmt.Sprintf("%s/%s", dir, base)
}
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 := ""
for idx, part := range parts {
f = f + part + units[idx]
}
dur, _ := t.ParseDuration(f)
return dur.Seconds(), nil
}
2023-09-24 22:34:30 -05:00
func Tern[V any](choice bool, one, two V) V {
if choice {
return one
}
return two
}