Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 46afe54

Browse files
committed
Better auto-parsing of different datetimes
1 parent 72bbf74 commit 46afe54

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

duffel_api/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ def parse_datetime(value: str) -> datetime:
3232
"""Parse a datetime string regardless of having milliseconds or not"""
3333
# There are inconsistent formats used for the field, therefore we try to accomodate
3434
# instead of making an API breaking change.
35-
if len(value) == 20:
36-
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
35+
#
36+
# I'm really not happy with this but it helps so I'm leaving it for now.
37+
if value.endswith("Z"):
38+
if len(value) == 20:
39+
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
40+
else:
41+
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
42+
if len(value) == 19:
43+
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
3744
else:
38-
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
45+
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")

0 commit comments

Comments
 (0)