forked from shiftsayan/plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
59 lines (48 loc) · 1.59 KB
/
util.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
from enum import IntEnum
import datetime
class Day(IntEnum):
TODAY = datetime.datetime.now().weekday()
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6
def convert_StrtoDate(str_date):
# we check what the string input of the user is
# and then we map each of those strings to the
# enumerated variant
if str_date == 'Monday':
return Day.MONDAY
elif str_date == 'Tuesday':
return Day.TUESDAY
elif str_date == 'Wednesday':
return Day.WEDNESDAY
elif str_date == 'Thursday':
return Day.THURSDAY
elif str_date == 'Friday':
return Day.FRIDAY
elif str_date == 'Saturday':
return Day.SATURDAY
else:
return Day.SUNDAY
def format_datetime(dt):
'''
Returns date in the format `Friday, 1 March`
'''
return dt.strftime("%A, %-d %B")
def get_next_datetime(day, hour=0):
'''
Takes in a day and an hour and returns the soonest possible datetime that is on the day and after the hour
'''
dt = datetime.datetime.now()
# If after hour on day, return current time (with a 1 minute buffer)
if dt.weekday() == day and dt.hour >= hour:
return datetime.datetime.now() + datetime.timedelta(minutes=1)
# Otherwise, increment date to required day and hour
dt += datetime.timedelta(days=(day-dt.weekday()) % 7)
return datetime.datetime(dt.year, dt.month, dt.day, hour, 0, 0, 0)
def get_next_datetime_formatted(day, hour=0):
next_dt = get_next_datetime(day, hour)
return format_datetime(next_dt)