2 # -*- coding: utf-8 -*-
4 # Author: Guilherme Victal <guilherme at victal.eti.br>
5 # Description: Generates regexes before a certain date
7 # Website: http://github.com/freitass/todo.txt-vim
10 from datetime import date, timedelta, MINYEAR
12 def _year_regex_before(year):
13 if int(year) <= MINYEAR:
16 year_regex += r'\d{1,%s}' % str(len(year) - 1) if len(year) > 1 else ''
17 for idx, digit in enumerate(year):
19 regex = '|' + year[0:idx]
20 regex += '0' if digit == '1' else '[0-%s]' % str(int(digit) - 1)
21 if idx < len(year) - 1:
22 regex += '\d{%s}' % (len(year) - (idx + 1))
26 return '-'.join((year_regex, r'\d{2}', r'\d{2}'))
28 def _month_regex_before(year, month):
32 digit1, digit2 = month
34 month_regex = '01' if month == '02' else r'0[1-%s]' % str(int(digit2) - 1)
38 month_regex = r'(0\d|10)'
40 month_regex = r'(0\d|1[01])'
42 return '-'.join((year, month_regex, r'\d{2}'))
44 def _day_regex_before(year, month, day):
47 last_month_day = str((date(int(year), int(month) % 12 + 1, 1) + - date.resolution).day)
48 last_digit1, last_digit2 = last_month_day
52 day_regex = '01' if day == '02' else r'0[1-%s]' % str(int(digit2) - 1)
55 day_regex += '0' if digit1 == '1' else r'[0-%s]' % str(int(digit1) - 1)
60 day_regex += '0' if digit2 == '1' else r'[0-%s]' % str(int(digit2) - 1)
63 return '-'.join((year, month, day_regex))
68 def regex_date_before(given_date):
69 year, month, day = given_date.isoformat().split('-')
71 year_regex = _year_regex_before(year)
72 month_regex = _month_regex_before(year, month)
73 day_regex = _day_regex_before(year, month, day)
75 date_regex = '(' + year_regex if year_regex else '('
76 date_regex += ('|' + month_regex) if month_regex else ''
77 date_regex += ('|' + day_regex) if day_regex else ''