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

Added check for date-formats #1141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions dateparser/date.py
Expand Up @@ -438,6 +438,13 @@ def get_date_data(self, date_string, date_formats=None):
period='day', locale='en')

"""

if date_formats:
try:
validate_date_format(date_string, date_formats)
except InvalidDateString as e:
pass

if not isinstance(date_string, str):
raise TypeError('Input type must be str')

Expand Down Expand Up @@ -521,3 +528,14 @@ def _get_locale_loader(cls):
if not cls.locale_loader:
cls.locale_loader = LocaleDataLoader()
return cls.locale_loader

def validate_date_format(date_string, date_formats):
for date_format in date_formats:
try:
datetime.strptime(date_string, date_format)
return
except ValueError:
raise InvalidDateString("Date string does not match any of the given formats")

class InvalidDateString(Exception):
pass