Fix naturalday/naturaldate wrong answer for tz-aware datetimes#296
Open
Fridayai700 wants to merge 2 commits intopython-humanize:mainfrom
Open
Fix naturalday/naturaldate wrong answer for tz-aware datetimes#296Fridayai700 wants to merge 2 commits intopython-humanize:mainfrom
Fridayai700 wants to merge 2 commits intopython-humanize:mainfrom
Conversation
When a tz-aware datetime is passed, naturalday() and naturaldate() extract the date in the value's timezone but compare it with date.today() which uses system local time. This produces wrong results when the value's timezone differs from the system timezone. Fix: capture the value's tzinfo before converting to a plain date, then derive "today" via datetime.now(tzinfo).date() so both dates are in the same timezone. Fixes python-humanize#152 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #296 +/- ##
==========================================
+ Coverage 99.51% 99.53% +0.01%
==========================================
Files 11 11
Lines 831 859 +28
==========================================
+ Hits 827 855 +28
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a tz-aware datetime is passed,
naturalday()andnaturaldate()extract the date in the value's timezone (viadt.date(value.year, value.month, value.day)) but compare it withdt.date.today()which uses system local time. This produces wrong results when the value's timezone differs from the system timezone.Example from the issue: When the system is in UTC and it's Oct 15 23:00 UTC (Oct 16 10:00 AEDT), calling
naturaldate()with an AEDT datetime on Oct 16 should return "today" (both dates are Oct 16 in AEDT), but instead returns "tomorrow" becausedate.today()returns Oct 15 (the system's UTC date).Fix
Capture the value's
tzinfobefore converting to a plain date. If a timezone is present, derive "today" viadatetime.now(tzinfo).date()so both dates are in the same timezone.Test plan
test_naturalday_tz_aware— tests UTC, AEDT (+11), EDT (-4), PDT (-7) against a future datetime, verifying today/tomorrow is computed relative to each timezonetest_naturaldate_tz_aware— same scenario throughnaturaldate()Fixes #152