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

Add support for negative timestamp #1060

Merged
merged 5 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(dateparser/{conf, date}.py): add negative-timestamp as optional p…
…arameter
  • Loading branch information
gutsytechster committed Jun 11, 2022
commit 23ca1f3acdc95d9a78db300d56d623eecc43a318
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