Python Forum

Full Version: Appropriate data-structure / design for business-day relations (week/month-wise)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I'm just trying to figure out "the best", i.e. cleanest and most appropriate way to do the following:

For a specified day, e.g. thursday, 22th of April 2021, I want to know / calculate: Is it the last working day of the current week, if not, what is the last working day of the current week? The same for whole month. And what's more: I want to know what was the last working day of the previous week (e.g. Friday, 16th of April) and the previous month (e.g. wednesday 31th of March).

As a basic condition, only Monday - Friday are working days, and on top of that, I have a list of days which I define as non-working days, which could be days during the week, or also at the end.

E.g. in case I define Friday [30th of April] and [Wednesday and Thursday 5th/6th of May] as non-working days, when I input [Tuesday 4th of May] into my algorithmn, it would say: [7th of May] is the last working day of the week and Thursday 29th of April was the last working day of the previous week.

What would be the "best" data-structure to handle this?
Is there a known algorithmn or even a suitable package which could be used?
In case no, I was wondering whether it makes sense to have some sort of pre-calculated list which are somehow interconnected, e.g. like a graph.

E.g. in a graph: Every day would be a node and be connected to the previous and the next working day nodes, it is connected to its working week (e.g. CW22), which, again is connected to its month. Every week and every month has a previous and a next week/month node.

By doing so, I would only have to search the node for a specific day, e.g. 22th of April, and from there, I could go to the week-Node the day belongs to, from there to the previous week-Node and from there to the last working day Node of that week-Node.

I'm just not clear whether I'm thinking way to overhead here!?

Thanks so much
no need of elaborate data structure. this could/should be done by working with datetime objects and with simple calculations. Of course the list of the additional non-working days will be in a data structure.
How can I get the last working day of a particular week or month using a customized list of non-working days without just iterating in reverse until I find the appropriate day?