Skip to content

Instantly share code, notes, and snippets.

@hybridjosto
Created February 21, 2014 14:12
Show Gist options
  • Save hybridjosto/9134964 to your computer and use it in GitHub Desktop.
Save hybridjosto/9134964 to your computer and use it in GitHub Desktop.
fiscal calendar functions
from datetime import *
# date, timedelta
week = {
0: 1, # remap so Sunday = 0
1: 2,
2: 3,
3: 4,
4: 5,
5: 6,
6: 0
}
year_now = date.today().year
def fiscYearStart(fiscYear=year_now):
"""
Returns the fiscal year start date for the
inputted year. Assumes start of week = sunday
"""
april = date(fiscYear, 4, 1)
aprilDay = april.weekday()
if aprilDay < 6:
subtractDays = week[aprilDay]
newApril = april - timedelta(days=subtractDays)
return newApril
else:
return april
# using cal type, work out month start dates
def fiscMonthStart(calendar=445, year=year_now):
"""
Returns a list of month start dates.
defaults to current year, 445 week orientation
"""
acceptedCals = [445, 454]
calType = [int(i) for i in list(str(calendar))] * 4
if calendar in acceptedCals:
fys = fiscYearStart(year)
monthStart = [fys]
for w in calType:
fys += timedelta(weeks=w)
monthStart.append(fys)
return monthStart
# fiscYearStart()
# fms = fiscMonthStart(calendar=454, year=2013)
# for d in fms:
# print d
print weekday(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment