forked from egen/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
44 lines (38 loc) · 817 Bytes
/
util.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
package main
import (
"fmt"
"regexp"
"strconv"
"time"
)
func duration(s string) (time.Duration, error) {
re := regexp.MustCompile(`^(\d+)([HhDdMmYy])$`)
if m := re.FindStringSubmatch(s); m != nil {
v, err := strconv.ParseUint(m[1], 10, 0)
if err != nil {
return 0, err
}
switch m[2] {
case "H", "h":
return time.Hour * time.Duration(v), nil
case "D", "d":
return time.Hour * time.Duration(24*v), nil
case "M", "m":
return time.Hour * time.Duration(24*30*v), nil
case "Y", "y":
return time.Hour * time.Duration(24*365*v), nil
}
}
return 0, fmt.Errorf("unrecognized time spec '%s'", s)
}
func uniq(l []string) []string {
seen := make(map[string] bool)
u := make([]string, 0)
for _, s := range l {
if !seen[s] {
u = append(u, s)
}
seen[s] = true
}
return u
}