Parsing date from file path
Issue Description
Hello! I'm trying to parse date from file path and facing an issue:
arrow.get("/absolute/path/2020-12-16/16-55-22/file.txt", "YYYY-MM-DD/hh-mm-ss")
Gives
Traceback (most recent call last):
File ".../scratches/scratch_64.py", line 10, in <module>
print(arrow.get("/absolute/path/2020-12-16/16-55-22/file.txt", "YYYY-MM-DD/hh-mm-ss"))
File ".../.pyenv/versions/3.8.5/lib/python3.8/site-packages/arrow/api.py", line 91, in get
return _factory.get(*args, **kwargs)
File ".../versions/3.8.5/lib/python3.8/site-packages/arrow/factory.py", line 292, in get
dt = parser.DateTimeParser(locale).parse(
File ".../.pyenv/versions/3.8.5/lib/python3.8/site-packages/arrow/parser.py", line 326, in parse
raise ParserMatchError(
arrow.parser.ParserMatchError: Failed to match 'YYYY-MM-DD/hh-mm-ss' when parsing '/absolute/path/2020-12-16/16-55-22/file.txt'.
This workaround works fine:
arrow.get("/absolute/path/2020-12-16/16-55-22/file.txt".replace("/", " "), "YYYY-MM-DD hh-mm-ss")
But if path gets little more complicated it fails again:
arrow.get("/absolute/path/prefix-2020-12-16/16-55-22/file.txt".replace("/", " "), "YYYY-MM-DD hh-mm-ss")
System Info
- 🖥 OS name and version: macOS 10.15.7
- 🐍 Python version: Python 3.8.5
- 🏹 Arrow version: 1.1.1
Interesting use case. Arrow's parser wasn't designed to handle file paths, rather it has an affinity towards natural language with whitespaces as separator at the moment.
Example:
>>> arrow.get("Let's meet on 2021-10-10", "YYYY-MM-DD")
<Arrow [2021-10-10T00:00:00+00:00]>
This is something we can improve, indeed. Something similar is discussed in #976.
Interesting use case. Arrow's parser wasn't designed to handle file paths, rather it has an affinity towards natural language with whitespaces as separator at the moment.
Example:
>>> arrow.get("Let's meet on 2021-10-10", "YYYY-MM-DD") <Arrow [2021-10-10T00:00:00+00:00]>This is something we can improve, indeed. Something similar is discussed in #976.
It isn't so much that it's a filepath, Arrow can and should be able to parse strings that the use clearly describes the rules for.