This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
91 lines (68 loc) · 2.46 KB
/
utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from datetime import datetime, date, timedelta
import dateutil
import sys
import calendar
import math
from dateutil.rrule import rrule, MONTHLY
def find_start_date(month, year, numWeek, weekday, startHour, startMinute):
cal = calendar.monthcalendar(year, month)
if(cal[0][weekday] == 0):
numWeek += 1
startDate = cal[numWeek][weekday]
return datetime(year, month, startDate, startHour, startMinute)
def list_all_dates(month, year, numWeek, weekday, startHour, startMinute):
startDate = find_start_date(month, year, numWeek, weekday, startHour, startMinute)
# if the start of the recurrance occurs before today's date, start at the next month
if(startDate < datetime.now()):
if(startDate.month == 12):
startDate = find_start_date(1, year, numWeek, weekday, startHour, startMinute)
else:
startDate = find_start_date(month + 1, year, numWeek, weekday, startHour, startMinute)
# scheduled messages can only be scheduled out 120 days
endDate = startDate + timedelta(days=120)
monthlist = ((startDate.month, startDate.year) for startDate in rrule(MONTHLY, dtstart=startDate, until=endDate))
dates = []
for months in monthlist:
dates.append(find_start_date(months[0], months[1], numWeek, weekday, startHour, startMinute).strftime("%s"))
return dates
def parse_command(command):
possible_occurance = {
"first": 0,
"second": 1,
"third": 2,
"fourth": 3
}
possible_days = {
'Monday': 0,
'Tuesday': 1,
'Wednesday': 2,
'Thursday': 3,
'Friday': 4,
'Saturday': 5,
'Sunday': 6
}
command = command.split(' ')
if(command[0] not in possible_occurance.keys()):
return command[0] + " is not a valid start occurance"
if(command[1] not in possible_days.keys()):
return command[1] + " is not a valid start date"
currentYear = datetime.now().year
currentMonth = datetime.now().month
startWeek = possible_occurance[command[0]]
startWeekDay = possible_days[command[1]]
time = command[3]
mode = time[-2:]
if(len(time) == 6):
startHour = int(time[0])
startMinute = int(time[2:4])
else:
startHour = int(time[:-5])
startMinute = int(time[3:5])
if startHour < 12 and mode == 'pm':
startHour += 12
if startHour == 12 and mode == 'am':
startHour = 0
return list_all_dates(currentMonth, currentYear, startWeek, startWeekDay, startHour, startMinute)
# if __name__ == "__main__":
# command = "fourth Thursday at 5:30am"
# print(parse_command(command))