Skip to content

feat(time): add optional weekday parameter to naturalday (#102) - #377

Open
harmehak0173 wants to merge 2 commits into
python-humanize:mainfrom
harmehak0173:feature/issue-102-naturalday-weekday
Open

feat(time): add optional weekday parameter to naturalday (#102)#377
harmehak0173 wants to merge 2 commits into
python-humanize:mainfrom
harmehak0173:feature/issue-102-naturalday-weekday

Conversation

@harmehak0173

Copy link
Copy Markdown

Summary of Changes

Adds an optional weekday: bool = False parameter to naturalday() to format dates within a 6-day window using weekday names (e.g. "this Saturday", "last Sunday") (#102).

Changes Implemented

  1. Updated naturalday(value, format="%b %d", *, weekday=False) in src/humanize/time.py:
    • Dates 2–6 days in the future return _("this %s") % day_name (e.g. "this Saturday").
    • Dates 2–6 days in the past return _("last %s") % day_name (e.g. "last Sunday").
    • today, tomorrow, and yesterday remain unchanged.
  2. Added comprehensive unit test test_naturalday_weekday in tests/test_time.py.

Testing

  • pytest tests/test_time.py: All 384 tests passed.

@Mukller Mukller left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified locally on the PR branch (Python 3.13, editable install), frozen at Wed 2026-08-12:

Full boundary matrix for naturalday(d, weekday=True):

offset result
today / yesterday / tomorrow today / yesterday / tomorrow (existing behavior untouched)
-2d last Monday
-6d last Thursday
-7d falls back to format (Aug 05) — correct out-of-range boundary
+2d this Friday
+6d this Tuesday
+7d falls back to format — correct
weekday=False (default) identical to master for all inputs

Master control: weekday=True raises TypeError: unexpected keyword argument — purely additive, zero risk to existing callers. The new strings are wrapped in _() so they flow through humanize's i18n catalog like the rest of the module.

pytest tests/test_time.py383 passed, including the new freeze_time-based tests.

Two non-blocking notes:

  1. Boundary asymmetry is intentional-looking but worth a docs sentence: past side covers [-6, -2] and future [2, +6], while ±1 are absorbed by yesterday/tomorrow and ±7+ fall through. The docstring documents it accurately; just make sure the README example (if added later) matches.
  2. %A uses locale-aware day names — consistent with the rest of humanize, but non-English locales will produce e.g. "last понедельник" unless translations cover last %s/this %s. Worth one line in the PR description so translators know new keys exist.

Feature is coherent and well-tested. Approving.

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