Fix humanize reporting "a year ago" for ~31-day deltas in same month#1247
Open
bysiber wants to merge 2 commits intoarrow-py:masterfrom
Open
Fix humanize reporting "a year ago" for ~31-day deltas in same month#1247bysiber wants to merge 2 commits intoarrow-py:masterfrom
bysiber wants to merge 2 commits intoarrow-py:masterfrom
Conversation
When the time difference exceeds _SECS_PER_MONTH (30.5 days) but calendar_months is still 0 (both dates in the same calendar month), no branch in the if/elif chain matched: - "weeks" requires diff < _SECS_PER_MONTH -> False - "months" requires calendar_months >= 1 -> False Execution fell through to the "year" branch, so a ~31-day difference within the same month reported "a year ago". This affects all 7 months with 31 days (Jan, Mar, May, Jul, Aug, Oct, Dec) when comparing dates near the start and end of the same month. Add an explicit branch for this case that reports "a month" when diff >= _SECS_PER_MONTH but calendar_months < 1.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1247 +/- ##
===========================================
- Coverage 100.00% 99.91% -0.09%
===========================================
Files 10 10
Lines 2315 2317 +2
Branches 358 359 +1
===========================================
Hits 2315 2315
- Misses 0 1 +1
- Partials 0 1 +1 ☔ View full report in Codecov by Sentry. |
Member
|
@bysiber please add tests, otherwise this looks to be good |
Author
|
Added a test, it checks the case where you have a ~31-day delta between Jan 15 and Feb 15, where calendar_months can round to 0 but the actual elapsed time exceeds one month. Without the fix, this case fell through to the wrong branch. |
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
Fix a gap in the
humanize()if/elifchain that causes~31-daydeltas within the same calendar month to report "a year ago" instead of "a month ago".Problem
In the auto-granularity branch of
humanize(), there's a gap between the "weeks" and "months" conditions:When
diff >= _SECS_PER_MONTH(30.5 days) andcalendar_months < 1(both dates in the same calendar month), neither the "weeks" nor the "months" condition is satisfied:diff < _SECS_PER_MONTH→ Falsecalendar_months >= 1→ FalseExecution falls through to the "a year ago" branch.
Reproduction
This affects all 7 months with 31 days (January, March, May, July, August, October, December) when comparing timestamps near the boundaries of the same month.
Fix
Add an explicit branch for this case: when
calendar_months < 1butdiff >= _SECS_PER_MONTH, report the delta as "a month".