FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve `precisedelta` to better support rounding and float values. by nuztalgia · Pull Request #39 · python-humanize/humanize · GitHub

Improve precisedelta to better support rounding and float values. - #39

Closed
nuztalgia wants to merge 6 commits into
python-humanize:mainfrom
nuztalgia:precisedelta-rounding
Closed

Improve precisedelta to better support rounding and float values.#39
nuztalgia wants to merge 6 commits into
python-humanize:mainfrom
nuztalgia:precisedelta-rounding

Conversation

nuztalgia commented Jul 2, 2022
edited
Loading

Copy link
Copy Markdown
Contributor

Fixes #14.
Fixes #20.

Changes proposed in this pull request

  • Implement better support for rounding and float values in precisedelta.
  • Add the test cases listed below, to clarify/enforce desired behavior.
  • Improve grammar and consistency in some internal docstrings/comments/annotations.

Test cases added to test_precisedelta_custom_format

>>> precisedelta(dt.timedelta(days=31), "days", format="%d")
BEFORE:  "1 month and 0 days"
AFTER:   "1 month"
>>> precisedelta(dt.timedelta(days=31.01), "days", format="%d")
BEFORE:  "1 month and 0 days"
AFTER:   "1 month and 1 day"
>>> precisedelta(dt.timedelta(days=31.99), "days", format="%d")
BEFORE:  "1 month and 1 days"
AFTER:   "1 month and 1 day"
>>> precisedelta(dt.timedelta(days=32), "days", format="%d")
BEFORE:  "1 month and 1 days"
AFTER:   "1 month and 2 days"
>>> precisedelta(dt.timedelta(days=62), "days", format="%d")
BEFORE:  "2 months and 1 day"
AFTER:   "2 months and 1 day"  # no change
>>> precisedelta(dt.timedelta(days=92), "days", format="%d")
BEFORE:  "3 months and 0 days"
AFTER:   "3 months"
>>> precisedelta(0.01, "seconds", format="%0.3f")
BEFORE:  "0 seconds"
AFTER:   "0.010 seconds"
>>> precisedelta(31, "minutes", format="%d")
BEFORE:  "0 minutes"
AFTER:   "1 minute"
>>> precisedelta(60 + 29.99, "minutes", format="%d")
BEFORE:  "1 minutes"
AFTER:   "1 minute"
>>> precisedelta(60 + 30, "minutes", format="%d")
BEFORE:  "1 minutes"
AFTER:   "2 minutes"
>>> precisedelta(60 * 60 + 30.99, "minutes", format="%.0f")
BEFORE:  "1 hour and 0 minutes"
AFTER:   "1 hour"
>>> precisedelta(60 * 60 + 31, "minutes", format="%.0f")
BEFORE:  "1 hour and 1 minutes"
AFTER:   "1 hour and 1 minute"
>>> precisedelta(ONE_DAY - MILLISECONDS_1_337, "seconds", format="%.1f")
BEFORE:  "23 hours, 59 minutes and 58 seconds"
AFTER:   "23 hours, 59 minutes and 58.7 seconds"
>>> precisedelta(ONE_DAY - ONE_MILLISECOND, "seconds", format="%.4f")
BEFORE:  "23 hours, 59 minutes and 59 seconds"
AFTER:   "23 hours, 59 minutes and 59.9990 seconds"

Test cases added to test_precisedelta_multiple_units

>>> precisedelta(dt.timedelta(days=31), "seconds")
BEFORE:  "1 month and 0 days"
AFTER:   "1 month and 12 hours"
>>> precisedelta(dt.timedelta(days=32), "seconds")
BEFORE:  "1 month and 1 days"
AFTER:   "1 month, 1 day and 12 hours"
>>> precisedelta(dt.timedelta(days=62), "seconds")
BEFORE:  "2 months and 1 day"
AFTER:   "2 months and 1 day"  # no change
>>> precisedelta(dt.timedelta(days=92), "seconds")
BEFORE:  "3 months and 0 days"
AFTER:   "3 months and 12 hours"
>>> precisedelta(dt.timedelta(days=31), "days")
BEFORE:  "1 month and 0.50 days"
AFTER:   "1 month and 0.50 days"  # no change
>>> precisedelta(dt.timedelta(days=32), "days")
BEFORE:  "1 month and 1.50 days"
AFTER:   "1 month and 1.50 days"  # no change
>>> precisedelta(dt.timedelta(days=62), "days")
BEFORE:  "2 months and 1 day"
AFTER:   "2 months and 1 day"  # no change
>>> precisedelta(dt.timedelta(days=92), "days")
BEFORE:  "3 months and 0.50 days"
AFTER:   "3 months and 0.50 days"  # no change

codecov-commenter commented Jul 2, 2022
edited by codecov Bot
Loading

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.41%. Comparing base (e705e43) to head (9ff98f7).
⚠️ Report is 421 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
- Coverage   99.13%   97.41%   -1.73%     
==========================================
  Files           9        9              
  Lines         690      695       +5     
==========================================
- Hits          684      677       -7     
- Misses          6       18      +12     
Flag Coverage Δ
macos-latest 97.41% <100.00%> (+0.01%) ⬆️
ubuntu-latest 97.41% <100.00%> (+0.01%) ⬆️
windows-latest ?

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment was marked as outdated.

hugovk added the changelog: Fixed For any bug fixes label Jul 4, 2022
nuztalgia force-pushed the precisedelta-rounding branch from e2f2dad to 9ff98f7 Compare July 11, 2022 18:20

nuztalgia commented Jul 11, 2022
edited
Loading

Copy link
Copy Markdown
Contributor Author

(Force-pushed to clean up the commit history, since it was getting a bit long. 😅 Let me know if you'd like me to squash it down even more!)

This PR now includes one of the commits from #19, to fix the root cause of #14. Thank you, @eldipa! 💜

I've also updated the description (#39 (comment)) to reflect all of the test cases that this PR adds and fixes.

nuztalgia mentioned this pull request Jul 11, 2022
1 task

Copy link
Copy Markdown
Contributor

@hugovk I think this PR could be closed. #254 was merged and superseded this one.

hugovk commented Nov 7, 2025

Copy link
Copy Markdown
Member

Thanks both!

hugovk closed this Nov 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For any bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

precisedelta: 1 hour and 1 minutes Issues with odd months when restricting to "day" precision (precisedelta)

5 participants


Back | FazBrowse Home | New Git URL