在python中生成15分钟的时间间隔数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39298054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:06:06  来源:igfitidea点击:

Generating 15 minute time interval array in python

pythonarrayspython-2.7

提问by born2Learn

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
  1. 它应该在每天晚上 9 点之前在祖鲁时间创建像上面这样的元素。
  2. 也应该为 next 和 day after next 生成元素
  3. 开始时间从 7:00 am - Ed 时间 9:00 pm,如果 current_time > start_time 然后生成 15 分钟时间间隔数组,直到 9 pm。然后为第二天和第二天 + 2 生成。间隔应该是 7:00、7:15 那样......而不是 7:12、8:32

回答by Charles Clayton

Here's a generic datetime_rangefor you to use.

这是一个通用的datetime_range供您使用。

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

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

回答by zhqiat

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

查看数据文件,您应该使用内置的 python 日期时间对象。其次是strftime格式化您的日期。

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.

将 100 替换为您想要的任意数量的时间间隔。

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]

回答by MaxU

Here is a Pandassolution:

这是一个熊猫解决方案:

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

回答by Mauro Baraldi

Here is an example using an arbitrary date time

这是使用任意日期时间的示例

from datetime import datetime

start = datetime(1900,1,1,0,0,0)
end = datetime(1900,1,2,0,0,0)

Now you need to get the timedelta (the difference between two dates or times.) between the startand end

现在,你需要之间获得timedelta(两个日期或时间之间的差异。)startend

seconds = (end - start).total_seconds()

Define the 15 minutes interval

定义 15 分钟间隔

from datetime import timedelta

step = timedelta(minutes=15)

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

在秒范围内迭代,时间增量步长为 15 分钟(900 秒)并将其总和为start

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),
...

At the end you can format the datetime objects to str representation.

最后,您可以将日期时间对象格式化为 str 表示。

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

You can format datetime object at first iteration. But it may hurt your eyes

您可以在第一次迭代时格式化日期时间对象。但它可能会伤害你的眼睛

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

回答by Bakuriu

I'll provide a solution that does nothandle timezones, since the problem is generating dates and times and you can set the timezone afterwards however you want.

我将提供一个处理时区的解决方案,因为问题是生成日期和时间,之后您可以根据需要设置时区。

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 timedeltaobject 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.

您有一个开始日期以及开始和结束时间(每天),以及这些日期时间的间隔(以分钟为单位)。这个想法是创建一个timedelta表示时间间隔的对象并重复更新日期时间直到我们到达结束时间,然后我们前进一天并将时间重置为初始时间并重复。

A simple implementation could be:

一个简单的实现可能是:

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() > end_time:
            num_days_passed += 1
            if num_days_passed > 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)]

Once you have the datetimes you can use the strftimemethod to convert them to strings.

获得日期时间后,您可以使用该strftime方法将它们转换为字符串。

回答by born2Learn

This is the final script I have written based on the answers posted on my question:

这是我根据发布在我的问题上的答案编写的最终脚本:

from datetime import datetime
from datetime import timedelta
import calendar

current_utc = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")

current_year = int(current_utc.split("-")[0])
current_month = int(current_utc.split("-")[1])
current_date = int(current_utc.split("-")[2])
current_hour = int(current_utc.split("-")[3])
current_min = int(current_utc.split("-")[4])
current_sec = int(current_utc.split("-")[5])

#### To make minutes round to quarter ####
min_range_1 = range(1,16)
min_range_2 = range(16,31)
min_range_3 = range(31,46)
min_range_4 = range(46,60)

if current_min in min_range_1:
    current_min = 15
elif current_min in min_range_2:
    current_min = 30
elif current_min in min_range_3:
    current_min = 45
elif current_min in min_range_4:
    current_hour = current_hour + 1
    current_min = 0
else:
    print("Please check current minute.")

current_sec = 00

date_range_31 = range(1,32)
date_range_30 = range(1,31)

month_days_31 = [1,3,5,7,8,10,12]
month_days_30 = [4,6,9,11]

if current_month in month_days_31:
    if current_date == 31:
        next_day_month = current_month + 1
        next_day_date = 1
    else:
        next_day_month = current_month
        next_day_date = current_date
elif current_month == 2:
    if calendar.isleap(current_year):
        if current_date == 29:
            next_day_month = current_month + 1
            next_day_date = 1
        else:
            next_day_month = current_month
            next_day_date = current_date
    else:
        if current_date == 28:
            next_day_month = current_month + 1
            next_day_date = 1
        else:
            next_day_month = current_month
            next_day_date = current_date
elif current_month in month_days_30:
    if current_date == 30:
        next_day_month = current_month + 1
        next_day_date = 1
    else:
        next_day_month = current_month
        next_day_date = current_date
else:
    print("Please check the current month and date to procedd further.")

if current_hour < 11:
    current_hour = 11
    current_min = 15
    next_day_date = current_date + 1

current_start = datetime(current_year,current_month,current_date,current_hour,current_min,current_sec)
current_end = datetime(current_year,current_month,current_date,21,15,0)

next_day_start = datetime(current_year,next_day_month,next_day_date,11,15,0)
next_day_end = datetime(current_year,next_day_month,next_day_date,21,15,0)

current_seconds = (current_end - current_start).total_seconds()
next_day_seconds = (next_day_end - next_day_start).total_seconds()

step = timedelta(minutes=15)

current_day_array = []
next_day_array = []

for i in range(0, int(current_seconds), int(step.total_seconds())):
    current_day_array.append(current_start + timedelta(seconds=i))

for i in range(0, int(next_day_seconds), int(step.total_seconds())):
    current_day_array.append(next_day_start + timedelta(seconds=i))

current_day_array = [i.strftime('%Y-%m-%dT%H:%M%:%SZ') for i in current_day_array]

print current_day_array

Which produces the following output:

产生以下输出:

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