misc: add years and months as units to display durations (#27036) #611

Merged
fpeters merged 1 commits from wip/27036-bigger-human-time into main 2023-08-17 10:32:30 +02:00
2 changed files with 27 additions and 0 deletions

View File

@ -75,6 +75,8 @@ def test_parse_invalid_file_size():
(13, '13 seconds'),
(60, '1 minute'),
(3600, '1 hour'),
(10_000_000, '3 months, 22 days, 17 hours, 46 minutes and 40 seconds'),
(100_000_000, '3 years, 1 month, 30 days, 15 hours, 46 minutes and 40 seconds'),
],
)
def test_humantime(seconds, expected):
@ -84,6 +86,21 @@ def test_humantime(seconds, expected):
assert humanduration2seconds(seconds2humanduration(seconds)) == seconds
@pytest.mark.parametrize(
'seconds, expected',
[
(120, '2min'),
(3600, '1h'),
(3720, '1h02'),
(100_000, '1 day and 3h46'),
],
)
def test_humantime_short(seconds, expected):
pub = create_temporary_pub()
pub.ngettext = translation.ngettext
assert seconds2humanduration(seconds, short=True) == expected
def test_parse_mimetypes():
assert FileTypesDirectory.parse_mimetypes('application/pdf') == ['application/pdf']
assert FileTypesDirectory.parse_mimetypes('.pdf') == ['application/pdf']

View File

@ -70,6 +70,11 @@ def seconds2humanduration(seconds, short=False):
if not isinstance(seconds, int):
return ''
if not short:
years = int(seconds / _year)
seconds = seconds - _year * years
months = int(seconds / _month)
seconds = seconds - _month * months
days = int(seconds / _day)
seconds = seconds - _day * days
hours = int(seconds / _hour)
@ -77,6 +82,11 @@ def seconds2humanduration(seconds, short=False):
minutes = int(seconds / _minute)
seconds = seconds - _minute * minutes
human = []
if not short:
if years:
human.append(ngettext('%(total)s year', '%(total)s years', years) % {'total': years})
if months:
human.append(ngettext('%(total)s month', '%(total)s months', months) % {'total': months})
if days:
human.append(ngettext('%(total)s day', '%(total)s days', days) % {'total': days})
if short: