forked from vsjha18/nsetools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datemgr.py
104 lines (96 loc) · 3.26 KB
/
datemgr.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
92
93
94
95
96
97
98
99
100
101
102
103
104
import datetime as dt
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from dateutil import rrule
from nsetools.errors import DateFormatError
def get_nearest_business_day(d):
""" takes datetime object"""
if d.isoweekday() is 7 or d.isoweekday() is 6:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# republic day
elif d.month is 1 and d.day is 26:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# labour day
elif d.month is 5 and d.day is 1:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# independece day
elif d.month is 8 and d.day is 15:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# Gandhi Jayanti
elif d.month is 10 and d.day is 2:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# chirstmas
elif d.month is 12 and d.day is 25:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
else:
return d
def is_known_holiday(d):
"""accepts datetime/date object and returns boolean"""
if type(d) == dt.datetime:
d = d.date()
elif type(d) != dt.date:
raise DateFormatError("only date objects or datetime objects")
else:
# fine do nothing
pass
# declare the list of holidays here.
# republic day.
if d.month is 1 and d.day is 26:
return True
# labour day
elif d.month is 5 and d.day is 1:
d = d - relativedelta(days=1)
return get_nearest_business_day(d)
# independence day
elif d.month is 8 and d.day is 15:
return True
# gandhi jayanti
elif d.month is 10 and d.day is 2:
return True
# christmas
elif d.month is 12 and d.day is 25:
return True
else:
return False
def mkdate(d):
"""tries its best to return a valid date. it can accept pharse like today,
yesterday, day before yesterday etc.
"""
# check if the it is a string
return_date = ""
if type(d) is str:
if d == "today":
return_date = dt.date.today()
elif d == "yesterday":
return_date = dt.date.today() - relativedelta(days=1)
elif d == "day before yesterday":
return_date = dt.date.today() - relativedelta(days=2)
else:
return_date = parse(d, dayfirst=True).date()
elif type(d) == dt.datetime:
return_date = d.date()
elif type(d) == dt.date:
return d
else:
raise DateFormatError("wrong date format %s" % str(d))
# check if future date.
return return_date
def usable_date(d):
"""accepts fuzzy format and returns most sensible date"""
return get_nearest_business_day(mkdate(d))
def get_date_range(frm, to, skip_dates=[]):
"""accepts fuzzy format date and returns business adjusted date ranges"""
# for x in rrule.rrule(rrule.DAILY, dtstart=s, until=dt.datetime.now(), byweekday=[0, 1, 2, 3, 4]): print(x)
frm = usable_date(frm)
to = usable_date(to)
datelist = []
for date in rrule.rrule(rrule.DAILY, dtstart=frm, until=to, byweekday=[0, 1, 2, 3, 4]):
if not is_known_holiday(date):
datelist.append(date.date())
return datelist