Skip to content

Commit

Permalink
Merge pull request #1305 from pimutils/feature/permissive_delta_parsing
Browse files Browse the repository at this point in the history
Feature/permissive delta parsing
  • Loading branch information
geier authored Oct 27, 2023
2 parents 25c5c26 + 5ce8007 commit a6d12ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ not released yet
an event
* NEW properties of ikhal themes (dark and light) can now be overriden from the
config file (via the new [palette] section, check the documenation)
* NEW timedelta strings can now have a leading `+`, e.g. `+1d`

0.11.2
======
Expand Down
4 changes: 3 additions & 1 deletion khal/parse_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,16 @@ def guesstimedeltafstr(delta_string: str) -> dt.timedelta:
:param delta_string: string encoding time-delta, e.g. '1h 15m'
"""

tups = re.split(r'(-?\d+)', delta_string)
tups = re.split(r'(-?\+?\d+)', delta_string)
if not re.match(r'^\s*$', tups[0]):
raise ValueError(f'Invalid beginning of timedelta string "{delta_string}": "{tups[0]}"')
tups = tups[1:]
res = dt.timedelta()

for num, unit in zip(tups[0::2], tups[1::2]):
try:
if num[0] == '+':
num = num[1:]
numint = int(num)
except ValueError:
raise DateTimeParseError(
Expand Down
17 changes: 17 additions & 0 deletions tests/parse_datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,30 @@ def test_single(self):
def test_seconds(self):
assert dt.timedelta(seconds=10) == guesstimedeltafstr('10s')

def test_single_plus(self):
assert dt.timedelta(minutes=10) == guesstimedeltafstr('+10m')

def test_seconds_plus(self):
assert dt.timedelta(seconds=10) == guesstimedeltafstr('+10s')

def test_days_plus(self):
assert dt.timedelta(days=10) == guesstimedeltafstr('+10days')

def test_negative(self):
assert dt.timedelta(minutes=-10) == guesstimedeltafstr('-10m')

def test_multi(self):
assert dt.timedelta(days=1, hours=-3, minutes=10) == \
guesstimedeltafstr(' 1d -3H 10min ')

def test_multi_plus(self):
assert dt.timedelta(days=1, hours=3, minutes=10) == \
guesstimedeltafstr(' 1d +3H 10min ')

def test_multi_plus_minus(self):
assert dt.timedelta(days=0, hours=21, minutes=10) == \
guesstimedeltafstr('+1d -3H 10min ')

def test_multi_nospace(self):
assert dt.timedelta(days=1, hours=-3, minutes=10) == \
guesstimedeltafstr('1D-3hour10m')
Expand Down

0 comments on commit a6d12ad

Please sign in to comment.