From 4af0ae9f8d0b81f6a73ae529b9c2c42bcd4100b7 Mon Sep 17 00:00:00 2001 From: harmehak0173 Date: Wed, 12 Aug 2026 14:14:41 -0400 Subject: [PATCH 1/2] feat(time): add optional weekday parameter to naturalday (#102) --- src/humanize/time.py | 17 ++++++++++++++--- tests/test_time.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 4a07d528..14f8a843 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -313,12 +313,15 @@ def _convert_aware_datetime( return value -def naturalday(value: dt.date | dt.datetime, format: str = "%b %d") -> str: +def naturalday( + value: dt.date | dt.datetime, format: str = "%b %d", *, weekday: bool = False +) -> str: """Return a natural day. For date values that are tomorrow, today or yesterday compared to - present day return representing string. Otherwise, return a string - formatted according to `format`. + present day return representing string. If `weekday` is True, dates within + 6 days past or future will return weekday names (e.g. 'this Monday', + 'last Friday'). Otherwise, return a string formatted according to `format`. """ import datetime as dt @@ -348,6 +351,14 @@ def naturalday(value: dt.date | dt.datetime, format: str = "%b %d") -> str: if delta.days == -1: return _("yesterday") + if weekday and 1 < delta.days <= 6: + day_name = value.strftime("%A") + return _("this %s") % day_name + + if weekday and -6 <= delta.days < -1: + day_name = value.strftime("%A") + return _("last %s") % day_name + return value.strftime(format) diff --git a/tests/test_time.py b/tests/test_time.py index 76997704..08700b23 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -852,3 +852,18 @@ def test_time_unit() -> None: ) def test_rounding_by_fmt(fmt: str, value: float, expected: float) -> None: assert time._rounding_by_fmt(fmt, value) == pytest.approx(expected) + + +def test_naturalday_weekday() -> None: + with freeze_time("2026-08-12"): # Wednesday + today = dt.date.today() + future_day = today + dt.timedelta(days=3) + assert humanize.naturalday(future_day, weekday=True) == "this Saturday" + + past_day = today - dt.timedelta(days=3) + assert humanize.naturalday(past_day, weekday=True) == "last Sunday" + + assert humanize.naturalday(today, weekday=True) == "today" + assert humanize.naturalday(today + dt.timedelta(days=1), weekday=True) == "tomorrow" + assert humanize.naturalday(today - dt.timedelta(days=1), weekday=True) == "yesterday" + From 38a27fd02677f96b1ffd3934cd2bf2f54f78e080 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Aug 2026 16:06:30 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_time.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_time.py b/tests/test_time.py index 08700b23..e9705ac4 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -864,6 +864,11 @@ def test_naturalday_weekday() -> None: assert humanize.naturalday(past_day, weekday=True) == "last Sunday" assert humanize.naturalday(today, weekday=True) == "today" - assert humanize.naturalday(today + dt.timedelta(days=1), weekday=True) == "tomorrow" - assert humanize.naturalday(today - dt.timedelta(days=1), weekday=True) == "yesterday" - + assert ( + humanize.naturalday(today + dt.timedelta(days=1), weekday=True) + == "tomorrow" + ) + assert ( + humanize.naturalday(today - dt.timedelta(days=1), weekday=True) + == "yesterday" + )