-
Notifications
You must be signed in to change notification settings - Fork 453
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ | |||||
|
||||||
import re | ||||||
import warnings | ||||||
import math | ||||||
from functools import lru_cache | ||||||
from typing import TYPE_CHECKING, SupportsInt | ||||||
|
||||||
|
@@ -862,6 +863,7 @@ | |||||
threshold: float = .85, | ||||||
add_direction: bool = False, | ||||||
format: Literal['narrow', 'short', 'medium', 'long'] = 'long', | ||||||
depth: Literal['shallow', 'full', 'fullest'] = 'shallow', | ||||||
locale: Locale | str | None = LC_TIME, | ||||||
) -> str: | ||||||
"""Return a time delta according to the rules of the given locale. | ||||||
|
@@ -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)) | ||||||
plural_form = locale.plural_form(value) | ||||||
pattern = None | ||||||
for patterns in _iter_patterns(unit): | ||||||
|
@@ -960,9 +966,14 @@ | |||||
# This really should not happen | ||||||
if pattern is None: | ||||||
return '' | ||||||
return pattern.replace('{0}', str(value)) | ||||||
|
||||||
return '' | ||||||
if (depth=='shallow'): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
formatted_string = ' '.join(filter(None, [formatted_string, pattern.replace('{0}', str(value))])) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
formatted_string = ' '.join(filter(None, [formatted_string, pattern.replace('{0}', str(value))])) | ||||||
seconds = seconds - value * secs_per_unit | ||||||
|
||||||
return formatted_string | ||||||
|
||||||
|
||||||
def _format_fallback_interval( | ||||||
|
There was a problem hiding this comment.
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.