-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaydatesidea.py
More file actions
78 lines (67 loc) · 3.73 KB
/
paydatesidea.py
File metadata and controls
78 lines (67 loc) · 3.73 KB
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
from datetime import datetime, date
from dateutil.relativedelta import relativedelta as rd
import calendar
def payTime(startDate, endDate, convention, period, format): #date_format = 'yyyy-mm-dd'
sDate = datetime.strptime(startDate, '%Y-%m-%d').date()
eDate = datetime.strptime(endDate, '%Y-%m-%d').date()
sDateTup = date.timetuple(sDate)
eDateTup = date.timetuple(eDate)
datesList = [eDate, sDate] # date array with start and end
endMonths = eDateTup[1] # end date month
if eDateTup[0] > sDateTup[0]: # determines no. of months between start and end dates
endMonths += 12*(eDateTup[0]-sDateTup[0]) # endMonths-sDateTup[1] is the actual value for no. of months
endYear = sDateTup[0] # sets year variable to start year
if 'monthly' in period:
stRange = sDateTup[1]
endRange = endMonths+1
if 'annually' in period:
stRange = endYear
endRange = eDateTup[0]+1
for i in range(stRange, endRange): # loops from start month to end month
endYear = date.timetuple(datesList[-1])[0]
periodIter = calendar.monthrange(endYear,date.timetuple(datesList[-1])[1])[1] # determines days in month i
# switch statements instead of repeated if
if period == 'annually': monthBreaks = [rd(years=1,days=-1), rd(years=1)]
if period == 'biannually': monthBreaks = [rd(months=6,days=-1), rd(months=6), rd(years=1,days=-1), rd(years=1)]
if period == 'monthly': monthBreaks = [rd(months=1,days=-1),rd(months=1)]
if period == 'bimonthly': monthBreaks = [rd(days=(periodIter/2)-1), rd(days=periodIter/2),rd(months=1,days=-1),rd(months=1)]
tempList = []
for j in range(len(monthBreaks)):
date_added = datesList[-1] + monthBreaks[j]
tempList.append(date_added)
datesList += tempList
tempDateList = []
for u in range(len(datesList)): # for date_iter in datesList:
if datesList[u] > sDate and datesList[u] < (eDate - rd(days=3)): # td is number of days new cycle cutoff
tempDateList.append(datesList[u])
# tempDateList.append(sDate)
# tempDateList.append(eDate)
if convention == 'following': # pushes date forward if it occurs on weekend
for u in range(len(tempDateList)):
eachDate = tempDateList[u].weekday()
if eachDate >= 5:
anotherTempValue = tempDateList[u] + rd(days=(7 - int(eachDate)))
if anotherTempValue in tempDateList: anotherTempValue += rd(days=1)
tempDateList[u] = anotherTempValue
elif convention == 'modified following': # pushes date forward if it occurs on weekend
for u in range(len(tempDateList)):
eachDate = tempDateList[u].weekday()
if date.timetuple(tempDateList[u])[2] == eDateTup[2] and eachDate >= 5:
anotherTempValue = tempDateList[u] + rd(days=(7 - int(eachDate)))
if date.timetuple(anotherTempValue)[1] != date.timetuple(tempDateList[u])[1]: anotherTempValue -= rd(days=3)
if anotherTempValue in tempDateList: anotherTempValue += rd(days=1)
tempDateList[u] = anotherTempValue
elif date.timetuple(tempDateList[u])[2] == sDateTup[2]: tempDateList[u] = tempDateList[u-1] + rd(days=1)
tempDateList.append(sDate)
tempDateList.append(eDate)
tempDateList.sort()
datesArr = []
while len(tempDateList) > 2: # breaks dates array into sets of two
datesArr.append(tempDateList[:2])
tempDateList = tempDateList[2:]
datesArr.append(tempDateList)
if format == 'string':
for i in range(len(datesArr)):
for j in range(len(datesArr[i])):
datesArr[i][j] = datesArr[i][j].strftime('%Y-%m-%d')
return datesArr