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

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

nuztalgia
Copy link
Contributor

@nuztalgia nuztalgia commented Jul 2, 2022

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
Copy link

codecov-commenter commented Jul 2, 2022

Codecov Report

Merging #39 (e2f2dad) into main (e705e43) will decrease coverage by 2.58%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
- Coverage   99.13%   96.54%   -2.59%     
==========================================
  Files           9        9              
  Lines         690      694       +4     
==========================================
- Hits          684      670      -14     
- Misses          6       24      +18     
Flag Coverage Δ
macos-latest 96.54% <100.00%> (-0.85%) ⬇️
ubuntu-latest ?
windows-latest ?

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

Impacted Files Coverage Δ
src/humanize/time.py 99.54% <100.00%> (+<0.01%) ⬆️
tests/test_time.py 100.00% <100.00%> (ø)
src/humanize/__init__.py 80.00% <0.00%> (-20.00%) ⬇️
tests/test_i18n.py 87.50% <0.00%> (-12.50%) ⬇️
src/humanize/i18n.py 90.56% <0.00%> (-7.55%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e705e43...e2f2dad. Read the comment docs.

@nuztalgia

This comment was marked as outdated.

@hugovk hugovk added the changelog: Fixed For any bug fixes label Jul 4, 2022
@nuztalgia
Copy link
Contributor Author

nuztalgia commented Jul 11, 2022

(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 nuztalgia mentioned this pull request Jul 11, 2022
1 task
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.

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