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
18 changes: 17 additions & 1 deletion dateparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,23 @@ def set_and_return(token, type, component, dateobj, skip_date_order=False):
def parse_number(token, skip_component=None):
type = 0

for component, directives in self.ordered_num_directives.items():
num_directives = self.ordered_num_directives
if (
skip_component == "year"
and self.day is None
and self.month is None
and "DATE_ORDER" not in self.settings._mod_settings
):
# The date string starts with a four-digit year (e.g. an
# ISO 8601 date like "2017-06-22"), so the remaining numeric
# components are expected in month-day order, regardless of
# the locale date order, unless the caller explicitly set
# DATE_ORDER (#360).
num_directives = {
k: self.num_directives[k] for k in ("month", "day", "year")
}

for component, directives in num_directives.items():
if skip_component == component:
continue
for directive in directives:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,32 @@ def test_iso_datestamp_format_should_always_parse(
self.result["date_obj"] = self.result["date_obj"].replace(tzinfo=None)
self.then_date_obj_exactly_is(expected)

@parameterized.expand(
[
param("2017-06-22", languages=["it"], expected=datetime(2017, 6, 22)),
param("2017-06-10", languages=["it"], expected=datetime(2017, 6, 10)),
param("2017-06-22", languages=["fr"], expected=datetime(2017, 6, 22)),
param(
"2015-05-02T10:20:19+0000",
languages=["fr"],
expected=datetime(2015, 5, 2, 10, 20, 19),
),
]
)
def test_iso_datestamp_format_should_parse_with_locale_date_order(
self, date_string, languages, expected
):
# Regression test for #360: dates starting with a four-digit year
# (e.g. ISO 8601 dates) must parse even when the locale date order
# is not year-first (Italian and French use DMY), without needing
# the PREFER_LOCALE_DATE_ORDER=False workaround.
self.given_local_tz_offset(0)
self.given_parser(languages=languages)
self.when_date_is_parsed(date_string)
self.then_date_was_parsed_by_date_parser()
self.result["date_obj"] = self.result["date_obj"].replace(tzinfo=None)
self.then_date_obj_exactly_is(expected)

@parameterized.expand(
[
# Epoch timestamps.
Expand Down
Loading