如何在python中添加一个秒数组到引用日期时间,例如:
ref_time = datetime.datetime(year=2017, month=6, day=5, hour=10, minute=30, second=30)
secs = (0,1,2,3,4,5,6,7,8,9,10)
结果必须是:
2017,6,5,10,30,30
2017,6,5,10,30,31
2017,6,5,10,30,32
2017,6,5,10,30,33 等等
我试过这个,但它不起作用,我做错了什么?
d = datetime.timedelta(seconds=np.int(secs))
new_timestamp = timestamp+d
对不起这个简单的问题,我对python
很新答案 0 :(得分:0)
timedelta
接受一个参数作为第二个参数,这意味着您需要一次传递secs
元组中的秒数。您只需使用列表解析即可实现此目的:
In [1]: import datetime
In [2]: ref_time = datetime.datetime(year=2017, month=6, day=5, hour=10, minute=30, second=30)
In [3]: secs = (0,1,2,3,4,5,6,7,8,9,10)
In [4]: [ref_time + datetime.timedelta(seconds=i) for i in secs]
Out[4]:
[datetime.datetime(2017, 6, 5, 10, 30, 30),
datetime.datetime(2017, 6, 5, 10, 30, 31),
datetime.datetime(2017, 6, 5, 10, 30, 32),
datetime.datetime(2017, 6, 5, 10, 30, 33),
datetime.datetime(2017, 6, 5, 10, 30, 34),
datetime.datetime(2017, 6, 5, 10, 30, 35),
datetime.datetime(2017, 6, 5, 10, 30, 36),
datetime.datetime(2017, 6, 5, 10, 30, 37),
datetime.datetime(2017, 6, 5, 10, 30, 38),
datetime.datetime(2017, 6, 5, 10, 30, 39),
datetime.datetime(2017, 6, 5, 10, 30, 40)]