Skip to content

'1,000,000 days ago' parses as the current time; thousands separators truncate the relative number #1367

Description

@toolshedlabs-hash

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions