I created this python code to create a python list with the financial year with quarters for a given range of dates for a given country format. It works with the USA, Australia, and Canada because all these countries' financial years start with 1 day each month. But in the UK it is the 6th of April of one particular year. So how do I solve this problem?
here is the full code of my custom function:
def to_financial_year(start_date:str=None,start_end:str=None,current_format:str=None, financial_year_format:str=None):
if current_format==None:
return print('Please include the start_date in function argument')
return
elif start_date==None:
return print('Please include the start_end in function argument')
return
elif start_end==None:
return print('Please include the current_format in function argument')
return
elif financial_year_format==None:
return print('Please include the financial_year_format in function argument')
return
else:
start_date = start_date
start_end=start_end
if financial_year_format=="Australia":
fiscalyear.START_MONTH = 7
elif financial_year_format=="USA":
fiscalyear.START_MONTH = 10
elif financial_year_format=="Canada":
fiscalyear.START_MONTH = 4
elif financial_year_format=="UK":
fiscalyear.START_MONTH = 4
else:
return print('Please enter valid financial_year_format country')
return
fiscalyear.START_MONTH = 7
try:
date_obj1 = dt.strptime(start_date, current_format)
except:
return print(
'Mismatch between in input start_date time and format that you entered or \n',sys.exc_info()[0],'error')
try:
date_obj2 = dt.strptime(start_end, current_format)
except:
return print(
'Mismatch between in input start_end time and format that you entered or \n',sys.exc_info()[0],'error')
list=[]
step = dd.timedelta(days=1)
while date_obj1 <= date_obj2:
day=int(dt.strftime(date_obj1,"%d"))
month=int(dt.strftime(date_obj1,"%m"))
year=int(dt.strftime(date_obj1,"%Y"))
jul_dt = fiscalyear.FiscalDate(year,month,day)
k=str(jul_dt.fiscal_year)+"-"+"Q"+str(jul_dt.fiscal_quarter)
list.append(k)
date_obj1 += step
print(list)