From 6f5134d10663547ee9b5759bdc10b20511bc569d Mon Sep 17 00:00:00 2001 From: Tasos Stamadianos Date: Fri, 19 Oct 2018 22:42:26 -0400 Subject: [PATCH 1/2] Support common parsing of month-only dates. Also added tests. Fixes #286 --- pendulum/parsing/__init__.py | 9 +++++---- tests/parsing/test_parsing.py | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pendulum/parsing/__init__.py b/pendulum/parsing/__init__.py index 58f60a99..f5835a34 100644 --- a/pendulum/parsing/__init__.py +++ b/pendulum/parsing/__init__.py @@ -16,11 +16,11 @@ # Date (optional) "^" "(?P" - " (?P" # Classic date (YYYY-MM-DD) + " (?P" # Classic date (YYYY:MM:DD) or (YYYY-MM-DD) or (YYYY/MM/DD) " (?P\d{4})" # Year " (?P" - " (?P[/:])?(?P\d{2})" # Month (optional) - " ((?P[/:])?(?P\d{2}))" # Day (optional) + " (?P[/:-])?(?P\d{1,2})" # Month (optional) + " ((?P[/:-])?(?P\d{1,2}))?" # Day (optional) " )?" " )" ")?" @@ -136,6 +136,7 @@ def _parse_common(text, **options): :rtype: dict or None """ m = COMMON.match(text) + print(m) has_date = False year = 0 month = 1 @@ -160,7 +161,7 @@ def _parse_common(text, **options): day = int(m.group("month")) else: month = int(m.group("month")) - day = int(m.group("day")) + day = int(m.group("day") or 1) if not m.group("time"): return date(year, month, day) diff --git a/tests/parsing/test_parsing.py b/tests/parsing/test_parsing.py index 19f532d4..83f04fc8 100644 --- a/tests/parsing/test_parsing.py +++ b/tests/parsing/test_parsing.py @@ -680,3 +680,7 @@ def test_exif_edge_case(): assert 15 == parsed.hour assert 45 == parsed.minute assert 28 == parsed.second + +def test_month_padding(): + # This shouldn't raise an exceptionf + pendulum.parse('2016-7') From 1195beb10cb9df669d286f91c38533b9c9178eaf Mon Sep 17 00:00:00 2001 From: Tasos Stamadianos Date: Mon, 29 Oct 2018 23:26:42 -0400 Subject: [PATCH 2/2] Remove unnecessary print --- pendulum/parsing/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pendulum/parsing/__init__.py b/pendulum/parsing/__init__.py index f5835a34..44455cb5 100644 --- a/pendulum/parsing/__init__.py +++ b/pendulum/parsing/__init__.py @@ -136,7 +136,6 @@ def _parse_common(text, **options): :rtype: dict or None """ m = COMMON.match(text) - print(m) has_date = False year = 0 month = 1