adamjstewart / fiscalyear

:calendar: Utilities for managing the fiscal calendar
MIT License
52 stars 13 forks source link

FiscalCalendar starting on 31st day of the month throws ValueError #8

Closed jmhansen closed 5 years ago

jmhansen commented 5 years ago

If the FiscalCalendar starts on the 31st of any month with 31 days, then FiscalDate().quarter throws a ValueError.

Example code:

from fiscalyear import FiscalDate, FiscalYear, fiscal_calendar

with fiscal_calendar(start_month=12, start_day=31):
    fiscal_date = FiscalDate(2019, 10, 22)
    fiscal_date.quarter

Traceback:

Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "/site-packages/fiscalyear.py", line 594, in quarter
    if self in q:
  File "/site-packages/fiscalyear.py", line 413, in __contains__
    return self.start.date() <= item <= self.end.date()
  File "/site-packages/fiscalyear.py", line 493, in end
    next_start = self.next_quarter.start
  File "/site-packages/fiscalyear.py", line 485, in start
    return FiscalDateTime(year, month, START_DAY, 0, 0, 0)
ValueError: day is out of range for month

The quarters generated are:

adamjstewart commented 5 years ago

Thanks for the bug report @jmhansen!

Are there any countries/businesses who actually use a fiscal calendar that starts on the 31st of any month, or is this just a theoretical bug? Either way I'm happy to fix it. What do you think the desired behavior should be? Should it choose the 30th of the month (or 28th if February), or the 1st of the next month?

jmhansen commented 5 years ago

@adamjstewart - this is definitely an edge case!

I don't know of a company that actually has a fiscal calendar start on the 31st, but we allow users to submit any date, and at least two different users have accidentally submitted the end of year as December 30th, making the 31st the start of their year.

(There is certainly more validation I can do to prevent this, but I do like that this package does aim to handle any fiscal calendar range.)

I think the desired outcome would be to have each quarter start on the last day of the month rather than the 1st of the next month. If someone wants a wonky fiscal calendar, they can have it!