-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
58 lines (51 loc) · 940 Bytes
/
time.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
package util
import (
"fmt"
"strconv"
"strings"
)
func RetentionSeconds(interval string) (uint32, error) {
unit, err := Unit(interval)
if err != nil {
return 0, err
}
secs, err := strconv.ParseUint(strings.ReplaceAll(interval, string(unit), ""), 10, 32)
if err != nil {
return 0, err
}
switch unit {
case 'y': // years
secs *= 365
secs *= 24
secs *= 60
secs *= 60
case 'M': // months
secs *= 31
secs *= 24
secs *= 60
secs *= 60
case 'd': // days
secs *= 24
secs *= 60
secs *= 60
case 'h': // hours
secs *= 60
secs *= 60
case 'm': // minutes
secs *= 60
default: // seconds
}
return uint32(secs), nil
}
func Unit(interval string) (rune, error) {
if len(interval) <= 1 {
return '0', fmt.Errorf("illegal interval")
}
r := rune(interval[len(interval)-1:][0])
switch r {
case 'y', 'M', 'd', 'h', 'm', 's':
return r, nil
default:
return '0', fmt.Errorf("unsupported unit")
}
}