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>I'll provide a solution that does <em>not</em> handle timezones, since the problem is generating dates and times and you can set the timezone afterwards however you want.</p>

<p>You have a starting date and starting  and ending time (for each day), plus an interval (in minutes) for these datetimes. The idea is to create a <code>timedelta</code> object that represent the time interval and repeatedly update the datetime until we reach the ending time, then we advance by one day and reset the time to the initial one and repeat.</p>

<p>A simple implementation could be:</p>

<pre><code>def make_dates(start_date, number_of_days, start_time, end_time, interval, timezone):
    if isinstance(start_date, datetime.datetime):
        start_date = start_date.date()
    start_date = datetime.datetime.combine(start_date, start_time)
    cur_date = start_date
    num_days_passed = 0
    step = datetime.timedelta(seconds=interval*60)

    while True:
        new_date = cur_date + step
        if new_date.time() &gt; end_time:
            num_days_passed += 1
            if num_days_passed &gt; number_of_days:
                break
            new_date = start_date + datetime.timedelta(days=num_days_passed)
        ret_date, cur_date = cur_date, new_date
        yield ret_date

In [31]: generator = make_dates(datetime.datetime.now(), 3, datetime.time(hour=17), datetime.time(hour=19), 15, None)

In [32]: next(generator)
Out[32]: datetime.datetime(2016, 9, 2, 17, 0)

In [33]: next(generator)
Out[33]: datetime.datetime(2016, 9, 2, 17, 15)

In [34]: list(generator)
Out[34]: 
[datetime.datetime(2016, 9, 2, 17, 30),
 datetime.datetime(2016, 9, 2, 17, 45),
 datetime.datetime(2016, 9, 2, 18, 0),
 datetime.datetime(2016, 9, 2, 18, 15),
 datetime.datetime(2016, 9, 2, 18, 30),
 datetime.datetime(2016, 9, 2, 18, 45),
 datetime.datetime(2016, 9, 2, 19, 0),
 datetime.datetime(2016, 9, 3, 17, 0),
 datetime.datetime(2016, 9, 3, 17, 15),
 datetime.datetime(2016, 9, 3, 17, 30),
 datetime.datetime(2016, 9, 3, 17, 45),
 datetime.datetime(2016, 9, 3, 18, 0),
 datetime.datetime(2016, 9, 3, 18, 15),
 datetime.datetime(2016, 9, 3, 18, 30),
 datetime.datetime(2016, 9, 3, 18, 45),
 datetime.datetime(2016, 9, 3, 19, 0),
 datetime.datetime(2016, 9, 4, 17, 0),
 datetime.datetime(2016, 9, 4, 17, 15),
 datetime.datetime(2016, 9, 4, 17, 30),
 datetime.datetime(2016, 9, 4, 17, 45),
 datetime.datetime(2016, 9, 4, 18, 0),
 datetime.datetime(2016, 9, 4, 18, 15),
 datetime.datetime(2016, 9, 4, 18, 30),
 datetime.datetime(2016, 9, 4, 18, 45),
 datetime.datetime(2016, 9, 4, 19, 0),
 datetime.datetime(2016, 9, 5, 17, 0),
 datetime.datetime(2016, 9, 5, 17, 15),
 datetime.datetime(2016, 9, 5, 17, 30),
 datetime.datetime(2016, 9, 5, 17, 45),
 datetime.datetime(2016, 9, 5, 18, 0),
 datetime.datetime(2016, 9, 5, 18, 15),
 datetime.datetime(2016, 9, 5, 18, 30),
 datetime.datetime(2016, 9, 5, 18, 45)]
</code></pre>

<p>Once you have the datetimes you can use the <a href="https://docs.python.org/3.5/library/datetime.html#datetime.datetime.strftime" rel="nofollow"><code>strftime</code></a> method to convert them to strings.</p>

python arrays python-2.7