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

Using floating numbers for Time Differences #1191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ Fred Thomsen - me [at] fredthomsen [dot] net - http://fredthomsen.net
Robin Schubert
Axel Gschaider - axel.gschaider [at] posteo [dot] de
Anuragh - kpanuragh [at] gmail [dot] com
Daniel James Perry - dperry45 [at] gatech [dot] edu
12 changes: 6 additions & 6 deletions khal/parse_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def guesstimedeltafstr(delta_string: str) -> dt.timedelta:
:rtype: datetime.timedelta
"""

tups = re.split(r'(-?\d+)', delta_string)
tups = re.split(r'(-?\d+\.?\d*)', delta_string)
if not re.match(r'^\s*$', tups[0]):
raise ValueError('Invalid beginning of timedelta string "%s": "%s"'
% (delta_string, tups[0]))
Expand All @@ -300,22 +300,22 @@ def guesstimedeltafstr(delta_string: str) -> dt.timedelta:

for num, unit in zip(tups[0::2], tups[1::2]):
try:
numint = int(num)
numfloat = float(num)
except ValueError:
raise DateTimeParseError(
f'Invalid number in timedelta string "{delta_string}": "{num}"')

ulower = unit.lower().strip()
if ulower == 'd' or ulower == 'day' or ulower == 'days':
res += dt.timedelta(days=numint)
res += dt.timedelta(days=numfloat)
elif ulower == 'h' or ulower == 'hour' or ulower == 'hours':
res += dt.timedelta(hours=numint)
res += dt.timedelta(hours=numfloat)
elif (ulower == 'm' or ulower == 'minute' or ulower == 'minutes' or
ulower == 'min'):
res += dt.timedelta(minutes=numint)
res += dt.timedelta(minutes=numfloat)
elif (ulower == 's' or ulower == 'second' or ulower == 'seconds' or
ulower == 'sec'):
res += dt.timedelta(seconds=numint)
res += dt.timedelta(seconds=numfloat)
else:
raise ValueError('Invalid unit in timedelta string "%s": "%s"'
% (delta_string, unit))
Expand Down
3 changes: 3 additions & 0 deletions tests/parse_datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def test_single(self):
def test_seconds(self):
assert dt.timedelta(seconds=10) == guesstimedeltafstr('10s')

def test_decimal(self):
assert dt.timedelta(hours=1.5) == guesstimedeltafstr('1.5h')

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

Expand Down