Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FiscalWeek and fiscal_week attribute to FiscalDate and FiscalDateTime #17

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ The start and end of each of the above objects are stored as instances of the ``
FiscalQuarter(2017, 4)
>>> e.fiscal_month
7
>>> e.fiscal_week
28
>>> e.fiscal_day
190

Expand Down
2 changes: 2 additions & 0 deletions docs/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ The start and end of each of the above objects are stored as instances of the ``
FiscalQuarter(2017, 4)
>>> e.fiscal_month
7
>>> e.fiscal_week
28
>>> e.fiscal_day
190

Expand Down
19 changes: 13 additions & 6 deletions fiscalyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,18 +1275,25 @@ def fiscal_month(self):
"""
return (self.month - FiscalYear(self.year).start.month) % 12 + 1

@property
def fiscal_week(self):
"""returns: The fiscal week
:rtype: int
"""
return -(-self.fiscal_day // 7)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamjstewart Let me know if you'd rather I just imported and used math.ceil


@property
def fiscal_day(self):
""":returns: The fiscal day
:rtype: int
"""
day_of_calendar_year = self.timetuple().tm_yday
fiscal_year = FiscalYear(self.fiscal_year)
beginning_of_fiscal_year = fiscal_year.start.timetuple().tm_yday
days_elapsed = day_of_calendar_year - beginning_of_fiscal_year + 1
if days_elapsed < 1:
days_elapsed += 365 if fiscal_year.isleap else 366
return days_elapsed
year_start = fiscal_year.start

if isinstance(self, FiscalDate):
year_start = year_start.date()

return (self - year_start).days + 1

@property
def prev_fiscal_year(self):
Expand Down
15 changes: 15 additions & 0 deletions test_fiscalyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,9 @@ def test_leap_year(self):
assert fiscalyear.FiscalDate(2016, 1, 1).fiscal_day == 93
assert fiscalyear.FiscalDate(2016, 2, 29).fiscal_day == 152
assert fiscalyear.FiscalDate(2017, 3, 1).fiscal_day == 152
assert fiscalyear.FiscalDate(2016, 9, 30).fiscal_day == 366
assert fiscalyear.FiscalDate(2017, 9, 30).fiscal_day == 365
assert fiscalyear.FiscalDate(2018, 9, 30).fiscal_day == 365

def test_contains(self, a, b, c, f):
assert b in c
Expand Down Expand Up @@ -1024,6 +1027,18 @@ def test_next_fiscal_quarter(self, a, c):
def test_prev_fiscal_month(self, a):
assert a.prev_fiscal_month == fiscalyear.FiscalMonth(2017, 3)

def test_fiscal_week(self):
a = fiscalyear.FiscalDay(2017, 1).start
b = fiscalyear.FiscalDay(2017, 7).start
assert a.fiscal_week == 1
assert b.fiscal_week == 1

c = fiscalyear.FiscalDay(2017, 8).start
assert c.fiscal_week == 2

d = fiscalyear.FiscalDay(2016, 366).start
assert d.fiscal_week == 53

def test_next_fiscal_month(self, a):
assert a.next_fiscal_month == fiscalyear.FiscalMonth(2017, 5)

Expand Down