Skip to content

Commit

Permalink
Merge pull request #62 from deardurham/61-midnight
Browse files Browse the repository at this point in the history
Fix parsing midnight date time
  • Loading branch information
copelco authored Jul 7, 2022
2 parents 33daf70 + e72ab45 commit cbf6903
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions ciprs_reader/parser/section/case_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ def clean(self, matches):

class OffenseDateTime(CaseInformationParser):

pattern = [r"\s*Offense", r"Date/Time:\s*(?P<value>[\w/ :]+[AaPp][Mm])"]
pattern = [r"\s*Offense", r"Date\/Time:\s*(?P<value>[\w/ :]+([AaPp][Mm])?)"]
section = ("Case Information", "Offense Date")

def clean(self, matches):
"""Parse and convert to the date and time in ISO 8601 format"""
date = dt.datetime.strptime(matches["value"], "%m/%d/%Y %I:%M %p")
try:
date = dt.datetime.strptime(matches["value"], "%m/%d/%Y %I:%M %p")
except ValueError:
# fallback to parsing time like midnight, e.g. 05/17/2015 00:00
date = dt.datetime.strptime(matches["value"], "%m/%d/%Y %H:%M")
matches["value"] = date.isoformat()
return matches

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest==6.2.2
pytest==7.1.2
pytest-cov==2.11.1
pytest-pylint==0.18.0
pre-commit==2.11.1
7 changes: 7 additions & 0 deletions tests/parsers/test_case_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def test_offense_date_time(report, state):
assert matches["value"] == "2015-05-17T21:59:00"


def test_offense_date_time_midnight(report, state):
string = " Offense Date/Time: 05/17/2015 00:00 "
matches = case_information.OffenseDateTime(report, state).match(string)
assert matches is not None, "Regex match failed"
assert matches["value"] == "2015-05-17T00:00:00"


@pytest.mark.parametrize(
"expected,val",
(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_records/expected_output/test_redacted_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@
},
"Case Information": {
"Case Status": "DISPOSED",
"Offense Date": "2005-03-04T00:00:00",
"Arrest Date": "2005-12-03"
},
"Defendant": {
Expand Down Expand Up @@ -1463,4 +1464,4 @@
"Superior Court Offense Information": [],
"_meta": {}
}
]
]

0 comments on commit cbf6903

Please sign in to comment.