Skip to content

Commit aa121e1

Browse files
committed
Fix from_format() with escaped elements
1 parent d0eebd9 commit aa121e1

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fixed `from_format()` not recognizing input strings when the specified pattern had escaped elements.
8+
9+
310
## [2.0.3] - 2018-07-30
411

512
### Fixed

pendulum/formatting/formatter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ def _replace_tokens(self, token, locale): # type: (str, Locale) -> str
626626
if token.startswith("[") and token.endswith("]"):
627627
return token[1:-1]
628628
elif token.startswith("\\"):
629+
if len(token) == 2 and token[1] in {"[", "]"}:
630+
return ""
631+
629632
return token
630633
elif token not in self._REGEX_TOKENS and token not in self._LOCALIZABLE_TOKENS:
631634
raise ValueError("Unsupported token: {}".format(token))

tests/datetime/test_from_format.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def test_from_format_with_timezone():
3131
assert "Europe/London" == d.timezone_name
3232

3333

34+
def test_from_format_with_escaped_elements():
35+
d = pendulum.from_format("1975-05-21T22:32:11+00:00", "YYYY-MM-DD[T]HH:mm:ssZ")
36+
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
37+
assert "+00:00" == d.timezone_name
38+
39+
3440
def test_from_format_with_millis():
3541
d = pendulum.from_format("1975-05-21 22:32:11.123456", "YYYY-MM-DD HH:mm:ss.SSSSSS")
3642
assert_datetime(d, 1975, 5, 21, 22, 32, 11, 123456)

0 commit comments

Comments
 (0)