Reputation: 315
So I have a set of working times (shifts), i.e. a tuple of start and end points in datetime format. One person then is able to have multiple shifts on the same day:
from datetime import datetime
shift1_1 = datetime(year=2019, month=8, day=21, hour=8)
shift1_2 = datetime(year=2019, month=8, day=21, hour=11)
shift2_1 = datetime(year=2019, month=8, day=21, hour=12)
shift2_2 = datetime(year=2019, month=8, day=21, hour=17)
shift3_1 = datetime(year=2019, month=8, day=22, hour=8)
shift3_2 = datetime(year=2019, month=8, day=22, hour=10)
shift4_1 = datetime(year=2019, month=8, day=22, hour=12)
shift4_2 = datetime(year=2019, month=8, day=22, hour=15)
shift5_1 = datetime(year=2019, month=8, day=22, hour=17)
shift5_2 = datetime(year=2019, month=8, day=22, hour=19)
shifts = [(shift1_1,shift1_2), (shift2_1,shift2_2), (shift3_1,shift3_2), (shift4_1,shift4_2),(shift5_1,shift5_2)]
I would like to calculate the complement, so start and end points of non working hours.
How would one tackle this problem? As for right now, I did not find a practical method of calculation. Does anybody know an algorithm for this problem?
I would be grateful for any hint in the right direction.
Upvotes: 0
Views: 46
Reputation: 315
I figured it out with the help of the post of tituszban
I generate a list of days I have in my list of shifts. Then I loop over all my shifts and split them up into a list of single datetimes. For each day I then:
In the end I get a new array of tuples containing my off_times.
Upvotes: 0
Reputation: 5152
How about this:
off_times = []
for i in range(shifts - 1):
off_times.append((shifts[i][1], shifts[i + 1][0]))
Upvotes: 1