Skip to content

Correct past and future directions for datetime durations - #247

Open
saitakarcesme wants to merge 1 commit into
python-babel:masterfrom
saitakarcesme:codex/correct-relative-datetime-direction
Open

saitakarcesme wants to merge 1 commit into
python-babel:masterfrom
saitakarcesme:codex/correct-relative-datetime-direction

Conversation

@saitakarcesme

Copy link
Copy Markdown

Fixes #246.

With add_direction=True, a datetime one hour in the past currently produces in 1 hour, while a future datetime produces 1 hour ago. Subtract the current time from the supplied datetime so its sign follows Babel's duration convention. Direct timedelta values retain their existing meaning; output without direction stays the same.

Tests cover past/future datetime inputs, positive/negative timedelta controls, the Jinja timedeltaformat filter, and output without direction. The clock is fixed without adding a test dependency. The docstring and changelog describe the corrected behavior.

This addresses direction separately from the timezone-aware datetime support proposed in #233; that PR currently retains the old subtraction order. No timezone-awareness changes are included here.

Validation (macOS, Python 3.13.5, Flask 3.1.3, Babel 2.18.0, pytest 7.4.4):

  • Before: both new datetime direction cases fail; timedelta controls pass.
  • After: all 8 date-formatting tests pass.
  • Full suite: 34 pass, 3 fail. An untouched git archive HEAD baseline has 30 pass, the same 3 fail: test_list_translations, test_list_translations_default_locale_exists, and test_multiple_directories. All three assume directory iteration order; the existing Fix list-translations() ordering in tests #242 addresses ordering in these tests.
  • Black 24.10.0 and whitespace checks pass.
  • Sphinx 8.2.3 documentation build with -W passes.

Prepared and tested with OpenAI Codex.

This branch has not been deployed

No deployments
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.

Support remaining time in format_timedelta

1 participant