-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
66 lines (62 loc) · 1.68 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cronjob
import (
"strconv"
"strings"
"time"
)
/*
IsCronTime
curTime: current time
schedue: crontab expression: seconds/minutes/hours/day/week/month
Determine whether the current time meet crontab expression
*/
func IsCronTime(curTime time.Time, schedue string) bool {
curSec := curTime.Second()
curMin := curTime.Minute()
curHour := curTime.Hour()
curDay := curTime.Day()
curMonth := int(curTime.Month())
curWeekday := int(curTime.Weekday())
schedule := strings.Split(schedue, " ")
if !CheckCronKey(schedule[0], curSec) || !CheckCronKey(schedule[1], curMin) ||
!CheckCronKey(schedule[2], curHour) || !CheckCronKey(schedule[3], curDay) ||
!CheckCronKey(schedule[4], curMonth) || !CheckCronKey(schedule[5], curWeekday) {
return false
}
return true
}
/*
CheckCronKey
schedule: crontab Expression of split corresponding seconds/minutes/hours/day/week/month
curTime: The current time of seconds/minutes/hours/day/week/month
Determine whether the current time a crontab time format
*/
func CheckCronKey(schedule string, curTime int) bool {
if strings.Contains(schedule, "/") {
scheduleSlice := strings.Split(schedule, "/")
scheInt, err := strconv.Atoi(scheduleSlice[1])
if err != nil {
return false
}
if curTime%scheInt != 0 {
return false
}
schedule = scheduleSlice[0]
}
if strings.Contains(schedule, "-") {
scheduleSlice := strings.Split(schedule, "-")
min, err1 := strconv.Atoi(scheduleSlice[0])
max, err2 := strconv.Atoi(scheduleSlice[1])
if err1 != nil || err2 != nil {
return false
}
if curTime < min || curTime > max {
return false
}
return true
}
if schedule != "*" && schedule != strconv.Itoa(curTime) {
return false
}
return true
}