adamjstewart / fiscalyear

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

Syntactic sugar for getting the current Fiscal{Year,Quarter} #2

Closed pmav99 closed 5 years ago

pmav99 commented 6 years ago

I think it would be nice if there was an easy way to get the current FiscalYear/FiscalQuarter.

This could easily be achieved by using a couple of classmethods. E.g.:

$ git diff
diff --git a/fiscalyear.py b/fiscalyear.py
index c6f8ddc..f7213d7 100644
--- a/fiscalyear.py
+++ b/fiscalyear.py
@@ -191,6 +191,11 @@ class FiscalYear(object):
         self._fiscal_year = fiscal_year
         return self

+    @classmethod
+    def current(cls):
+        today = datetime.date.today()
+        return cls(today.year)
+
     def __repr__(self):
         """Convert to formal string, for repr().

@@ -371,6 +376,11 @@ class FiscalQuarter(object):
         self._quarter = quarter
         return self

+    @classmethod
+    def current(cls):
+        today = datetime.date.today()
+        return cls(today.year, (today.month // 3) + 1)
+
     def __repr__(self):
         """Convert to formal string, for repr().

This way you could write:

In [3]: FiscalYear.current()
Out[3]: FiscalYear(2018)

In [4]: FiscalQuarter.current()
Out[4]: FiscalQuarter(2018, 1)

If you want, I can try to make a pull request.

adamjstewart commented 6 years ago

I like it! However, there seem to be a few bugs in your implementation. If today's date is October 1st, 2018, the current fiscal year is FiscalYear(2019). It might be better to use:

@classmethod
def current(cls):
    today = FiscalDate.today()
    return cls(today.fiscal_year)

and similarly for FiscalQuarter.

I'll have to think about whether we should call it current or today. current makes more sense, but today is more in line with the datetime module.

I also toyed around with the idea of having FiscalDate.today().fiscal_year return a FiscalYear object instead of an int, but I figured the current implementation would be more useful.

Pull requests are always welcome! Just make sure to write a good docstring and unit tests. Hopefully the unit testing framework is pretty simple to understand, but let me know if you have any questions.

pmav99 commented 6 years ago

You are right about the bug. I am not in the US so I was thinking about Fiscal Years starting on 1st of January... :P

I will make a pull request.