Skip to content

Commit

Permalink
fix(dateparser/{conf, date}.py): add negative-timestamp as optional p…
Browse files Browse the repository at this point in the history
…arameter
  • Loading branch information
gutsytechster committed Jun 11, 2022
1 parent 7d59127 commit 23ca1f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dateparser/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _check_require_part(setting_name, setting_value):
def _check_parsers(setting_name, setting_value):
"""Returns `True` if the provided list of parsers contains valid values"""
existing_parsers = [
'timestamp', 'relative-time', 'custom-formats', 'absolute-time', 'no-spaces-time'
'timestamp', 'relative-time', 'custom-formats', 'absolute-time', 'no-spaces-time', 'negative-timestamp'
] # FIXME: Extract the list of existing parsers from another place (#798)
unknown_parsers = set(setting_value) - set(existing_parsers)
if unknown_parsers:
Expand Down
20 changes: 15 additions & 5 deletions dateparser/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
RE_SANITIZE_APOSTROPHE = re.compile('|'.join(APOSTROPHE_LOOK_ALIKE_CHARS))

RE_SEARCH_TIMESTAMP = re.compile(r'^(\d{10})(\d{3})?(\d{3})?(?![^.])')
RE_SEARCH_NEGATIVE_TIMESTAMP = re.compile(r'^([-]?\d{10})(\d{3})?(\d{3})?(?![^.])')
RE_SEARCH_NEGATIVE_TIMESTAMP = re.compile(r'^([-]\d{10})(\d{3})?(\d{3})?(?![^.])')


def sanitize_spaces(date_string):
Expand Down Expand Up @@ -113,8 +113,11 @@ def sanitize_date(date_string):
return date_string


def get_date_from_timestamp(date_string, settings):
match = RE_SEARCH_NEGATIVE_TIMESTAMP.search(date_string)
def get_date_from_timestamp(date_string, settings, negative=False):
match = RE_SEARCH_TIMESTAMP.search(date_string)
if negative:
match = RE_SEARCH_NEGATIVE_TIMESTAMP.search(date_string)

if match:
seconds = int(match.group(1))
millis = int(match.group(2) or 0)
Expand Down Expand Up @@ -167,6 +170,7 @@ def __init__(self, locale, date_string, date_formats, settings=None):
self._translated_date_with_formatting = None
self._parsers = {
'timestamp': self._try_timestamp,
'negative-timestamp': self._try_negative_timestamp,
'relative-time': self._try_freshness_parser,
'custom-formats': self._try_given_formats,
'absolute-time': self._try_absolute_parser,
Expand All @@ -186,12 +190,18 @@ def _parse(self):
else:
return None

def _try_timestamp(self):
def _try_timestamp_parser(self, negative=False):
return DateData(
date_obj=get_date_from_timestamp(self.date_string, self._settings),
date_obj=get_date_from_timestamp(self.date_string, self._settings, negative=negative),
period='time' if self._settings.RETURN_TIME_AS_PERIOD else 'day',
)

def _try_timestamp(self):
return self._try_timestamp_parser()

def _try_negative_timestamp(self):
return self._try_timestamp_parser(negative=True)

def _try_freshness_parser(self):
try:
return freshness_date_parser.get_date_data(self._get_translated_date(), self._settings)
Expand Down

0 comments on commit 23ca1f3

Please sign in to comment.