-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
57 lines (44 loc) · 1.48 KB
/
test.py
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
from datetime import datetime
from croniter import croniter
import pytz
def get_current_date_time():
tz = pytz.timezone("Asia/Kolkata")
return datetime.now(tz)
def test_cron(start_cron, stop_cron, machine_state="stop"):
'''
find stop and start region for current 24 hour current day
'''
current_datetime = get_current_date_time()
start_cr = croniter(start_cron, ret_type=datetime, start_time=current_datetime)
end_cr = croniter(stop_cron, ret_type=datetime, start_time=current_datetime)
next_start = {
"time": start_cr.get_next(),
"type": "start"
}
previous_start = {
"time": start_cr.get_prev(),
"type": "start"
}
next_stop = {
"time": end_cr.get_next(),
"type": "stop"
}
previous_stop = {
"time": end_cr.get_prev(),
"type": "stop"
}
'''
find start region
'''
sorted_previous = sorted([previous_start, previous_stop], key=lambda k: k.get("time"))
sorted_next = sorted([next_start, next_stop], key=lambda k: k.get("time"))
print(sorted_previous[1].get("time"), sorted_previous[1].get("type"))
print(sorted_next[0].get("time"), sorted_next[0].get("type"))
last_state = sorted_previous[1].get("type")
next_state = sorted_next[0].get("type")
action = last_state
if action != machine_state:
print("perform action", action)
else:
print("no action needed")
test_cron("0 9 * * *", "0 21 * * *", "start")