in_learning_software in_learning_software - 1 month ago 9
Python Question

Generating 15 minute time interval array in python

I am new to python. I am trying to generate time interval array. for example:


time_array =
["2016-09-02T17:30:00Z", "2016-09-02T17:45:00Z", "2016-09-02T18:00:00Z", "2016-09-02T18:15:00Z", "2016-09-02T18:30:00Z", "2016-09-02T18:45:00Z"]

1. It should create the element like above in zulu time till 9 pm everyday.
2. Should generate the elements for next and day after next as well
3. Start time from 7:00 am - Ed time 9:00 pm,
if current_time is > start_time then generate 15 min time interval array till 9 pm. and then generate for next day and day + 2.
And Interval should be 7:00, 7:15 like that.. not in 7:12, 8:32

Answer

Here's a generic datetime_range for you to use.

Code

from datetime import datetime, timedelta

def datetime_range(start, end, delta):
    current = start
    while current < end:
        yield current
        current += delta

dts = [dt.strftime('%Y-%m-%d T%H:%M Z') for dt in 
       datetime_range(datetime(2016, 9, 1, 7), datetime(2016, 9, 1, 9+12), 
       timedelta(minutes=15))]

print(dts)

Output

['2016-09-01 T07:00 Z', '2016-09-01 T07:15 Z', '2016-09-01 T07:30 Z', '2016-09-01 T07:45 Z', '2016-09-01 T08:00 Z', '2016-09-01 T08:15 Z', '2016-09-01 T08:30 Z', '2016-09-01 T08:45 Z', '2016-09-01 T09:00 Z', '2016-09-01 T09:15 Z', '2016-09-01 T09:30 Z', '2016-09-01 T09:45 Z' ... ]

Comments