From 0afe798b83089e274b244d8eb171e18d6ebbf8fa Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 28 Apr 2019 12:11:48 +0100 Subject: [PATCH] Support parsing iso8601 with microseconds --- src/utils/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index b3af3f61..d7a3d968 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -6,6 +6,7 @@ class Direction(enum.Enum): Recv = 1 ISO8601_PARSE = "%Y-%m-%dT%H:%M:%S%z" +ISO8601_PARSE_MICROSECONDS = "%Y-%m-%dT%H:%M:%S.%f%z" DATETIME_HUMAN = "%Y/%m/%d %H:%M:%S" def iso8601_format(dt: datetime.datetime, milliseconds: bool=False) -> str: @@ -17,8 +18,9 @@ def iso8601_format(dt: datetime.datetime, milliseconds: bool=False) -> str: return "%sZ" % formatted def iso8601_format_now() -> str: return iso8601_format(datetime.datetime.utcnow()) -def iso8601_parse(s: str) -> datetime.datetime: - return datetime.datetime.strptime(s, ISO8601_PARSE) +def iso8601_parse(s: str, microseconds: bool=False) -> datetime.datetime: + fmt = ISO8601_PARSE_MICROSECONDS if microseconds else ISO8601_PARSE + return datetime.datetime.strptime(s, fmt) def datetime_human(dt: datetime.datetime): return datetime.datetime.strftime(dt, DATETIME_HUMAN)