Fix bug in timedeltashort and add tests

This commit is contained in:
Michael Manfre 2022-11-20 13:14:49 -05:00 committed by GitHub
parent 6b7082a194
commit f8f4fa8665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -26,8 +26,8 @@ def timedeltashort(value: datetime.datetime):
hours = seconds // (60 * 60) hours = seconds // (60 * 60)
text = f"{hours:0n}h" text = f"{hours:0n}h"
elif days < 365: elif days < 365:
text = f"{days:0n}h" text = f"{days:0n}d"
else: else:
years = days // 365.25 years = max(days // 365.25, 1)
text = f"{years:0n}y" text = f"{years:0n}y"
return text return text

View File

@ -0,0 +1,21 @@
from datetime import timedelta
from django.utils import timezone
from activities.templatetags.activity_tags import timedeltashort
def test_timedeltashort_regress():
assert timedeltashort(None) == ""
assert timedeltashort("") == ""
value = timezone.now()
assert timedeltashort(value) == "0s"
assert timedeltashort(value - timedelta(seconds=2)) == "2s"
assert timedeltashort(value - timedelta(minutes=2)) == "2m"
assert timedeltashort(value - timedelta(hours=2)) == "2h"
assert timedeltashort(value - timedelta(days=2)) == "2d"
assert timedeltashort(value - timedelta(days=364)) == "364d"
assert timedeltashort(value - timedelta(days=365)) == "1y"
assert timedeltashort(value - timedelta(days=366)) == "1y"