Skip to content

Commit

Permalink
Support more date format strings for parsing, ref #53
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Sep 14, 2023
1 parent 091c71d commit 1650ca7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions odmpy/libby.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,16 +631,23 @@ def parse_datetime(value: str) -> datetime: # type: ignore[return]
:param value:
:return:
"""
formats = ("%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S.%fZ")
for i, fmt in enumerate(formats, start=1):
formats = (
"%Y-%m-%dT%H:%M:%SZ",
"%Y-%m-%dT%H:%M:%S.%fZ",
"%Y-%m-%dT%H:%M:%S%z",
"%Y-%m-%dT%H:%M:%S.%f%z",
"%m/%d/%Y", # publishDateText
)
for fmt in formats:
try:
dt = datetime.strptime(value, fmt)
if not dt.tzinfo:
dt = dt.replace(tzinfo=timezone.utc)
return dt
except ValueError:
if i == len(formats):
raise
pass

raise ValueError(f"time data '{value}' does not match known formats {formats}")

@staticmethod
def is_renewable(loan: Dict) -> bool:
Expand Down
15 changes: 15 additions & 0 deletions tests/libby_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,18 @@ def test_get_loan_format(self):
),
LibbyFormats.MagazineOverDrive,
)

def test_parse_datetime(self):
for value in (
"2017-06-06T04:00:00Z", # estimatedReleaseDate, publishDate
"2023-08-10T23:00:01.000Z", # expireDate
"2023-07-31T08:00:01.000+00:00", # placedDate
"2023-08-01T10:00:01.000Z", # placedDate
"2023-09-14T07:20:30+00:00", # expireDate
"05/30/2023",
):
with self.subTest(value=value):
LibbyClient.parse_datetime(value)

with self.assertRaises(ValueError):
LibbyClient.parse_datetime("2023/05/30 23:01:14")

0 comments on commit 1650ca7

Please sign in to comment.