Skip to content

Commit

Permalink
Merge pull request #74 from alan-turing-institute/additional-iso-date…
Browse files Browse the repository at this point in the history
…-formats

add extra supported iso date format
  • Loading branch information
edwardchalstrey1 authored Jun 6, 2019
2 parents 7349392 + a93c645 commit ddef2fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion readabilipy/extractors/extract_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def ensure_iso_date_format(date_string, ignoretz=True):
supported_date_formats = [
"%Y-%m-%dT%H:%M:%S", # '2014-10-24T17:32:46'
"%Y-%m-%dT%H:%M:%S%z", # '2014-10-24T17:32:46+12:00'
"%Y-%m-%dT%H:%M%z", # '2014-10-24T17:32+12:00'
"%Y-%m-%dT%H:%M:%SZ", # '2014-10-24T17:32:46Z'
"%Y-%m-%dT%H:%M:%S.%fZ", # '2014-10-24T17:32:46.000Z'
"%Y-%m-%dT%H:%M:%S.%f" # '2014-10-24T17:32:46.493'
Expand All @@ -41,7 +42,7 @@ def ensure_iso_date_format(date_string, ignoretz=True):
# For python < 3.7, strptime() is not able to parse timezones containing
# colons (eg. 2014-10-24T17:32:46+12:00). By stripping the colon here,
# we ensure that all versions of python can parse datetimes like these
if date_format == "%Y-%m-%dT%H:%M:%S%z" and date_string[-3] == ':':
if date_format in ("%Y-%m-%dT%H:%M:%S%z", "%Y-%m-%dT%H:%M%z") and date_string[-3] == ':':
isodate = datetime.strptime(date_string[:-3] + date_string[-2:], date_format)
else:
isodate = datetime.strptime(date_string, date_format)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_date_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_ensure_iso_date_format_timezone_drop():
assert ensure_iso_date_format(datetime_string) == expected_iso_string


def test_ensure_iso_date_format_no_seconds():
datetime_string = '2014-10-24T17:32+12:00'
expected_iso_string = '2014-10-24T17:32:00'
assert ensure_iso_date_format(datetime_string) == expected_iso_string


def test_ensure_iso_date_format_no_tz():
datetime_string = '2014-10-24T17:32:46'
expected_iso_string = '2014-10-24T17:32:46'
Expand Down

0 comments on commit ddef2fa

Please sign in to comment.