Skip to content

Comments

fix: handle tz-aware datetimes in naturalday and naturaldate#297

Merged
hugovk merged 1 commit intopython-humanize:mainfrom
bysiber:fix-naturaldate-tz-aware
Feb 22, 2026
Merged

fix: handle tz-aware datetimes in naturalday and naturaldate#297
hugovk merged 1 commit intopython-humanize:mainfrom
bysiber:fix-naturaldate-tz-aware

Conversation

@bysiber
Copy link
Contributor

@bysiber bysiber commented Feb 21, 2026

Fixes #152

When a tz-aware datetime is passed to naturalday() or naturaldate(), the functions now compute "today" in the datetime's timezone instead of using the system's local date.

For example, if the current UTC time is 2023-10-15 23:00 and you pass a datetime in AEDT (+11:00) where the local date is already Oct 16th, the comparison should use Oct 16th as "today" for that timezone. Previously the timezone was stripped and compared against the system local date, which gave wrong results when the tz-local date differed from the system date.

The fix checks whether the value is a tz-aware datetime and, if so, calls datetime.now(value.tzinfo).date() instead of date.today(). The existing behavior for naive datetimes and plain date objects is unchanged.

Test included using the exact scenarios from the issue.

When a tz-aware datetime is passed to naturalday() or naturaldate(),
the date comparison should use 'today' in the datetime's timezone
rather than the system's local date.

Previously the timezone was stripped and the date compared against
dt.date.today(), which could yield wrong results (e.g. returning
'tomorrow' when the local date in the given timezone is actually
the same day).

Fixes python-humanize#152
@hugovk hugovk added the changelog: Fixed For any bug fixes label Feb 22, 2026
@codecov
Copy link

codecov bot commented Feb 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.52%. Comparing base (471cd9b) to head (b7aa0a2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #297      +/-   ##
==========================================
+ Coverage   99.51%   99.52%   +0.01%     
==========================================
  Files          11       11              
  Lines         831      851      +20     
==========================================
+ Hits          827      847      +20     
  Misses          4        4              
Flag Coverage Δ
macos-latest 97.88% <100.00%> (+0.05%) ⬆️
ubuntu-latest 97.88% <100.00%> (+0.05%) ⬆️
windows-latest 96.00% <100.00%> (+0.09%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk
Copy link
Member

hugovk commented Feb 22, 2026

Please see also similar PR #296, review it, and say which you think is better.

@hugovk hugovk merged commit a47a89e into python-humanize:main Feb 22, 2026
43 of 44 checks passed
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.

naturaldate() gives the wrong answer for tz-aware datetimes

2 participants