Getting a lists of times in Python from 0:0:0 to 23:59:59 and then comparing with datetime values
Question: I was working on code to generate the time for an entire day with 30 second
intervals. I tried using DT.datetime and DT.time but I always end up with
either a datetime value or a timedelta value like (0,2970). Can someone please
tell me how to do this.

So I need a list that has data like:

[00:00:00] [00:00:01] [00:00:02]

till [23:59:59] and needed to compare it against a datetime value like
6/23/2011 6:38:00 AM.

Thanks!


Answer: There's a great [answer](http://stackoverflow.com/a/9736260/196068) on how to
get a list of incremental values for seconds for a 24-hour day. I reused a
part of it.

  * **Note 1**. I'm not sure how you're thinking of comparing `time` with `datetime`. Assuming that you're just going to compare the time part and extracting that.
  * **Note 2**. The `time.strptime` call expects a 12-hour AM/PM-based time, as in your example. Its result is then passed to `time.strftime` that returns a 24-hour-based time.

Here's what I think you're looking for:

    
    
    my_time = '6/23/2011 6:38:00 AM' # time you defined
    
    from datetime import datetime, timedelta
    from time import strftime, strptime
    
    now = datetime(2013, 1, 1, 0, 0, 0)
    last = datetime(2013, 1, 1, 23, 59, 59)
    delta = timedelta(seconds=1)
    
    times = []
    while now <= last:
        times.append(now.strftime('%H:%M:%S'))
        now += delta
    
    twenty_four_hour_based_time = strftime('%H:%M:%S', strptime(my_time, '%m/%d/%Y %I:%M:%S %p'))
    twenty_four_hour_based_time in times # returns True
    

