Generating 15 minute time interval array in python
<p>I am new to python. I am trying to generate time interval array. for example:
<code>
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"]
</code>
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</p>

<p>Here is an example using an arbitrary date time</p>

<pre><code>from datetime import datetime

start = datetime(1900,1,1,0,0,0)
end = datetime(1900,1,2,0,0,0)
</code></pre>

<p>Now you need to get the timedelta (the difference between two dates or times.) between the <code>start</code> and <code>end</code></p>

<pre><code>seconds = (end - start).total_seconds()
</code></pre>

<p>Define the 15 minutes interval</p>

<pre><code>from datetime import timedelta

step = timedelta(minutes=15)
</code></pre>

<p>Iterate over the range of seconds, with step of time delta of 15 minutes (900 seconds) and sum it to <code>start</code>.</p>

<pre><code>array = []
for i in range(0, int(seconds), int(step.total_seconds())):
    array.append(start + timedelta(seconds=i))

print array
[datetime.datetime(1900, 1, 1, 0, 0),
datetime.datetime(1900, 1, 1, 0, 15),
datetime.datetime(1900, 1, 1, 0, 30),
datetime.datetime(1900, 1, 1, 0, 45),
datetime.datetime(1900, 1, 1, 1, 0),
...
</code></pre>

<p>At the end you can format the datetime objects to str representation.</p>

<pre><code>array = [i.strftime('%Y-%m-%d %H:%M%:%S') for i in array]

print array
['1900-01-01 00:00:00',
 '1900-01-01 00:15:00',
 '1900-01-01 00:30:00',
 '1900-01-01 00:45:00',
 '1900-01-01 01:00:00',
...
</code></pre>

<p>You can format datetime object at first iteration. But it may hurt your eyes</p>

<pre><code>array.append((start + timedelta(seconds=i)).strftime('%Y-%m-%d %H:%M%:%S'))
</code></pre>

python arrays python-2.7