-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
45 lines (37 loc) · 1.17 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
import calendar
import os
from datetime import datetime
def create_dir_if_not_exist(path):
try:
os.makedirs(path)
except:
pass
def get_expiry_date(year=None, month=None):
if not year or not month:
today = datetime.today()
month, year = today.month, today.year
cal_month = calendar.month(year, month).split("\n")
try:
last_thursday = cal_month[-2].strip().split(' ')[3]
except:
last_thursday = cal_month[-3].strip().split(' ')[3]
month_name = cal_month[0].strip().split(" ")[0][:3].upper()
expiry_date = "{}{}{}".format(last_thursday, month_name, year)
return expiry_date
def is_weekend():
today = datetime.today()
if calendar.weekday(today.year, today.month, today.day) >= 5:
# 5 and 6 are Saturday and Sunday.
return True
return False
def are_off_market_hours():
now = datetime.now()
if (now.hour == 15 and now.minute >= 35) or now.hour > 15 or now.hour < 9:
return True
return False
def is_market_closed():
return (is_weekend() or are_off_market_hours())
def get_home_directory():
return os.path.expanduser("~")
if __name__ == "__main__":
main()