2020-05-14 11:57:26 +02:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
2022-11-17 08:28:30 +01:00
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
// Duration converts a metadata string to a valid duration.
|
|
|
|
func Duration(s string) (result time.Duration) {
|
2022-11-17 08:28:30 +01:00
|
|
|
if s == "" {
|
2023-02-22 16:33:33 +01:00
|
|
|
return 0
|
2022-11-17 08:28:30 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
s = clean.Duration(s)
|
2020-05-14 11:57:26 +02:00
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
if txt.IsFloat(s) {
|
|
|
|
result = time.Duration(txt.Float(s) * 1e9)
|
|
|
|
} else if n := strings.Split(strings.TrimSpace(s), ":"); len(n) == 3 {
|
|
|
|
hr, _ := strconv.Atoi(n[0])
|
|
|
|
min, _ := strconv.Atoi(n[1])
|
|
|
|
sec, _ := strconv.Atoi(n[2])
|
2020-05-14 11:57:26 +02:00
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
result = time.Duration(hr)*time.Hour + time.Duration(min)*time.Minute + time.Duration(sec)*time.Second
|
|
|
|
} else if d, err := time.ParseDuration(s); err == nil {
|
|
|
|
result = d
|
2020-05-14 11:57:26 +02:00
|
|
|
}
|
|
|
|
|
2023-02-22 16:33:33 +01:00
|
|
|
return result.Round(10e6)
|
2020-05-14 11:57:26 +02:00
|
|
|
}
|