/
helper.go
79 lines (67 loc) · 1.43 KB
/
helper.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
package expr
import (
"errors"
"strconv"
)
var errUnknownTimeUnits = errors.New("unknown time units")
// IntervalString converts a sign and string into a number of seconds
func IntervalString(s string, defaultSign int) (int32, error) {
sign := defaultSign
switch s[0] {
case '-':
sign = -1
s = s[1:]
case '+':
sign = 1
s = s[1:]
}
var totalInterval int32
for len(s) > 0 {
var j int
for j < len(s) && '0' <= s[j] && s[j] <= '9' {
j++
}
var offsetStr string
offsetStr, s = s[:j], s[j:]
j = 0
for j < len(s) && (s[j] < '0' || '9' < s[j]) {
j++
}
var unitStr string
unitStr, s = s[:j], s[j:]
var units int
switch unitStr {
case "s", "sec", "secs", "second", "seconds":
units = 1
case "min", "mins", "minute", "minutes":
units = 60
case "h", "hour", "hours":
units = 60 * 60
case "d", "day", "days":
units = 24 * 60 * 60
case "w", "week", "weeks":
units = 7 * 24 * 60 * 60
case "mon", "month", "months":
units = 30 * 24 * 60 * 60
case "y", "year", "years":
units = 365 * 24 * 60 * 60
default:
return 0, errUnknownTimeUnits
}
offset, err := strconv.Atoi(offsetStr)
if err != nil {
return 0, err
}
totalInterval += int32(sign * offset * units)
}
return totalInterval, nil
}
func TruthyBool(s string) bool {
switch s {
case "", "0", "false", "False", "no", "No":
return false
case "1", "true", "True", "yes", "Yes":
return true
}
return false
}