Skip to content

Commit 87d792e

Browse files
authored
Merge pull request #276 from lanker/sub
Support for subtracting when entering a date
2 parents 960deb0 + 21146b0 commit 87d792e

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

ftplugin/orgmode/plugins/Date.py

+26-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import re
33
from datetime import timedelta, date, datetime
44

5+
import operator
6+
57
import vim
68

79
from orgmode._vim import ORGMODE, echom, insert_at_cursor, get_user_input
@@ -66,6 +68,8 @@ def _modify_time(cls, startdate, modifier):
6668
# rm crap from modifier
6769
modifier = modifier.strip()
6870

71+
ops = {'-': operator.sub, '+': operator.add}
72+
6973
# check real date
7074
date_regex = r"(\d\d\d\d)-(\d\d)-(\d\d)"
7175
match = re.search(date_regex, modifier)
@@ -131,37 +135,42 @@ def _modify_time(cls, startdate, modifier):
131135
newdate = startdate + timedelta(days=diff)
132136

133137
# check for days modifier with appended d
134-
match = re.search(u'\+(\d*)d', modifier)
138+
match = re.search(u'^(\+|-)(\d*)d', modifier)
135139
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)))
138142

139143
# check for days modifier without appended d
140-
match = re.search(u'\+(\d*) |\+(\d*)$', modifier)
144+
match = re.search(u'^(\+|-)(\d*) |^(\+|-)(\d*)$', modifier)
141145
if match:
146+
groups = match.groups()
142147
try:
143-
days = int(match.groups()[0])
148+
op = groups[0]
149+
days = int(groups[1])
144150
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))
147154

148155
# check for week modifier
149-
match = re.search(u'\+(\d+)w', modifier)
156+
match = re.search(u'^(\+|-)(\d+)w', modifier)
150157
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)))
153160

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)
156163
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)
159167

160168
# check for year modifier
161-
match = re.search(u'\+(\d*)y', modifier)
169+
match = re.search(u'^(\+|-)(\d*)y', modifier)
162170
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)
165174

166175
# check for month day
167176
match = re.search(

0 commit comments

Comments
 (0)