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

Support multiple units in format_timedelta() #1066

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import re
import warnings
import math
from functools import lru_cache
from typing import TYPE_CHECKING, SupportsInt

Expand Down Expand Up @@ -862,6 +863,7 @@
threshold: float = .85,
add_direction: bool = False,
format: Literal['narrow', 'short', 'medium', 'long'] = 'long',
depth: Literal['shallow', 'full', 'fullest'] = 'shallow',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these names are the best or most descriptive. No matter what they end up being, they do need to be documented, though.

locale: Locale | str | None = LC_TIME,
) -> str:
"""Return a time delta according to the rules of the given locale.
Expand Down Expand Up @@ -945,12 +947,16 @@
a_unit = f"duration-{a_unit}"
yield locale._data['unit_patterns'].get(a_unit, {}).get(format)

formatted_string = ''
for unit, secs_per_unit in TIMEDELTA_UNITS:
value = abs(seconds) / secs_per_unit
if value >= threshold or unit == granularity:
if value >= threshold or unit == granularity or (formatted_string and depth == 'fullest'):
if unit == granularity and value > 0:
value = max(1, value)
value = int(round(value))
if depth == 'shallow' or unit == granularity:
value = int(round(value))
else:
value = int(math.floor(value))

Check warning on line 959 in babel/dates.py

View check run for this annotation

Codecov / codecov/patch

babel/dates.py#L959

Added line #L959 was not covered by tests
plural_form = locale.plural_form(value)
pattern = None
for patterns in _iter_patterns(unit):
Expand All @@ -960,9 +966,14 @@
# This really should not happen
if pattern is None:
return ''
return pattern.replace('{0}', str(value))

return ''
if (depth=='shallow'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (depth=='shallow'):
if depth == 'shallow':

formatted_string = ' '.join(filter(None, [formatted_string, pattern.replace('{0}', str(value))]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what's going on in this magic incantation 😄

It's repeated two lines underneath this, so maybe it should be DRYed out too?

break
elif ((depth=='full' and value > 0) or depth == 'fullest'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif ((depth=='full' and value > 0) or depth == 'fullest'):
elif (depth == 'full' and value > 0) or depth == 'fullest':

formatted_string = ' '.join(filter(None, [formatted_string, pattern.replace('{0}', str(value))]))
seconds = seconds - value * secs_per_unit

Check warning on line 974 in babel/dates.py

View check run for this annotation

Codecov / codecov/patch

babel/dates.py#L972-L974

Added lines #L972 - L974 were not covered by tests

return formatted_string


def _format_fallback_interval(
Expand Down
Loading