diff --git a/arrow/parser.py b/arrow/parser.py index ee31a5bc..9ccf88df 100644 --- a/arrow/parser.py +++ b/arrow/parser.py @@ -107,7 +107,7 @@ class DateTimeParser: ) _ESCAPE_RE: ClassVar[Pattern[str]] = re.compile(r"\[[^\[\]]*\]") - _ONE_OR_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{1,2}") + _ONE_OR_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[1-9]\d|\d") _ONE_OR_TWO_OR_THREE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{1,3}") _ONE_OR_MORE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d+") _TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{2}") diff --git a/tests/test_parser.py b/tests/test_parser.py index bdcc1026..ee121af3 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -147,7 +147,7 @@ def test_YY_and_YYYY_format_list(self): assert self.parser.parse( "15/01/2019T04:05:06.789120Z", - ["D/M/YYThh:mm:ss.SZ", "D/M/YYYYThh:mm:ss.SZ"], + ["D/MM/YYThh:mm:ss.SZ", "D/MM/YYYYThh:mm:ss.SZ"], ) == datetime(2019, 1, 15, 4, 5, 6, 789120, tzinfo=tz.tzutc()) # regression test for issue #447 @@ -790,6 +790,23 @@ def test_parse_normalize_whitespace(self): with pytest.raises(ParserError): self.parser.parse(" \n Jun 1\t 2005\n ", "MMM D YYYY") + def test_parse_leading_zero(self): + # Regression tests for issue #1084 + with pytest.raises(ParserError): + self.parser.parse("01", "M") + + with pytest.raises(ParserError): + self.parser.parse("01", "D") + + with pytest.raises(ParserError): + self.parser.parse("01", "H") + + with pytest.raises(ParserError): + self.parser.parse("01", "h") + + with pytest.raises(ParserError): + self.parser.parse("01", "s") + @pytest.mark.usefixtures("dt_parser_regex") class TestDateTimeParserRegex: