I want to split a period into several sub-periods of predefined size.
Here is an example:
Between 2021-04-11 15:03:00 and 2021-04-11 18:03:00, decompose into hours, quarter-hours and minutes. The expected result is (no specific order):
2021-04-11 15:03:00 (the minute)
2021-04-11 15:04:00 (the minute)
2021-04-11 15:05:00 (the minute)
2021-04-11 15:06:00 (the minute)
2021-04-11 15:07:00 (the minute)
2021-04-11 15:08:00 (the minute)
2021-04-11 15:09:00 (the minute)
2021-04-11 15:10:00 (the minute)
2021-04-11 15:11:00 (the minute)
2021-04-11 15:12:00 (the minute)
2021-04-11 15:13:00 (the minute)
2021-04-11 15:14:00 (the minute)
2021-04-11 15:15:00 (the quarter-hour)
2021-04-11 15:30:00 (the quarter-hour)
2021-04-11 15:45:00 (the quarter-hour)
2021-04-11 16:00:00 (the hour)
2021-04-11 17:00:00 (the hour)
2021-04-11 18:00:00 (the minute)
2021-04-11 18:01:00 (the minute)
2021-04-11 18:02:00 (the minute)
And my current code:
def ceil_dt(dt, delta):
return dt + (datetime.min - dt) % delta
def floor_dt(dt, delta):
return dt - (dt - datetime.min) % delta
def list_dt(start, end, subperiod, indice):
temp = start
min = ceil_dt(temp, timedelta(minutes=subperiod[indice]))
print(f'\nstart {start}')
print(f'end {end}')
print(f'subperiod {subperiod}')
print(f'indice {indice}')
print(f'min {min}')
while temp + timedelta(minutes=subperiod[indice]) <= floor_dt(end, timedelta(minutes=subperiod[indice])) :
print(f'result {ceil_dt(temp, timedelta(minutes=subperiod[indice]))}')
temp = temp + timedelta(minutes=subperiod[indice])
max = ceil_dt(temp, timedelta(minutes=subperiod[indice]))
print(f'max {max}')
if min != start:
print("other min")
indice = indice + 1
list_dt(start, min, subperiod, indice)
# if max != end:
# print("other max")
# indice = indice + 1
# list_dt(max, end, subperiod, indice)
subperiod = [60, 15, 1]
indice = 0
start = datetime(2021, 4, 11, 15, 3, 0)
end = datetime(2021, 4, 11, 18, 3, 0)
list_dt(start, end, subperiod, indice)
I can't find how to enter correctly in the min and max parts. I'm not sure if we should use recursive. Does anyone have an idea?