forked from VadimIpatov/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
34 lines (31 loc) · 862 Bytes
/
common.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
package zabbix
import (
"regexp"
"strconv"
"github.com/pkg/errors"
)
var timeSuffixRegexp = regexp.MustCompile(`^(\d+)([smhdw])$`)
// getTime convert string with time (suffixes supported) to number of seconds
// https://www.zabbix.com/documentation/3.2/manual/config/triggers/suffixes
func getTime(tm string) (int, error) {
sec, err := strconv.Atoi(tm)
if err != nil {
m := timeSuffixRegexp.FindStringSubmatch(tm)
if len(m) == 3 {
val, err := strconv.Atoi(m[1])
if err != nil {
return 0, errors.New("Can't convert " + tm + " to seconds")
}
switch m[2] {
case "s": return val, nil
case "m": return val*60, nil
case "h": return val*3600, nil
case "d": return val*86400, nil
case "w": return val*604800, nil
}
return 0, errors.New("Can't convert " + tm + " to seconds")
}
return 0, nil
}
return sec, nil
}