Skip to content

Commit

Permalink
allow misformatted dates in vendor reports in one more place
Browse files Browse the repository at this point in the history
  • Loading branch information
Wooble committed Jan 24, 2020
1 parent f41843d commit 526e7f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
10 changes: 2 additions & 8 deletions pycounter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import re

import pendulum
import six


Expand Down Expand Up @@ -49,14 +50,7 @@ def convert_date_run(datestring):
if isinstance(datestring, datetime.date):
return datestring

try:
return datetime.datetime.strptime(datestring, "%Y-%m-%d").date()
except ValueError:
try:
return datetime.datetime.strptime(datestring, "%m/%d/%Y").date()
except ValueError:
# ISO 8601 without timezone
return datetime.datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S").date()
return pendulum.parse(datestring, strict=False).date()


def convert_date_column(datestring):
Expand Down
17 changes: 16 additions & 1 deletion pycounter/test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

import pytest

from pycounter.helpers import convert_covered, is_first_last, next_month, prev_month
from pycounter.helpers import (
convert_covered,
convert_date_run,
is_first_last,
next_month,
prev_month,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -58,3 +64,12 @@ def test_is_first_last(period, expected):
def test_convert_covered(covered_line, expected):
expected_dates = tuple(datetime.date(*val) for val in expected)
assert convert_covered(covered_line) == expected_dates


@pytest.mark.parametrize(
"date_run, expected",
[("2017-01-01", (2017, 1, 1)), ("2020-01-24T14:04:36Z", (2020, 1, 24))],
)
def test_convert_date_run(date_run, expected):
expected_date = datetime.date(*expected)
assert convert_date_run(date_run) == expected_date
2 changes: 1 addition & 1 deletion pycounter/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""version information."""
__version__ = "2.1.1"
__version__ = "2.1.2"

0 comments on commit 526e7f0

Please sign in to comment.