Skip to content

Commit

Permalink
v2.1.1: allow bad Begin and End date strings
Browse files Browse the repository at this point in the history
(TSV reports with formatting like "Begin_Date=2019-01-01; End_Date=2019-12-31"
exist in the wild; better to allow this than to hope the vendors will fix
things any time soon...)
  • Loading branch information
Wooble committed Jan 24, 2020
1 parent 40823b6 commit 85a42d9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog
## 2.1.0 (unreleased)
## 2.1.1 (2020-01-24)
* less strict date handling in Begin/End date strings to allow working with
badly-formatted reports that exist in the wild.

## 2.1.0 (2019-11-06)
* add support for COUNTER 4 title-level turnaways reports (JR2 and BR3).

## 2.0.0 (2019-09-13)
Expand Down
10 changes: 9 additions & 1 deletion pycounter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime

import six
import re


def convert_covered(datestring):
Expand All @@ -17,8 +18,15 @@ def convert_covered(datestring):
(Will also accept MM/DD/YYYY format, ISO 8601 timestamps, or existing
datetime objects; these shouldn't be in COUNTER reports, but they
do show up in real world data...)
Also accepts strings of the form 'Begin_Date=2019-01-01; End_Date=2019-12-31'
for better compatibility with some (broken) COUNTER 5 implementations.
"""
start_string, end_string = datestring.split(" to ")
try:
start_string, end_string = datestring.split(" to ")
except ValueError:
start_string, end_string = tuple(re.findall(r"\d{4}-\d{2}-\d{2}", datestring))

start_date = convert_date_run(start_string)
end_date = convert_date_run(end_string)

Expand Down
14 changes: 13 additions & 1 deletion pycounter/test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from pycounter.helpers import is_first_last, next_month, prev_month
from pycounter.helpers import convert_covered, is_first_last, next_month, prev_month


@pytest.mark.parametrize(
Expand Down Expand Up @@ -46,3 +46,15 @@ def test_prevmonth(pair):
def test_is_first_last(period, expected):
dt_period = tuple(datetime.date(*part) for part in period)
assert is_first_last(dt_period) == expected


@pytest.mark.parametrize(
"covered_line, expected",
[
("2017-01-01 to 2017-06-30", ((2017, 1, 1), (2017, 6, 30))),
("Begin_Date=2019-01-01; End_Date=2019-12-31", ((2019, 1, 1), (2019, 12, 31))),
],
)
def test_convert_covered(covered_line, expected):
expected_dates = tuple(datetime.date(*val) for val in expected)
assert convert_covered(covered_line) == expected_dates
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.0"
__version__ = "2.1.1"

0 comments on commit 85a42d9

Please sign in to comment.