1 |
|
---|
2 | import re
|
---|
3 | from datetime import datetime, date, timedelta
|
---|
4 |
|
---|
5 |
|
---|
6 | class DateFilter(object):
|
---|
7 |
|
---|
8 | """
|
---|
9 | Filtering tools for the dates input provided by the user
|
---|
10 | """
|
---|
11 |
|
---|
12 | def __init__(self, date_format):
|
---|
13 | self.date_format = date_format
|
---|
14 |
|
---|
15 | def days(self, current_date, days, action='-'):
|
---|
16 | """
|
---|
17 | Add/substract the given number of days from the given current_date
|
---|
18 | """
|
---|
19 | delta = timedelta(days=days)
|
---|
20 | if action == '-':
|
---|
21 | new_date = current_date - delta
|
---|
22 | else:
|
---|
23 | new_date = current_date + delta
|
---|
24 | new_date = new_date.replace(hour=0, minute=0, second=0)
|
---|
25 | return new_date
|
---|
26 |
|
---|
27 | def days_ago(self, current_date, days):
|
---|
28 | """
|
---|
29 | Return the date N days ago from the current date
|
---|
30 | """
|
---|
31 | return self.days(current_date, days, '-')
|
---|
32 |
|
---|
33 | def days_future(self, current_date, days):
|
---|
34 | """
|
---|
35 | Return the date N days in the future from the current date
|
---|
36 | """
|
---|
37 | return self.days(current_date, days, '+')
|
---|
38 |
|
---|
39 | def months(self, current_date, months, action='-'):
|
---|
40 | """
|
---|
41 | Add/substract the given number of months from the given current_date
|
---|
42 | """
|
---|
43 | if action == '-':
|
---|
44 | # start travelling in time, back to N months ago
|
---|
45 | for n in range(months):
|
---|
46 | current_date = current_date.replace(day=1) - timedelta(days=1)
|
---|
47 | else:
|
---|
48 | # start travelling in time, forward to N months
|
---|
49 | for n in range(months):
|
---|
50 | current_date = current_date.replace(day=1) + timedelta(days=1)
|
---|
51 |
|
---|
52 | # Now use the new year/month values + the current day to set the proper
|
---|
53 | # date
|
---|
54 | new_date = datetime(
|
---|
55 | current_date.year, current_date.month, date.today().day)
|
---|
56 | return new_date
|
---|
57 |
|
---|
58 | def months_ago(self, current_date, months):
|
---|
59 | """
|
---|
60 | Return the date N days ago from the current date
|
---|
61 | """
|
---|
62 | return self.months(current_date, months, '-')
|
---|
63 |
|
---|
64 | def months_future(self, current_date, months):
|
---|
65 | """
|
---|
66 | Return the date N days in the future from the current date
|
---|
67 | """
|
---|
68 | return self.months(current_date, months, '+')
|
---|
69 |
|
---|
70 | def parse_number_filter(self, number_filter, start_date=None,
|
---|
71 | future=False):
|
---|
72 | """
|
---|
73 | Given a numeric filter with the N[d|w|m|y] pattern, return both
|
---|
74 | the number of days/months/years that apply as a filter, + the
|
---|
75 | date N[d|w|m|y] ago from start_date (which defaults to today if
|
---|
76 | None is passed).
|
---|
77 |
|
---|
78 | If future is True, it will return a date in the future, not from
|
---|
79 | the past.
|
---|
80 | """
|
---|
81 | number = None
|
---|
82 | filtered_date = None
|
---|
83 | start_date = start_date or datetime.today()
|
---|
84 |
|
---|
85 | # set which methods use to process the date, going backwards into the
|
---|
86 | # past as a default, unless we are told otherwise
|
---|
87 | filter_days = self.days_ago
|
---|
88 | filter_months = self.months_ago
|
---|
89 | if future:
|
---|
90 | filter_days = self.days_future
|
---|
91 | filter_months = self.months_future
|
---|
92 |
|
---|
93 | if re.search(r'(\d+[dD]{1})', number_filter):
|
---|
94 | number = int(number_filter.lower().replace('d', ''))
|
---|
95 | filtered_date = filter_days(start_date, number)
|
---|
96 |
|
---|
97 | elif re.search(r'(\d+[wW]{1})', number_filter):
|
---|
98 | number = int(number_filter.lower().replace('w', '')) * 7
|
---|
99 | filtered_date = filter_days(start_date, number)
|
---|
100 |
|
---|
101 | elif re.search(r'(\d+[mM]{1})', number_filter):
|
---|
102 | number = int(number_filter.lower().replace('m', ''))
|
---|
103 | filtered_date = filter_months(start_date, number)
|
---|
104 |
|
---|
105 | elif re.search(r'(\d+[yY]{1})', number_filter):
|
---|
106 | number = int(number_filter.lower().replace('y', ''))
|
---|
107 | today = date.today()
|
---|
108 | # by default assume going backwards into the past...
|
---|
109 | year = today.year - number
|
---|
110 | if future:
|
---|
111 | #...unless told otherwise
|
---|
112 | year = today.year + number
|
---|
113 | filtered_date = datetime(year, today.month, today.day)
|
---|
114 |
|
---|
115 | return number, filtered_date
|
---|
116 |
|
---|
117 | def validate(self, stamp_filter):
|
---|
118 | """
|
---|
119 | Validate a given filter. Filters can have the following notation:
|
---|
120 |
|
---|
121 | - %Y-%m-%d: Times recorded at a given date
|
---|
122 |
|
---|
123 | - %Y-%m-%d--%Y-%m-%d: Times recorded between two dates
|
---|
124 |
|
---|
125 | - *%Y-%m-%d: Times recorded up to a given date
|
---|
126 |
|
---|
127 | - %Y-%m-%d*: Times recorded from a given date
|
---|
128 |
|
---|
129 | - %Y-%m-%d+sN[d|w|m|y]: Times recorded since the given date up to
|
---|
130 | N more days/weeks/months/years
|
---|
131 |
|
---|
132 | - N...N[d|w|m|y]: Times recorded N...N days/weeks/months/years ago
|
---|
133 |
|
---|
134 | Important: all date comparisons are made on datetime objects, using
|
---|
135 | 00:00 as the time (first second of the given day). This means that
|
---|
136 | for range filters, the first day is included, but the second day is not
|
---|
137 | """
|
---|
138 | filter_from = None
|
---|
139 | filter_to = None
|
---|
140 |
|
---|
141 | if stamp_filter is None:
|
---|
142 | return filter_from, filter_to
|
---|
143 |
|
---|
144 | if '--' in stamp_filter:
|
---|
145 | filter_from, filter_to = stamp_filter.split('--')
|
---|
146 | filter_from = datetime.strptime(filter_from, self.date_format)
|
---|
147 | filter_to = datetime.strptime(filter_to, self.date_format)
|
---|
148 |
|
---|
149 | elif stamp_filter.startswith('*'):
|
---|
150 | filter_to = datetime.strptime(stamp_filter, '*'+self.date_format)
|
---|
151 | filter_to = filter_to.replace(hour=0, minute=0, second=0)
|
---|
152 |
|
---|
153 | elif stamp_filter.endswith('*'):
|
---|
154 | filter_from = datetime.strptime(stamp_filter, self.date_format+'*')
|
---|
155 | filter_from = filter_from.replace(hour=0, minute=0, second=0)
|
---|
156 |
|
---|
157 | elif '+' in stamp_filter:
|
---|
158 | # "+" filtering works with a date following by "+", a number and
|
---|
159 | # a letter (d|w|m|y) which sets the number of days, weeks, months,
|
---|
160 | # years we would like to go into the future
|
---|
161 | filter_from, number = stamp_filter.split('+')
|
---|
162 | filter_from = datetime.strptime(filter_from, self.date_format)
|
---|
163 | number, filter_to = self.parse_number_filter(
|
---|
164 | number, filter_from, future=True)
|
---|
165 |
|
---|
166 | elif stamp_filter.count('-') == 3:
|
---|
167 | # "-" filtering works with a date followed by "-", a number and
|
---|
168 | # a letter (d|w|m|y) which sets the number of days, weeks, months,
|
---|
169 | # years we would like to go backwards into the past
|
---|
170 | year, month, day, number = stamp_filter.split('-')
|
---|
171 | filter_to = '-'.join([year, month, day])
|
---|
172 | filter_to = datetime.strptime(filter_to, self.date_format)
|
---|
173 | number, filter_from = self.parse_number_filter(number, filter_to)
|
---|
174 |
|
---|
175 | else:
|
---|
176 | # Check if the user is asking for N days/weeks/months/years
|
---|
177 | number, filter_from = self.parse_number_filter(stamp_filter)
|
---|
178 | if number is None:
|
---|
179 | # no filtering found, maybe they are giving us a fixed date
|
---|
180 | try:
|
---|
181 | filter_from = datetime.strptime(stamp_filter,
|
---|
182 | self.date_format)
|
---|
183 | except:
|
---|
184 | # nothing to be used as a filter, go on, printing a warning
|
---|
185 | print('[warning] invalid date filter: ' + stamp_filter)
|
---|
186 | else:
|
---|
187 | filter_from = filter_from.replace(hour=0, minute=0,
|
---|
188 | second=0)
|
---|
189 | filter_to = filter_from + timedelta(days=1)
|
---|
190 |
|
---|
191 | return filter_from, filter_to
|
---|