Skip to content

Commit

Permalink
The tests for this feature but not the feature itself
Browse files Browse the repository at this point in the history
seem to have been included in the merge into 3.x??
  • Loading branch information
Wooble committed Mar 20, 2020
1 parent c457eff commit 11598a9
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pycounter/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Helper functions used by pycounter."""
import calendar
import datetime
import re

import pendulum


def convert_covered(datestring):
Expand All @@ -15,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 All @@ -39,14 +49,18 @@ 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):
"""
Convert human-readable month to date of first day of month.
:param datestring: the string to convert to a date. Format like "Jan-2014".
:return: datetime.date
"""
return datetime.datetime.strptime(datestring.strip(), "%b-%Y").date()


def is_first_last(period):
Expand Down

0 comments on commit 11598a9

Please sign in to comment.