Skip to content

fix: apply modifier (ago/before/next) to all numbers in chunk - #297

Open
binggao1230 wants to merge 1 commit into
bear:masterfrom
binggao1230:fix/ago-apply-to-all-numbers
Open

fix: apply modifier (ago/before/next) to all numbers in chunk#297
binggao1230 wants to merge 1 commit into
bear:masterfrom
binggao1230:fix/ago-apply-to-all-numbers

Conversation

@binggao1230

@binggao1230 binggao1230 commented Jun 28, 2026

Copy link
Copy Markdown

When a modifier like "ago" appears after multiple quantity-unit pairs
(e.g. "11 hours 0 min ago"), only the last number was being negated.
"11 hours 0 min ago" would incorrectly parse as +11 hours instead of
-11 hours, because the modifier was only applied to the "0" while the
"11" was left unchanged.

Root cause

In _evalModifier, when the else branch processes chunk1 (the text
before the modifier), it finds all numbers via CRE_NUMBER.finditer
but only picks [-1] (the last match). For single-unit expressions
like "11 hours ago" this is fine, but for multi-unit expressions like
"11 hours 0 min ago" it misses the "11".

Fix

Iterate over all matched numbers in reverse order (to preserve string
positions) and apply the modifier offset to each quantity.

Tests

Added testMultipleUnitsAgo to tests/TestDelta.py covering:

  • "11 hours 0 minutes ago"
  • "2 days 3 hours ago"
  • "1 day 5 hours 30 mins ago"

All 85 tests pass (84 existing + 1 new).


This PR was prepared with assistance from an AI coding tool.


This change is Reviewable

When a modifier like "ago" appears after multiple quantity-unit pairs
(e.g. "11 hours 0 min ago"), only the last number was negated. This
caused wrong results: "11 hours 0 min ago" would parse as +11 hours
instead of -11 hours.

Fix by iterating over all numbers in reverse order (to preserve
string positions) and applying the modifier to each one.
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.

1 participant