Skip to content

parse returns wrong month when using RELATIVE_BASE #1266

@dvklopfenstein

Description

@dvklopfenstein

Thank you for creating such a useful package and for sharing it with all of us.

I am seeing the wrong month returned if I use a relative base of Wednesday, January 1st, 2025 and the string, "Sun 9am." I expect to see Sunday, December 29th, 2024.

CALENDAR

For reference, see the calendar view surrounding RELATIVE_BASE day, Wed Jan 1, 2025:

    January 2025       # Jan 2025 is RELATIVE_BASE month
Su Mo Tu We Th Fr Sa   # EXPECTED for "Sun 9am": Sun Dec 29, 2024
29 30 31  1  2  3  4   # RELATIVE_BASE is Wed Jan 1, 2025
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31      

$ cal 1 1 2024        # Jan 2024 (ONE YEAR before RELATIVE_BASE, Wed Jan 1, 2025)
    January 2024
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31            # ACTUAL for "Sun 9am": Mon??? _Jan_ 29, 2024

EXPECTED BEHAVIOR

date = parse("Sun 9am", settings={'RELATIVE_BASE': datetime(2025, 1, 1)})
2025-01-01 00:00:00: RELATIVE_BASE
2024-12-29 09:00:00: "Sun 9am"

ACTUAL BEHAVIOR

date = parse("Sun 9am", settings={'RELATIVE_BASE': datetime(2025, 1, 1)})
2025-01-01 00:00:00: RELATIVE_BASE
2024-01-29 09:00:00: "Sun 9am"

1/29/2024 is not a Sunday, it is a Monday
And the 1/29/2024 is a year too early compared to the Jan 1, 2025 relative date

CODE TO REPLICATE THE ERROR

from datetime import datetime
from dateparser import parse


def test_dateparser():
    """Test dateparser"""
    base = datetime(2025, 1, 1)
    timestr = 'Sun 9am'
    date = parse(timestr, settings={'RELATIVE_BASE': base})
    print(f'{base}: RELATIVE_BASE')
    print(f'{date}: "{timestr}"')

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