|
2 | 2 | import re |
3 | 3 | from datetime import timedelta, date, datetime |
4 | 4 |
|
| 5 | +import operator |
| 6 | + |
5 | 7 | import vim |
6 | 8 |
|
7 | 9 | from orgmode._vim import ORGMODE, echom, insert_at_cursor, get_user_input |
@@ -66,6 +68,8 @@ def _modify_time(cls, startdate, modifier): |
66 | 68 | # rm crap from modifier |
67 | 69 | modifier = modifier.strip() |
68 | 70 |
|
| 71 | + ops = {'-': operator.sub, '+': operator.add} |
| 72 | + |
69 | 73 | # check real date |
70 | 74 | date_regex = r"(\d\d\d\d)-(\d\d)-(\d\d)" |
71 | 75 | match = re.search(date_regex, modifier) |
@@ -131,37 +135,42 @@ def _modify_time(cls, startdate, modifier): |
131 | 135 | newdate = startdate + timedelta(days=diff) |
132 | 136 |
|
133 | 137 | # check for days modifier with appended d |
134 | | - match = re.search(u'\+(\d*)d', modifier) |
| 138 | + match = re.search(u'^(\+|-)(\d*)d', modifier) |
135 | 139 | if match: |
136 | | - days = int(match.groups()[0]) |
137 | | - newdate = startdate + timedelta(days=days) |
| 140 | + op, days = match.groups() |
| 141 | + newdate = ops[op](startdate, timedelta(days=int(days))) |
138 | 142 |
|
139 | 143 | # check for days modifier without appended d |
140 | | - match = re.search(u'\+(\d*) |\+(\d*)$', modifier) |
| 144 | + match = re.search(u'^(\+|-)(\d*) |^(\+|-)(\d*)$', modifier) |
141 | 145 | if match: |
| 146 | + groups = match.groups() |
142 | 147 | try: |
143 | | - days = int(match.groups()[0]) |
| 148 | + op = groups[0] |
| 149 | + days = int(groups[1]) |
144 | 150 | except: |
145 | | - days = int(match.groups()[1]) |
146 | | - newdate = startdate + timedelta(days=days) |
| 151 | + op = groups[2] |
| 152 | + days = int(groups[3]) |
| 153 | + newdate = ops[op](startdate, timedelta(days=days)) |
147 | 154 |
|
148 | 155 | # check for week modifier |
149 | | - match = re.search(u'\+(\d+)w', modifier) |
| 156 | + match = re.search(u'^(\+|-)(\d+)w', modifier) |
150 | 157 | if match: |
151 | | - weeks = int(match.groups()[0]) |
152 | | - newdate = startdate + timedelta(weeks=weeks) |
| 158 | + op, weeks = match.groups() |
| 159 | + newdate = ops[op](startdate, timedelta(weeks=int(weeks))) |
153 | 160 |
|
154 | | - # check for week modifier |
155 | | - match = re.search(u'\+(\d+)m', modifier) |
| 161 | + # check for month modifier |
| 162 | + match = re.search(u'^(\+|-)(\d+)m', modifier) |
156 | 163 | if match: |
157 | | - months = int(match.groups()[0]) |
158 | | - newdate = date(startdate.year, startdate.month + months, startdate.day) |
| 164 | + op, months = match.groups() |
| 165 | + newdate = date(startdate.year, ops[op](startdate.month, int(months)), |
| 166 | + startdate.day) |
159 | 167 |
|
160 | 168 | # check for year modifier |
161 | | - match = re.search(u'\+(\d*)y', modifier) |
| 169 | + match = re.search(u'^(\+|-)(\d*)y', modifier) |
162 | 170 | if match: |
163 | | - years = int(match.groups()[0]) |
164 | | - newdate = date(startdate.year + years, startdate.month, startdate.day) |
| 171 | + op, years = match.groups() |
| 172 | + newdate = date(ops[op](startdate.year, int(years)), startdate.month, |
| 173 | + startdate.day) |
165 | 174 |
|
166 | 175 | # check for month day |
167 | 176 | match = re.search( |
|
0 commit comments