arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Parsing date from file path

Open almaslov opened this issue 4 years ago • 2 comments

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

almaslov avatar Sep 03 '21 08:09 almaslov

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.

krisfremen avatar Sep 04 '21 16:09 krisfremen

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.

rtphokie avatar Nov 07 '21 19:11 rtphokie