Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Created February 2, 2024 18:02
Show Gist options
  • Select an option

  • Save cbaragao/046cf3253691d47a803e69942d18b617 to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/046cf3253691d47a803e69942d18b617 to your computer and use it in GitHub Desktop.
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
class DateGenerator:
def __init__(self):
self.fiscal_months = {1:10,2:11,3:12,4:1,5:2,6:3,7:4,8:5,9:6,10:7,11:8,12:9}
self.current_month = date.today().month
self.current_year = date.today().year
self.current_date = date.today()
def check_fiscal_year(self,fy):
if fy == {}:
return self.fiscal_months
else:
return fy
def get_key_by_value(self, dict, val):
for key, value in dict.items():
if value == val:
return key
def get_fiscal_year(self, offset = 0, fiscal = {}):
check_fiscal = self.check_fiscal_year(fiscal)
if self.current_month > check_fiscal[12]:
return self.current_year + 1 + offset
else:
return self.current_year + offset
def get_fiscal_year_start(self,offset=0, fiscal = {}):
check_fiscal = self.check_fiscal_year(fiscal)
start_month = check_fiscal[1]
current = self.get_fiscal_year(offset, fiscal= check_fiscal)
if start_month > self.current_month:
start = date(year=current-1, month=start_month, day=1)
else:
start = date(year=current, month=start_month, day=1)
return start
def get_fiscal_quarter_start(self, offset=0, fiscal = {}):
check_fiscal = self.check_fiscal_year(fiscal)
m = self.current_month
fy_month= self.get_key_by_value(check_fiscal, m)
if fy_month in (1,4,7,10):
fq_start_offset = 0
elif fy_month in (2,5,8,11):
fq_start_offset = -1
else:
fq_start_offset = - 2
start = date(year=self.current_year, month=m, day=1) + relativedelta(months = fq_start_offset)
if offset != 0:
months_offset = offset * 3
fq = start + relativedelta(months=months_offset)
else:
fq = start
return fq
def get_date(self, offset=0):
self.current_date
if offset == 0:
return self.current_date
else:
return_date = self.current_date + timedelta(days=offset)
return return_date
def get_weekday(self, date, day=0, offset=0):
days = (day - date.weekday() + 7) % 7
val = date + timedelta(weeks=offset,days=days)
return val
def get_fiscal_month(self,offset=0, fiscal={}):
check_fiscal = self.check_fiscal_year(fiscal)
target_month = self.current_month + offset
val = self.get_key_by_value(check_fiscal, target_month)
return val
def get_dynamic_fiscal_year(self, start_month):
month = start_month
fiscal_dict = {}
for i in range(12):
fiscal_dict[i+1] = month
if month != 12:
month += 1
else:
month = 1
return fiscal_dict
def get_end_of_month(self, eom_date, offset=0):
end_of_month = eom_date + relativedelta(months=offset,day=31)
return end_of_month
def get_start_of_month(self, start_of_month_date, offset=0):
start_of_month = start_of_month_date + relativedelta(months=offset,day=1)
return start_of_month
def date_diff(self, start_date, end_date):
delta = abs(end_date-start_date)
return delta.days
def date_add(self, start_date, num_units, units='d'):
time_units = units.lower()
match time_units:
case 'd':
return start_date + relativedelta(days=num_units)
case 'm':
return start_date + relativedelta(months=num_units)
case 'w':
return start_date + relativedelta(weeks=num_units)
case 'y':
return start_date + relativedelta(years=num_units)
def get_dates_in_range(self, start, end):
diff = self.date_diff(start_date=start, end_date=end)
results = []
for day in range(diff):
date = (start + timedelta(days=day)).isoformat()
results.append(date)
return results
def get_dates_mtd(self):
start_date = self.get_start_of_month(self.current_date)
end_date = self.current_date
res = self.get_dates_in_range(start=start_date, end=end_date)
return res
def get_dates_fytd(self, fiscal={}):
check_fiscal = self.check_fiscal_year(fiscal)
start_date = self.get_fiscal_year_start(fiscal=check_fiscal)
end_date = self.current_date
res = self.get_dates_in_range(start=start_date, end=end_date)
return res
#def get_dates_fqtd(self, fiscal={}):
d1 = DateGenerator()
test = d1.get_dates_fytd()
print(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment