projects
/
python_utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
82e1225
)
Docs and test.
author
Scott Gasch
<
[email protected]
>
Thu, 9 Jun 2022 00:29:57 +0000
(17:29 -0700)
committer
Scott Gasch
<
[email protected]
>
Thu, 9 Jun 2022 00:29:57 +0000
(17:29 -0700)
string_utils.py
patch
|
blob
|
history
diff --git
a/string_utils.py
b/string_utils.py
index 7c40dc99cb978a1d527ee42b9082dd4bede955f5..24fc59542ba96614e364c0331d9db738258bfa32 100644
(file)
--- a/
string_utils.py
+++ b/
string_utils.py
@@
-1388,7
+1388,21
@@
def to_date(in_str: str) -> Optional[datetime.date]:
return None
return None
-def extract_date(in_str: Any) -> Optional[str]:
+def extract_date(in_str: Any) -> Optional[datetime.datetime]:
+ """Finds and extracts a date from the string, if possible.
+
+ Args:
+ in_str: the string to extract a date from
+
+ Returns:
+ a datetime if date was found, otherwise None
+
+ >>> extract_date("filename.txt dec 13, 2022")
+ datetime.datetime(2022, 12, 13, 0, 0)
+
+ >>> extract_date("Dear Santa, please get me a pony.")
+
+ """
import itertools
import dateparse.dateparse_utils as du
import itertools
import dateparse.dateparse_utils as du
@@
-1405,7
+1419,7
@@
def extract_date(in_str: Any) -> Optional[str]:
expr = " ".join(ngram)
logger.debug(f"Trying {expr}")
if d.parse(expr):
expr = " ".join(ngram)
logger.debug(f"Trying {expr}")
if d.parse(expr):
- return d.get_date()
+ return d.get_date
time
()
except du.ParseException: # type: ignore
pass
return None
except du.ParseException: # type: ignore
pass
return None