-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigduration.go
53 lines (47 loc) · 1.04 KB
/
bigduration.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
46
47
48
49
50
51
52
53
package bigduration
import (
"fmt"
"math"
"regexp"
"strconv"
)
type BigDuration uint64
func ParseDuration(s string) (BigDuration, error) {
r := regexp.MustCompile(`^(([0-9|\.]+)y)?(([0-9|\.]+)d)?(([0-9|\.]+)h)?(([0-9|.]+)m)?(([0-9|\.]+)s)?$`)
matches := r.FindStringSubmatch(s)
if len(matches) != 11 {
return 0, fmt.Errorf("Failed to parse %s", s)
}
years, err := toFloat64(matches[2])
if err != nil {
return 0, err
}
days, err := toFloat64(matches[4])
if err != nil {
return 0, err
}
hours, err := toFloat64(matches[6])
if err != nil {
return 0, err
}
minutes, err := toFloat64(matches[8])
if err != nil {
return 0, err
}
seconds, err := toFloat64(matches[10])
if err != nil {
return 0, err
}
totalSeconds := seconds + 60*minutes + 3600*hours + 86400*days + 31536000*years
f := math.Round(totalSeconds)
return BigDuration(uint64(f)), nil
}
func (b BigDuration) Seconds() float64 {
return float64(b)
}
func toFloat64(s string) (float64, error) {
if s == "" {
return 0, nil
}
return strconv.ParseFloat(s, 64)
}