Python generator to return series of times
Question: I hope this is not outside of the abilities of Python generators, but I'd like
to build one so that every time the function is called, it returns the next
minute up until the end time.

So the function reads in a start and end time, and returns the time on a
minute by minute basis until all the time in between has been covered.

How would this be implemented? TIA


Answer: The [datetime module](http://docs.python.org/library/datetime.html) is quite
awesome. There are two datatypes you need to know about:
[`datetime`](http://docs.python.org/library/datetime.html#datetime-objects)
and [`timedelta`](http://docs.python.org/library/datetime.html#timedelta-
objects). `datetime` is a point in time, while `timedelta` is a period of
time. Basically, what I'm going to do here is start at a time and end at a
time (as a `datetime` object), and progressively add 1 minute.

This obviously has the caveat that you have to figure out how to get your
start and end time into a `datetime`. There are a number of ways to do this:
through [the
constructor](http://docs.python.org/library/datetime.html#datetime.datetime),
[right
now](http://docs.python.org/library/datetime.html#datetime.datetime.now), from
[UTC
timestamp](http://docs.python.org/library/datetime.html#datetime.datetime.fromtimestamp),
etc.

    
    
    import datetime
    
    def minute_range(start, end, step=1):
       cur = start
       while cur < end:
          yield cur
          cur += datetime.timedelta(minutes=step)
    

