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

Conversation

hartmutobendorf
Copy link

Issue #728 describes returning measures for several units when using format_timedelta().

E.g. instead of

10840 seconds
3 hours

return for depth=="fullest"

3 hours 0 minutes 40 seconds

and for depth=="full"

3 hours 40 seconds

Tests are passing, but I haven't tried contributing to babel before, and am not aware of any additional requirements beyond the few lines of source code in this PR. Feedback appreciated.

Copy link

codecov bot commented Mar 4, 2024

Codecov Report

Attention: Patch coverage is 69.23077% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 90.94%. Comparing base (40e60a1) to head (8683607).

Files Patch % Lines
babel/dates.py 69.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1066      +/-   ##
==========================================
- Coverage   90.99%   90.94%   -0.05%     
==========================================
  Files          26       26              
  Lines        4444     4453       +9     
==========================================
+ Hits         4044     4050       +6     
- Misses        400      403       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

Thanks!

The depth names don't sound great to me, and this would really require some tests, in various locales too.

@@ -862,6 +863,7 @@ def format_timedelta(
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.

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':

if (depth=='shallow'):
formatted_string = ' '.join(filter(None, [formatted_string, pattern.replace('{0}', str(value))]))
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':


return ''
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants