Support parsing iso8601 with microseconds

This commit is contained in:
jesopo 2019-04-28 12:11:48 +01:00
parent cbed30ed62
commit 0afe798b83

View file

@ -6,6 +6,7 @@ class Direction(enum.Enum):
Recv = 1 Recv = 1
ISO8601_PARSE = "%Y-%m-%dT%H:%M:%S%z" 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" DATETIME_HUMAN = "%Y/%m/%d %H:%M:%S"
def iso8601_format(dt: datetime.datetime, milliseconds: bool=False) -> str: 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 return "%sZ" % formatted
def iso8601_format_now() -> str: def iso8601_format_now() -> str:
return iso8601_format(datetime.datetime.utcnow()) return iso8601_format(datetime.datetime.utcnow())
def iso8601_parse(s: str) -> datetime.datetime: def iso8601_parse(s: str, microseconds: bool=False) -> datetime.datetime:
return datetime.datetime.strptime(s, ISO8601_PARSE) fmt = ISO8601_PARSE_MICROSECONDS if microseconds else ISO8601_PARSE
return datetime.datetime.strptime(s, fmt)
def datetime_human(dt: datetime.datetime): def datetime_human(dt: datetime.datetime):
return datetime.datetime.strftime(dt, DATETIME_HUMAN) return datetime.datetime.strftime(dt, DATETIME_HUMAN)