Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing plain year numbers like "2024" with "PREFER..."-settings set to "last" returns wrong date. 1 day missing... #1228

Open
Zeutschler opened this issue Jun 9, 2024 · 4 comments

Comments

@Zeutschler
Copy link

print(dateparser.parse(
date_string="2024",
settings={"STRICT_PARSING": False,
"PREFER_DAY_OF_MONTH": "last",
"PREFER_MONTH_OF_YEAR": "last",
"PREFER_DATES_FROM": "past"}))
2024-12-30 00:00:00

It seems you're skipping a day. It should be 2024-12-31 00:00:00

...running on Macbook Air M2, Python 3.11 with

import locale
print(locale.getlocale())
(None, 'UTF-8')

@gguimezanes
Copy link

Same problem here on windows, using only
settings={
"PREFER_DAY_OF_MONTH": "last",
"PREFER_MONTH_OF_YEAR": "last"
}

@lluissalord
Copy link

I am also experiencing this with the following minimal code on dateparser version is 1.2.0:

import dateparser
dateparser_settings = {
    "PREFER_MONTH_OF_YEAR": (
        "last"
    ),
    "PREFER_DAY_OF_MONTH": (
        "last"
    ),
    "PARSERS": ["absolute-time"],
}

value = "2024"
parsed_date = dateparser.parse(
    value, settings=dateparser_settings
) # output: datetime.datetime(2024, 12, 30, 0, 0)

@lluissalord
Copy link

lluissalord commented Sep 2, 2024

Investigating a bit more and testing it with time_machine library. I've been able to check that the output seems to depend on the month when you are executing the code:

Code:

import time_machine

for month in range(1,13):
    travel_date = f"2024-{month}-15"
    with time_machine.travel(travel_date):
        import dateparser
        dateparser_settings = {
            "PREFER_MONTH_OF_YEAR": (
                "last"
            ),
            "PREFER_DAY_OF_MONTH": (
                "last"
            ),
            "PARSERS": ["absolute-time"],
        }

        value = "2024"
        parsed_date = dateparser.parse(
            value, settings=dateparser_settings
        )
        print(f"Current date: {travel_date}. Last year date: {parsed_date}")

Output:

Current date: 2024-1-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-2-15. Last year date: 2024-12-29 00:00:00
Current date: 2024-3-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-4-15. Last year date: 2024-12-30 00:00:00
Current date: 2024-5-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-6-15. Last year date: 2024-12-30 00:00:00
Current date: 2024-7-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-8-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-9-15. Last year date: 2024-12-30 00:00:00
Current date: 2024-10-15. Last year date: 2024-12-31 00:00:00
Current date: 2024-11-15. Last year date: 2024-12-30 00:00:00
Current date: 2024-12-15. Last year date: 2024-12-31 00:00:00

@Zeutschler
Copy link
Author

@lluissalord : That's a perfect proof for a funny bug! Thx

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

No branches or pull requests

3 participants