-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_utilities.py
39 lines (26 loc) · 967 Bytes
/
date_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from datetime import datetime, timedelta
DATE_FORMAT = '%Y-%m-%d'
DATE_STEP = timedelta(days=1)
def _strptime(string):
return datetime.strptime(string, DATE_FORMAT)
def _strftime(date):
return date.strftime(DATE_FORMAT)
def _date_range_parameters(start, end, span_days):
start = _strptime(start)
end = _strptime(end)
span = timedelta(days=span_days)
return start, end, span
def forward_date_range(start, end, span_days):
"""
Generate tuples with intervals from given range of dates (forward).
forward_date_range('2012-01-01', '2012-01-5', 2)
1st yield = ('2012-01-01', '2012-01-03')
2nd yield = ('2012-01-04', '2012-01-05')
"""
start, end, span = _date_range_parameters(start, end, span_days)
stop = end - span
while start < stop:
current = start + span
yield _strftime(start), _strftime(current)
start = current + DATE_STEP
yield _strftime(start), _strftime(end)