简体   繁体   中英

Iterative Datetime - TypeError: an integer is required

I wish to create a series of epoch time stamps from a datetime object.

I can do this with one datetime like so:

start_time = calendar.timegm(datetime.datetime(2015,9,30,0).timetuple())

returning:

1443571200

I wish to iterate over a list of datetimes to create a series of epoch time stamps.

slices = [(2015,9,30,0),(2015,10,04,23)]

for time_slice in slices:
    start_time = calendar.timegm(datetime.datetime(time_slice).timetuple())

However, this returns a TypeError: an integer is required

How do I iteratively use the calendar and datetime modules to create epoch time stamps?

You can use the following list comprehension . Simply unpack the item and pass it to the datetime constructor:

>>> from calendar import timegm
>>> from datetime import datetime
>>> start_times  = [timegm(datetime(*slc).timetuple()) for slc in slices] 
>>> start_times
[1443571200, 1443999600]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM