Skip to content

Add 'no_correction' option for PREFER_DAY_OF_MONTH and PREFER_MONTH_OF_YEAR #1263

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

histogal
Copy link

@histogal histogal commented Apr 3, 2025

In cases where only the day of the week is input, for example, 'Monday', without the day and month, the set_correct_day_from_settings and set_correct_month_from_settings functions can produce an incorrect date.

I added a 4th option for the PREFER_DAY_OF_MONTH and PREFER_MONTH_OF_YEAR settings - no_correction.

It is intended for use in cases where day and month correction is not required.

Use example:

from dateparser import parse
from datetime import datetime

print(
    parse(
        "Mon 01:11",
        settings={"PREFER_DATES_FROM": "past", "RELATIVE_BASE": datetime(2025, 4, 3)},
    )
)
# Expected '2025-03-31 01:11:00'. Got '2025-12-31 01:11:00'
print(
    parse(
        "Mon 01:11",
        settings={
            "PREFER_DATES_FROM": "past",
            "PREFER_DAY_OF_MONTH": "no_correction",
            "PREFER_MONTH_OF_YEAR": "no_correction",
            "RELATIVE_BASE": datetime(2025, 4, 3),
        },
    )
)
# '2025-03-31 01:11:00'

days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

for day in days:

    print(
        f"""'{day}' - wrong with corretion {
    parse(
        day,
        settings={"PREFER_DATES_FROM": "past", "RELATIVE_BASE": datetime(2025, 4, 1)},
    )
} - w/o correction {
    parse(
        day,
        settings={
            "PREFER_DATES_FROM": "past",
            "PREFER_DAY_OF_MONTH": "no_correction",
            "PREFER_MONTH_OF_YEAR": "no_correction",
            "RELATIVE_BASE": datetime(2025, 4, 1),
        },
    )}"""
    )
# 'monday' - wrong with corretion 2025-12-31 00:00:00 - w/o correction 2025-03-31 00:00:00
# 'tuesday' - wrong with corretion 2025-04-25 00:00:00 - w/o correction 2025-03-25 00:00:00
# 'wednesday' - wrong with corretion 2025-04-26 00:00:00 - w/o correction 2025-03-26 00:00:00
# 'thursday' - wrong with corretion 2025-04-27 00:00:00 - w/o correction 2025-03-27 00:00:00
# 'friday' - wrong with corretion 2025-04-28 00:00:00 - w/o correction 2025-03-28 00:00:00
# 'saturday' - wrong with corretion 2025-04-29 00:00:00 - w/o correction 2025-03-29 00:00:00
# 'sunday' - wrong with corretion 2025-04-30 00:00:00 - w/o correction 2025-03-30 00:00:00

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