Skip to content

Instantly share code, notes, and snippets.

@jorotenev
Last active April 11, 2017 16:50
Show Gist options
  • Save jorotenev/ea2ff1028725ca2c45d93863d0d87117 to your computer and use it in GitHub Desktop.
Save jorotenev/ea2ff1028725ca2c45d93863d0d87117 to your computer and use it in GitHub Desktop.
A simple way to simulate the range() function when using datetime
from datetime import datetime as dt, timedelta as td
def datetime_range(start, end, step):
assert end >= start, "The end date should be after in time than the start. End: {end} Start:{start}".format(end=end,
start=start)
i = 0
shifted_start = start
# `end - step` because we want to exclude end from the results
while shifted_start < end - step:
delta = i * step
shifted_start = delta + start
i += 1
yield shifted_start
start = dt.now()
end = start.replace(hour=start.hour + 1)
step = td(minutes=5)
print("Start: %s" % start)
print("End: %s" % end)
print("Step: %s" % step)
for d in datetime_range(start, end, step):
print(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment