I've been through every question and every third party library trying to figure out a way to do this where I don't have to manually map dates.
I'm trying to get the week number of the current fiscal quarter. Each quarter starts on the 1st of either January, April, July or October.
Given a date (string or object, it doesn't matter), I need to be able to calculate the week number of the fiscal quarter that it's in.
To make matters a little more complicated, the Fiscal year starts in April.
So for example, today, July 9th 2020 is week 2 of this Fiscal quarter (Q2), because the quarter starts in April. Similarly, the 29 and 30th of June 2020 are week 14 of quarter 1.
Most time formatting libraries and even the standard library ones have methods like ISO date where I can extract the week number fine. But it's the week number from the 1st day of the year.
I can't use arithmetic to simply remove the number of weeks to the current date as there are a different number of weeks in each quarter. Quarters can have 12, 13 or 14 weeks depending on the year.
The closest I've gotten is using the FiscalYear library which is great as it has a Fiscal Quarter class with it. Unfortunately, the inherited method isoformat() doesn't apply to it. Only the FiscalDate class, which doesn't give me the quarter which I need.
Has anyone run into this? Can someone point me in the right direction?
I'd post code snippets but it's just the 100 ways there are in Python to get the current week number (as of today, that's 28).
I've tried using rrules and deltas in dateutils but the closest I can get is the week number of the 1st quarter using offsets. The second quarter, it falls apart.
I'm happy to use pandas or any other 3rd party library if it will help me avoid hard coding the quarter dates or, god forbid, the week number to dates mappings.
Any help in the right direction would be very much appreciated.
Edit: All three answers below solved this issue for me in different ways. I struggled with which one to give the correct answer to, but I gave it to @Paul's answer as it was the one that I could follow most as someone who isn't a senior. It was also the answer that fit in with my personal use case (that I didn't mention), which was receiving a datetime object and getting the results. So that gave it the edge. Sorry to the others who provided amazing answers. I'm thrilled to have gotten the code what all I was hoping for was a nudge in the right direction. Thank you all.