-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
55 lines (43 loc) · 902 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
package wlbdqm
import (
"errors"
"fmt"
"regexp"
"strconv"
)
var (
intervalRegex = regexp.MustCompile(`(\d+)([dhms])`)
)
type intervalUnit string
const (
intervalUnitD intervalUnit = "d"
intervalUnitH intervalUnit = "h"
intervalUnitM intervalUnit = "m"
intervalUnitS intervalUnit = "s"
)
type Interval struct {
t float64
unit intervalUnit
}
func (i Interval) String() string {
return fmt.Sprintf("%v%v", i.t, i.unit)
}
func (i Interval) ToSpec() string {
return "@every " + i.String()
}
func IntervalFromString(s string) (*Interval, error) {
if !intervalRegex.MatchString(s) {
return nil, errors.New("interval format error")
}
ss := intervalRegex.FindAllStringSubmatch(s, -1)
DebugPrintln(ss)
t, err := strconv.ParseFloat(ss[0][1], 64)
if err != nil {
return nil, err
}
interval := &Interval{
t: t,
unit: intervalUnit(ss[0][2]),
}
return interval, nil
}