Skip to content

Preserve sub-day seconds for precisedelta month and year minimum units - #436

Open
PDGGK wants to merge 1 commit into
python-humanize:mainfrom
PDGGK:fix-precisedelta-coarse-unit-seconds
Open

PDGGK wants to merge 1 commit into
python-humanize:mainfrom
PDGGK:fix-precisedelta-coarse-unit-seconds

Conversation

@PDGGK

@PDGGK PDGGK commented Oct 3, 2026

Copy link
Copy Markdown

Changes proposed in this pull request:

  • Preserve sub-day elapsed seconds when rounding precisedelta to a minimum unit of months or years.
  • Divide the whole elapsed-second count directly by the scaled month/year divisor, then consume the lower components. This avoids both dropped seconds and intermediate double rounding.
  • Add regressions for fractional units, short durations, and rounding midpoints. Keep the existing 30.5-day month, 365-day year, negative-duration behavior, and documented microsecond-precision limitation.

Before and after

from datetime import timedelta
from humanize import precisedelta

precisedelta(timedelta(days=15, hours=6), minimum_unit="months", format="%.2f")
# Before: '0.49 months'
# After:  '0.50 months'

precisedelta(timedelta(days=30, hours=12), minimum_unit="months", format="%.2f")
# Before: '0.98 months'
# After:  '1 month'

precisedelta(timedelta(days=182, hours=12), minimum_unit="years", format="%.4f")
# Before: '0.4986 years'
# After:  '0.5000 years'

precisedelta(timedelta(hours=12), minimum_unit="years", format="%.2f")
# Before: '0 years and 43200000000 microseconds'
# After:  '0 years'

Validation

Linux, CPython 3.12.14:

  • Final full suite and source doctests: 883 passed, 2 translation-update tests skipped because GNU xgettext is unavailable.
  • Final focused selection: 63 passed; original production and production-only revert each reproduce the same 13 failures with 50 passing controls.
  • humanize.time statement coverage: 100%.
  • Unchanged configured prek hooks, Ruff, Black, strict documentation build, and serial mypy pass. Default multi-worker mypy is blocked by sandbox IPC sockets.

No Windows/macOS/other-Python matrix or hosted-CI result is claimed. Benchmark functions execute with timing disabled, so this is not a performance measurement.

AI assistance: implementation, tests, and this description were prepared with OpenAI assistance.

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.

1 participant