-
Notifications
You must be signed in to change notification settings - Fork 2
/
duration.go
45 lines (40 loc) · 1.07 KB
/
duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func parseDuration(durationStr string) (time.Duration, error) {
parts := strings.Split(durationStr, ":")
if len(parts) < 2 || len(parts) > 3 {
return 0, fmt.Errorf("invalid duration format %q", durationStr)
}
var hours, minutes, seconds int
var err error
switch len(parts) {
case 2: // "mm:ss" format
minutes, err = strconv.Atoi(parts[0])
if err != nil {
return 0, fmt.Errorf("invalid minutes: %v", err)
}
seconds, err = strconv.Atoi(parts[1])
if err != nil {
return 0, fmt.Errorf("invalid seconds: %v", err)
}
case 3: // "hh:mm:ss" format
hours, err = strconv.Atoi(parts[0])
if err != nil {
return 0, fmt.Errorf("invalid hours: %v", err)
}
minutes, err = strconv.Atoi(parts[1])
if err != nil {
return 0, fmt.Errorf("invalid minutes: %v", err)
}
seconds, err = strconv.Atoi(parts[2])
if err != nil {
return 0, fmt.Errorf("invalid seconds: %v", err)
}
}
return time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute + time.Duration(seconds)*time.Second, nil
}