-
Notifications
You must be signed in to change notification settings - Fork 3
/
durations.go
109 lines (89 loc) · 2.39 KB
/
durations.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package fisk
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
var (
durationMatcher = regexp.MustCompile(`([-+]?)(([\d\.]+)([a-zA-Z]+))`)
errInvalidDuration = fmt.Errorf("invalid duration")
)
// ParseDuration parse durations with additional units over those from
// standard go parser.
//
// In addition to normal go parser time units it also supports
// these.
//
// The reason these are not in go standard lib is due to precision around
// how many days in a month and about leap years and leap seconds. This
// function does nothing to try and correct for those.
//
// * "w", "W" - a week based on 7 days of exactly 24 hours
// * "d", "D" - a day based on 24 hours
// * "M" - a month made of 30 days of 24 hours
// * "y", "Y" - a year made of 365 days of 24 hours each
//
// Valid duration strings can be -1y1d1µs
func ParseDuration(d string) (time.Duration, error) {
// golang time.ParseDuration has a special case for 0
if d == "0" {
return 0 * time.Second, nil
}
var (
r time.Duration
neg = 1
)
d = strings.TrimSpace(d)
if len(d) == 0 {
return r, errInvalidDuration
}
parts := durationMatcher.FindAllStringSubmatch(d, -1)
if len(parts) == 0 {
return r, errInvalidDuration
}
for i, p := range parts {
if len(p) != 5 {
return 0, errInvalidDuration
}
if i == 0 && p[1] == "-" {
neg = -1
}
switch p[4] {
case "w", "W":
val, err := strconv.ParseFloat(p[3], 32)
if err != nil {
return 0, fmt.Errorf("%w: %v", errInvalidDuration, err)
}
r += time.Duration(val*7*24) * time.Hour
case "d", "D":
val, err := strconv.ParseFloat(p[3], 32)
if err != nil {
return 0, fmt.Errorf("%w: %v", errInvalidDuration, err)
}
r += time.Duration(val*24) * time.Hour
case "M":
val, err := strconv.ParseFloat(p[3], 32)
if err != nil {
return 0, fmt.Errorf("%w: %v", errInvalidDuration, err)
}
r += time.Duration(val*24*30) * time.Hour
case "Y", "y":
val, err := strconv.ParseFloat(p[3], 32)
if err != nil {
return 0, fmt.Errorf("%w: %v", errInvalidDuration, err)
}
r += time.Duration(val*24*365) * time.Hour
case "ns", "us", "µs", "ms", "s", "m", "h":
dur, err := time.ParseDuration(p[2])
if err != nil {
return 0, fmt.Errorf("%w: %v", errInvalidDuration, err)
}
r += dur
default:
return 0, fmt.Errorf("%w: invalid unit %v", errInvalidDuration, p[4])
}
}
return time.Duration(neg) * r, nil
}