+ import itertools
+
+ import dateparse.dateparse_utils as du
+
+ d = du.DateParser() # type: ignore
+ chunks = in_str.split()
+ for ngram in itertools.chain(
+ list_utils.ngrams(chunks, 5),
+ list_utils.ngrams(chunks, 4),
+ list_utils.ngrams(chunks, 3),
+ list_utils.ngrams(chunks, 2),
+ ):
+ try:
+ expr = " ".join(ngram)
+ logger.debug(f"Trying {expr}")
+ if d.parse(expr):
+ return d.get_datetime()
+ except du.ParseException: # type: ignore
+ pass
+ return None
+
+
+def is_valid_date(in_str: str) -> bool:
+ """
+ Args:
+ in_str: the string to check
+
+ Returns:
+ True if the string represents a valid date that we can recognize
+ and False otherwise. This parser is relatively clever; see
+ :class:`python_modules.dateparse.dateparse_utils` docs for
+ details.
+
+ >>> is_valid_date('1/2/2022')
+ True
+ >>> is_valid_date('christmas')
+ True
+ >>> is_valid_date('next wednesday')
+ True
+ >>> is_valid_date('xyzzy')
+ False