0

I am converting date timeline into smaller date time segments based on the frequency in minutes from start time to end time. Input:

start_time = '2022-11-20-09:48:00'
last_time = '2022-11-20-08:48:00'
frequency = 300 # seconds so it is 5 minutes

what I tried so far

from datetime import datetime

def time_divider(s_time, e_time, frequency):
    st_obj = datetime.strptime(s_time, '%Y-%m-%d-%H:%M:%S')
    start_range = st_obj.timestamp()
    end_range = datetime.strptime(e_time, '%Y-%m-%d-%H:%M:%S').timestamp()
    date_segments = []
    a = int(start_range)
    b = int(end_range+1)
    for i in range(a, b+1):
        date_segments.append({datetime.fromtimestamp(i):  datetime.fromtimestamp(i + frequency)})
        i = i + frequency
    return date_segments

date_s = time_divider(start_time, last_time, 300)
print(date_s)

current output:

[]

expected output and the output should come in this sequence

{datetime.datetime(2022, 11, 20, 9, 48): datetime.datetime(2022, 11, 20, 9, 52),datetime.datetime(2022, 11, 20, 9, 53): datetime.datetime(2022, 11, 20, 9, 57), datetime.datetime(2022, 11, 20, 9, 57): datetime.datetime(2022, 11, 20, 10, 02 ), ...}
Saad Hasan
  • 162
  • 7
  • Is there a reason you wouldn't use something like `pandas` here, since it has the tools built in to perform these types of operations?? – Grismar Nov 28 '22 at 05:16
  • Would any of these solutions work for you? [Generate a list of datetimes between an interval](https://stackoverflow.com/q/10688006/13843268). – sj95126 Nov 28 '22 at 05:18
  • @Grismar I am developing an automation module to divide the user defined start time and end time into segments and then call the API instead of calling the whole range as there is a limit to use get an API for the specific application I am working on. So, pandas would be an extra step to do it as my main intentions was once I have the specific ranges instead of create a dictionary , I will directly call the API. – Saad Hasan Nov 28 '22 at 05:25
  • I noticed that you have given start_time and last_time, but the last_time is less than start_time so basically it would never go into the for loop. Let me know if I am correct or that is what you intended for. – Prachi Patel Nov 28 '22 at 10:27

1 Answers1

2

start_time should be less than end_time

like:

start_time = '2022-11-20-08:48:00'

last_time = '2022-11-20-09:48:00'

and if we hadd 5 minutes in 48 it would become 53 not 52 as you have mentioned in expected output.

Here is the solution which might solve the problem.

from datetime import datetime, timedelta


start_time = '2022-11-20-08:48:00'
last_time = '2022-11-20-09:48:00'
frequency = 300  # seconds so it is 5 minutes


def time_divider(s_time, e_time, frequency):
    st_obj = datetime.strptime(s_time, '%Y-%m-%d-%H:%M:%S')
    end_range = datetime.strptime(e_time, '%Y-%m-%d-%H:%M:%S')
    date_segments = []
    while st_obj <= end_range:
        old_st_obj = st_obj
        st_obj = st_obj + timedelta(seconds=frequency)
        date_segments.append({old_st_obj: st_obj})
    return date_segments


date_s = time_divider(start_time, last_time, 300)
print(date_s)
Usman Arshad
  • 537
  • 11