Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/permissive delta parsing #1305

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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