Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating 15 minute time interval array in 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
like image 405
born2Learn Avatar asked Sep 02 '16 17:09

born2Learn


3 Answers

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' ... ]

like image 74
Charles Clayton Avatar answered Nov 20 '22 02:11

Charles Clayton


Looking at the data file, you should use the built in python date-time objects. followed by strftime to format your dates.

Broadly you can modify the code below to however many date-times you would like First create a starting date.

Today= datetime.datetime.today()

Replace 100 with whatever number of time intervals you want.

date_list = [Today + datetime.timedelta(minutes=15*x) for x in range(0, 100)]

Finally, format the list in the way that you would like, using code like that below.

datetext=[x.strftime('%Y-%m-%d T%H:%M Z') for x in date_list]
like image 40
zhqiat Avatar answered Nov 20 '22 02:11

zhqiat


Here is a Pandas solution:

import pandas as pd

l = (pd.DataFrame(columns=['NULL'],
                  index=pd.date_range('2016-09-02T17:30:00Z', '2016-09-04T21:00:00Z',
                                      freq='15T'))
       .between_time('07:00','21:00')
       .index.strftime('%Y-%m-%dT%H:%M:%SZ')
       .tolist()
)

Output:

In [165]: l
Out[165]:
['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',
 '2016-09-02T19:00:00Z',
 '2016-09-02T19:15:00Z',
 '2016-09-02T19:30:00Z',
 '2016-09-02T19:45:00Z',
 '2016-09-02T20:00:00Z',
 '2016-09-02T20:15:00Z',
 '2016-09-02T20:30:00Z',
 '2016-09-02T20:45:00Z',
 '2016-09-02T21:00:00Z',
 '2016-09-03T07:00:00Z',
 '2016-09-03T07:15:00Z',
 '2016-09-03T07:30:00Z',
 '2016-09-03T07:45:00Z',
 '2016-09-03T08:00:00Z',
 '2016-09-03T08:15:00Z',
 '2016-09-03T08:30:00Z',
 '2016-09-03T08:45:00Z',
 '2016-09-03T09:00:00Z',
 '2016-09-03T09:15:00Z',
 '2016-09-03T09:30:00Z',
 '2016-09-03T09:45:00Z',
 '2016-09-03T10:00:00Z',
 '2016-09-03T10:15:00Z',
 '2016-09-03T10:30:00Z',
 '2016-09-03T10:45:00Z',
 '2016-09-03T11:00:00Z',
 '2016-09-03T11:15:00Z',
 '2016-09-03T11:30:00Z',
 '2016-09-03T11:45:00Z',
 '2016-09-03T12:00:00Z',
 '2016-09-03T12:15:00Z',
 '2016-09-03T12:30:00Z',
 '2016-09-03T12:45:00Z',
 '2016-09-03T13:00:00Z',
 '2016-09-03T13:15:00Z',
 '2016-09-03T13:30:00Z',
 '2016-09-03T13:45:00Z',
 '2016-09-03T14:00:00Z',
 '2016-09-03T14:15:00Z',
 '2016-09-03T14:30:00Z',
 '2016-09-03T14:45:00Z',
 '2016-09-03T15:00:00Z',
 '2016-09-03T15:15:00Z',
 '2016-09-03T15:30:00Z',
 '2016-09-03T15:45:00Z',
 '2016-09-03T16:00:00Z',
 '2016-09-03T16:15:00Z',
 '2016-09-03T16:30:00Z',
 '2016-09-03T16:45:00Z',
 '2016-09-03T17:00:00Z',
 '2016-09-03T17:15:00Z',
 '2016-09-03T17:30:00Z',
 '2016-09-03T17:45:00Z',
 '2016-09-03T18:00:00Z',
 '2016-09-03T18:15:00Z',
 '2016-09-03T18:30:00Z',
 '2016-09-03T18:45:00Z',
 '2016-09-03T19:00:00Z',
 '2016-09-03T19:15:00Z',
 '2016-09-03T19:30:00Z',
 '2016-09-03T19:45:00Z',
 '2016-09-03T20:00:00Z',
 '2016-09-03T20:15:00Z',
 '2016-09-03T20:30:00Z',
 '2016-09-03T20:45:00Z',
 '2016-09-03T21:00:00Z',
 '2016-09-04T07:00:00Z',
 '2016-09-04T07:15:00Z',
 '2016-09-04T07:30:00Z',
 '2016-09-04T07:45:00Z',
 '2016-09-04T08:00:00Z',
 '2016-09-04T08:15:00Z',
 '2016-09-04T08:30:00Z',
 '2016-09-04T08:45:00Z',
 '2016-09-04T09:00:00Z',
 '2016-09-04T09:15:00Z',
 '2016-09-04T09:30:00Z',
 '2016-09-04T09:45:00Z',
 '2016-09-04T10:00:00Z',
 '2016-09-04T10:15:00Z',
 '2016-09-04T10:30:00Z',
 '2016-09-04T10:45:00Z',
 '2016-09-04T11:00:00Z',
 '2016-09-04T11:15:00Z',
 '2016-09-04T11:30:00Z',
 '2016-09-04T11:45:00Z',
 '2016-09-04T12:00:00Z',
 '2016-09-04T12:15:00Z',
 '2016-09-04T12:30:00Z',
 '2016-09-04T12:45:00Z',
 '2016-09-04T13:00:00Z',
 '2016-09-04T13:15:00Z',
 '2016-09-04T13:30:00Z',
 '2016-09-04T13:45:00Z',
 '2016-09-04T14:00:00Z',
 '2016-09-04T14:15:00Z',
 '2016-09-04T14:30:00Z',
 '2016-09-04T14:45:00Z',
 '2016-09-04T15:00:00Z',
 '2016-09-04T15:15:00Z',
 '2016-09-04T15:30:00Z',
 '2016-09-04T15:45:00Z',
 '2016-09-04T16:00:00Z',
 '2016-09-04T16:15:00Z',
 '2016-09-04T16:30:00Z',
 '2016-09-04T16:45:00Z',
 '2016-09-04T17:00:00Z',
 '2016-09-04T17:15:00Z',
 '2016-09-04T17:30:00Z',
 '2016-09-04T17:45:00Z',
 '2016-09-04T18:00:00Z',
 '2016-09-04T18:15:00Z',
 '2016-09-04T18:30:00Z',
 '2016-09-04T18:45:00Z',
 '2016-09-04T19:00:00Z',
 '2016-09-04T19:15:00Z',
 '2016-09-04T19:30:00Z',
 '2016-09-04T19:45:00Z',
 '2016-09-04T20:00:00Z',
 '2016-09-04T20:15:00Z',
 '2016-09-04T20:30:00Z',
 '2016-09-04T20:45:00Z',
 '2016-09-04T21:00:00Z']
like image 17
MaxU - stop WAR against UA Avatar answered Nov 20 '22 02:11

MaxU - stop WAR against UA