Create date intervals from number of hours

Question:

I am trying to create date intervals from the number of hours. Known variables are start_date, end_date and hour positions which are extracted from XML file. This is just one example as the time frame can also be one month with different hour positions and thus intervals.

Start_date = 2023-01-01
End_date = 2024-01-01

Pos1 = 1 (hour)
Pos2 = 2232
Pos3 = 2424
Pos4 = 6432
Pos5 = 6624

It should look something like this:

1h    - 2232h
2233h - 2424h
2425h - 6432h
6433h - 6624h
6625h - 8760h

…and when converted to date, like this:

01.01.2023 00:00 - 03.04.2023 00:00
04.04.2023 00:00 - 11.04.2023 00:00
12.04.2023 00:00 - 25.09.2023 00:00  
26.09.2023 00:00 - 03.10.2023 00:00
04.10.2023 00:00 - 01.01.2024 00:00

First I tried to get "date_from", and as for the "date_to" I really have no idea how to get it. I’m not even sure if my approach is good.

import datetime, timedelta
time_interval = ['2023-01-01T00:00Z','2024-01-01T00:00Z']
for per in xml_file.findall('Period'):
    for intv in per.findall('Interval'):
        pos = int(intv.get('v')) # here the output is the five-hour positions which I wrote above
        date_from = (datetime.fromisoformat(time_interval[0]) + timedelta(hours=pos)).strftime('%d.%m.%Y %H:%M')

Solution:

import datetime, timedelta

time_interval = ['2023-01-01T00:00Z','2024-01-01T00:00Z']
hour_positions = [1, 2232, 2424, 6432, 6624]

for i in range(len(hour_positions)):
    if i == len(hour_positions) - 1:
        # Last interval - go up to end_date
        start_date = datetime.fromisoformat(time_interval[0])
        end_date = datetime.fromisoformat(time_interval[1])
    else:
        # Here we calculate the end date based on the next hour position
        start_date = datetime.fromisoformat(time_interval[0])
        end_date = start_date + timedelta(hours=hour_positions[i+1]-1)
    # Calculate the start date based on the current hour position
    start_date = start_date + timedelta(hours=hour_positions[i])
    # Print the date interval
    print(start_date.strftime('%d.%m.%Y %H:%M') + ' - ' + end_date.strftime('%d.%m.%Y %H:%M'))
Asked By: BB-9

||

Answers:

You’re on the right track with your code so far! To calculate the "date_to" for each interval, you can use the difference between the hour positions. I configured your code which should give you an output similar to what you described.

Code:

import datetime, timedelta

# You can set your time interval here.
time_interval = ['2023-01-01T00:00Z','2024-01-01T00:00Z']
start_date = datetime.fromisoformat(time_interval[0])

# Get the hour positions from the XML file
hour_positions = [1, 2232, 2424, 6432, 6624]

for i in range(len(hour_positions)):
    if i == len(hour_positions) - 1:
        # Last interval - go up to end_date
        end_date = datetime.fromisoformat(time_interval[1])
    else:
        # Here we calculate the end date based on the next hour position
        end_date = start_date + timedelta(hours=hour_positions[i+1]-1)
    # Calculate the start date based on the current hour position
    start_date = start_date + timedelta(hours=hour_positions[i])
    # Print the date interval
    print(start_date.strftime('%d.%m.%Y %H:%M') + ' - ' + end_date.strftime('%d.%m.%Y %H:%M'))

Output:

01.01.2023 01:00 - 03.04.2023 02:00
03.04.2023 03:00 - 11.04.2023 01:00
11.04.2023 02:00 - 25.09.2023 00:00
25.09.2023 01:00 - 03.10.2023 00:00
03.10.2023 01:00 - 01.01.2024 00:00
Answered By: CRM000
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.