pendulum
pendulum copied to clipboard
Unexpected escaped characters behaviour
-
[x] I am on the latest Pendulum version 2.1.2.
-
[x] I have searched the issues of this repo and believe that this is not a duplicate.
-
OS version and name: Win, Python 3.9.7 / WSL Python 3.9.7
-
Pendulum version: 2.1.2
Issue
According to the docs, we can escape characters in a format string (so that they don't converted to tokens).
However, when parsing e.g. the following filename, this does not work as expected and the z in zip is still converted to a timezone token.
This is expected:
>>> pendulum.from_format("19991231.zip", "YYYYMMDD.zip")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pendulum/__init__.py", line 259, in from_format
parts = _formatter.parse(string, fmt, now(), locale=locale)
File "pendulum/formatting/formatter.py", line 415, in parse
re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)
File "/home/kulhma/.pyenv/versions/3.9.7/lib/python3.9/re.py", line 210, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "pendulum/formatting/formatter.py", line 415, in <lambda>
re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)
File "pendulum/formatting/formatter.py", line 548, in _get_parsed_values
self._get_parsed_value(token, m.group(index), parsed, now)
File "pendulum/formatting/formatter.py", line 606, in _get_parsed_value
raise ValueError("Invalid date")
ValueError: Invalid date
Ok, let's escape the extension. This is unexpected:
pendulum.from_format("19991231.zip", "YYYYMMDD.[zip]")
>>> >>> >>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pendulum/__init__.py", line 259, in from_format
parts = _formatter.parse(string, fmt, now(), locale=locale)
File "pendulum/formatting/formatter.py", line 415, in parse
re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)
File "/home/kulhma/.pyenv/versions/3.9.7/lib/python3.9/re.py", line 210, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "pendulum/formatting/formatter.py", line 415, in <lambda>
re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)
File "pendulum/formatting/formatter.py", line 548, in _get_parsed_values
self._get_parsed_value(token, m.group(index), parsed, now)
File "pendulum/formatting/formatter.py", line 606, in _get_parsed_value
raise ValueError("Invalid date")
ValueError: Invalid date
Only way I can get it to work is to escape only the z character:
>>> pendulum.from_format("19991231.zip", "YYYYMMDD.[z]ip")
DateTime(1999, 12, 31, 0, 0, 0, tzinfo=Timezone('UTC'))