Merge pull request #1977 from Henddher/issue1970

validatorfuncs.duration() returns incorrect datetime.timedelta() Closes #1970
This commit is contained in:
Griatch 2019-10-27 20:26:31 +01:00 committed by GitHub
commit 33fd9c13bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View file

@ -44,10 +44,13 @@ class TestValidatorFuncs(TestCase):
self.assertTrue(
isinstance(validatorfuncs.duration(d), datetime.timedelta))
# THE FOLLOWING FAILS, year calculation seems to be incorrect
# self.assertEqual(
# datetime.timedelta(1+5*365, 2, 0, 0, 3, 4, 5),
# validatorfuncs.duration('1d 2s 3m 4h 5w 5y'))
self.assertEqual(
datetime.timedelta(1+6*365, 2, 0, 0, 3, 4, 5),
validatorfuncs.duration('1d 2s 3m 4h 5w 6y'))
# values may be duplicated
self.assertEqual(
datetime.timedelta((1+7)+(6+12)*365, 2+8, 0, 0, 3+9, 4+10, 5+11),
validatorfuncs.duration('1d 2s 3m 4h 5w 6y 7d 8s 9m 10h 11w 12y'))
def test_duration_raises_ValueError(self):
for d in ['', '1', '5days', '1Week']:

View file

@ -102,17 +102,17 @@ def duration(entry, option_key="Duration", **kwargs):
for interval in time_string:
if _re.match(r"^[\d]+s$", interval):
seconds = +int(interval.rstrip("s"))
seconds += int(interval.rstrip("s"))
elif _re.match(r"^[\d]+m$", interval):
minutes = +int(interval.rstrip("m"))
minutes += int(interval.rstrip("m"))
elif _re.match(r"^[\d]+h$", interval):
hours = +int(interval.rstrip("h"))
hours += int(interval.rstrip("h"))
elif _re.match(r"^[\d]+d$", interval):
days = +int(interval.rstrip("d"))
days += int(interval.rstrip("d"))
elif _re.match(r"^[\d]+w$", interval):
weeks = +int(interval.rstrip("w"))
weeks += int(interval.rstrip("w"))
elif _re.match(r"^[\d]+y$", interval):
days = +int(interval.rstrip("y")) * 365
days += int(interval.rstrip("y")) * 365
else:
raise ValueError(f"Could not convert section '{interval}' to a {option_key}.")