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

Reject text after matching string in from_format() #372

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
2 changes: 1 addition & 1 deletion pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def parse(
lambda m: self._replace_tokens(m.group(0), locale), escaped_fmt
)

if not re.match(pattern, time):
if not re.search("^" + pattern + "$", time):
raise ValueError("String does not match format {}".format(fmt))

re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)
Expand Down
5 changes: 5 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def test_from_format_returns_datetime():
assert "UTC" == d.timezone_name


def test_from_format_rejects_extra_text():
with pytest.raises(ValueError):
pendulum.from_format("1975-05-21 22:32:11 extra text", "YYYY-MM-DD HH:mm:ss")


def test_from_format_with_timezone_string():
d = pendulum.from_format(
"1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz="Europe/London"
Expand Down