-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.go
More file actions
48 lines (41 loc) · 1.05 KB
/
Copy pathtime.go
File metadata and controls
48 lines (41 loc) · 1.05 KB
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
46
47
48
package parkrunparser
import (
"fmt"
"strconv"
"strings"
"time"
)
func ParseDate(s string) (time.Time, error) {
if t, err := time.Parse("02/01/2006", s); err == nil {
return t, nil
}
return time.Parse("2006-01-02", s)
}
func ParseDuration(s string) (time.Duration, error) {
split := strings.Split(s, ":")
splitLen := len(split)
if splitLen != 2 && splitLen != 3 {
return 0, fmt.Errorf("cannot parse duration '%s'", s)
}
t := time.Duration(0)
// hh:mm::ss
if splitLen == 3 {
if value, err := strconv.Atoi(split[0]); err != nil {
return 0, fmt.Errorf("cannot parse duration '%s': %w", s, err)
} else {
t += time.Duration(value) * time.Hour
}
}
// mm::ss
if value, err := strconv.Atoi(split[splitLen-2]); err != nil {
return 0, fmt.Errorf("cannot parse duration '%s': %w", s, err)
} else {
t += time.Duration(value) * time.Minute
}
if value, err := strconv.Atoi(split[splitLen-1]); err != nil {
return 0, fmt.Errorf("cannot parse duration '%s': %w", s, err)
} else {
t += time.Duration(value) * time.Second
}
return t, nil
}