2 # -*- coding: utf-8 -*-
4 # Author: Guilherme Victal <guilherme at victal.eti.br>
5 # Description: Generates regexes after a certain date
7 # Website: http://github.com/freitass/todo.txt-vim
10 from datetime import date, timedelta, MAXYEAR
13 def _year_regex_after(year):
14 if int(year) > MAXYEAR:
17 year_regex = r'(\d+\d{%s}' % len(year)
18 for idx, digit in enumerate(year):
20 regex = '|' + year[0:idx]
21 regex += '9' if digit == '8' else '[%s-9]' % str(int(digit) + 1)
22 if idx < len(year) - 1:
23 regex += '\d{%s}' % (len(year) - (idx + 1))
27 return '-'.join((year_regex, r'\d{2}', r'\d{2}'))
30 def _month_regex_after(year, month):
34 digit1, digit2 = month
36 month_regex = r'12' if month == '11' else r'1[12]'
38 month_regex = r'1[0-2]'
41 month_regex = r'(' + month_regex + r'|09)'
43 month_regex = r'(' + month_regex + r'|0[%s-9])'
44 month_regex = month_regex % str(int(digit2) + 1)
45 return '-'.join((year, month_regex, r'\d{2}'))
47 def _day_regex_after(year, month, day):
48 last_month_day = str((date(int(year), (int(month) + 1) % 12, 1) + - date.resolution).day)
49 if day == last_month_day:
53 last_digit1, last_digit2 = last_month_day
54 if digit1 == last_digit1:
55 day_regex = last_month_day if int(digit2) == int(last_digit2) - 1 else last_digit1 + r'[%s-%s]' % (str(int(digit2) + 1), last_digit2)
58 day_regex += last_digit1 if int(digit1) == int(last_digit1) - 1 else r'[%s-%s]' % (str(int(digit1) + 1), last_digit1)
61 day_regex += '|' + digit1
62 day_regex += '9' if digit2 == '8' else r'[%s-9]' % str(int(digit2) + 1)
65 return '-'.join((year, month, day_regex))
68 def regex_date_after(given_date):
69 year, month, day = given_date.isoformat().split('-')
71 year_regex = _year_regex_after(year)
72 month_regex = _month_regex_after(year, month)
73 day_regex = _day_regex_after(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 ''
84 date_regex = regex_date_after(date(1999,12,31))
86 pattern = re.compile(date_regex)
89 d = date.today() + date.resolution
90 assert pattern.match(date.strftime(d, '%Y-%m-%d')) is not None
91 print(date.strftime(d, '%Y-%m-%d') + ' is okay')
94 if __name__ == '__main__':