Handle datetime without second fraction part

This commit is contained in:
Ngô Ngọc Đức Huy 2024-02-14 21:11:10 +07:00
parent c82e670882
commit b35facb84c
Signed by: xarvos
GPG key ID: 904AF1C7CDF695C3

View file

@ -1,12 +1,18 @@
import math
import os
import sys
from datetime import datetime, timezone
def parse_datetime(value):
"""Returns an aware datetime in local timezone"""
dttm = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f%z")
if sys.version_info.major == 3 and sys.version_info.minor >= 11:
dttm = datetime.fromisoformat(value)
else:
# Fallback manual format parsing
# This does not catches all possible formats used by server software
dttm = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f%z")
# When running tests return datetime in UTC so that tests don't depend on
# the local timezone