Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dateparser/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from dateparser.parser import _parse_absolute, _parse_nospaces
from dateparser.timezone_parser import pop_tz_offset_from_string
from dateparser.utils import (
_get_missing_parts,
apply_timezone_from_settings,
get_timezone_from_tz_string,
set_correct_day_from_settings,
Expand Down Expand Up @@ -187,8 +188,9 @@ def parse_with_formats(date_string, date_formats, settings):
except ValueError:
continue
else:
missing_month = not any(m in date_format for m in ["%m", "%b", "%B"])
missing_day = "%d" not in date_format
_missing = _get_missing_parts(date_format)
missing_month = "month" in _missing
missing_day = "day" in _missing
if missing_month and missing_day:
period = "year"
date_obj = set_correct_month_from_settings(date_obj, settings)
Expand Down
4 changes: 3 additions & 1 deletion dateparser/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def _get_missing_parts(fmt):
"""
directive_mapping = {
"day": ["%d", "%-d", "%j", "%-j"],
"month": ["%b", "%B", "%m", "%-m"],
# %j (day of year) encodes month implicitly: a successful strptime with %j always
# populates the month field, so %j should count as providing month information.
"month": ["%b", "%B", "%m", "%-m", "%j", "%-j"],
"year": ["%y", "%-y", "%Y"],
}

Expand Down
28 changes: 28 additions & 0 deletions tests/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,34 @@ def test_should_parse_date(self, date_string, date_formats, expected_result):
self.then_parsed_period_is("day")
self.then_parsed_date_is(expected_result)

@parameterized.expand(
[
param(
date_string="2023-100",
date_formats=["%Y-%j"],
expected_result=datetime(2023, 4, 10),
),
param(
date_string="2024-060",
date_formats=["%Y-%j"],
expected_result=datetime(2024, 2, 29),
),
param(
date_string="2023 060",
date_formats=["%Y %j"],
expected_result=datetime(2023, 3, 1),
),
]
)
Comment on lines +377 to +395
def test_should_parse_day_of_year_format(
self, date_string, date_formats, expected_result
):
"""Format %%j (day of year) should correctly set both month and day."""
self.when_date_is_parsed_with_formats(date_string, date_formats)
self.then_date_was_parsed()
self.then_parsed_period_is("day")
self.then_parsed_date_is(expected_result)

@parameterized.expand(
[
param(
Expand Down
Loading