Have you ever wanted to iterate over the last 10 days? DateIterator provides simple solution for your problem.
from datetime import datetime, timedelta
from date_iterrator import DateIterator
itr = DateIterator(datetime(2019, 1, 1), datetime(2019, 1, 10), timedelta(days=1))
list(itr)
[(datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2019, 1, 2, 0, 0)),
(datetime.datetime(2019, 1, 2, 0, 0), datetime.datetime(2019, 1, 3, 0, 0)),
(datetime.datetime(2019, 1, 3, 0, 0), datetime.datetime(2019, 1, 4, 0, 0)),
(datetime.datetime(2019, 1, 4, 0, 0), datetime.datetime(2019, 1, 5, 0, 0)),
(datetime.datetime(2019, 1, 5, 0, 0), datetime.datetime(2019, 1, 6, 0, 0)),
(datetime.datetime(2019, 1, 6, 0, 0), datetime.datetime(2019, 1, 7, 0, 0)),
(datetime.datetime(2019, 1, 7, 0, 0), datetime.datetime(2019, 1, 8, 0, 0)),
(datetime.datetime(2019, 1, 8, 0, 0), datetime.datetime(2019, 1, 9, 0, 0)),
(datetime.datetime(2019, 1, 9, 0, 0), datetime.datetime(2019, 1, 10, 0, 0))]
Open and closed iterators are equal in the case when the interval fits exactly n times between from_date and to_date. On the other side when the parameters are as follows:
itr = DateIterator(datetime(2019, 1, 1), datetime(2019, 1, 10, hour=5), timedelta(days=1))
Now there are 5 hours left at the end. An open iterator (passed as argument closed=False) will stop and ignore the 5 additional hours. Checkout the example:
itr = DateIterator(datetime(2019, 1, 1), datetime(2019, 1, 10), timedelta(days=1), closed=False)
list(itr)
[(datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2019, 1, 2, 0, 0)),
...
(datetime.datetime(2019, 1, 9, 0, 0), datetime.datetime(2019, 1, 10, 0, 0))]
Whereas a closed iterator will generate one more iteration and include the remaining 5 hours:
itr = DateIterator(datetime(2019, 1, 1), datetime(2019, 1, 10), timedelta(days=1), closed=True)
list(itr)
[(datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2019, 1, 2, 0, 0)),
...
(datetime.datetime(2019, 1, 9, 0, 0), datetime.datetime(2019, 1, 10, 0, 0)),
(datetime.datetime(2019, 1, 10, 0, 0), datetime.datetime(2019, 1, 11, 0, 0))]