scrapinghub / scrapinghub/dateparser
'1,000,000 days ago' parses as the current time; thousands separators truncate the relative number
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.9k
- Forks
- 520
- Avg merge
- 22h 56m
- Merged PRs (30d)
- 6
Description
A number with a thousands separator parses as a much smaller relative date. With two separators it comes back as the base time.
from datetime import datetime
import dateparser
base = datetime(2024, 6, 15, 12, 0, 0)
S = {"RELATIVE_BASE": base}
dateparser.parse("1000 days ago", settings=S) # 2021-09-19 correct
dateparser.parse("1,000 days ago", settings=S) # 2024-06-14 one day
dateparser.parse("12,345 days ago", settings=S) # 2024-06-03 12.345 days
dateparser.parse("1,000,000 days ago", settings=S) # 2024-06-15 12:00, the base, unchanged
The first three follow from float(num.replace(",", ".")) in get_kwargs, and #876 added that on purpose so "1,5 hours" works. That should stay. In a locale where the comma is a decimal mark those readings are right, and there is no locale information at that point to tell the two apart.
The last one is different. Translation rewrites the string first:
"1,000,000 days ago" -> "1 000,000 day ago"
PATTERN.findall -> [(" 000,000", "day")]
float("000.000") -> 0.0
The leading 1 is dropped, not misread. The \s* in ([+-]?\s*\d++[.,]?\d*+), which is there for "+ 5 days", lets the match start partway through the number. No reading of "1,000,000 days ago" gives the current time, so this one is wrong in every locale.
Worth deciding together, since a fix for the second probably settles the first. The options I can see are to use the detected locale for the separator, treat a separator followed by exactly three digits as grouping, or fail instead of returning a truncated number. Happy to send a patch once you say which way you want it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the examples with RELATIVE_BASE, then inspect get_kwargs and the translation step before PATTERN.findall. Trace how the \s* in the numeric pattern handles grouped numbers and compare the existing comma-decimal behavior. Done means the multi-separator case cannot silently truncate or return the unchanged base time, while the intended decimal behavior remains covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100