From d3ed08e336ce2f32567f29458ca48d4b44c82dec Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 30 Aug 2019 13:25:19 +0100 Subject: [PATCH] Don't use 'Z' for "utc timezone" - actually parse timezone out of datetimes --- src/utils/__init__.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index eee3fc50..d19c74dc 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -7,17 +7,24 @@ class Direction(enum.Enum): ISO8601_PARSE = "%Y-%m-%dT%H:%M:%S%z" ISO8601_PARSE_MICROSECONDS = "%Y-%m-%dT%H:%M:%S.%f%z" + +ISO8601_FORMAT_DT = "%Y-%m-%dT%H:%M:%S" +ISO8601_FORMAT_TZ = "%z" + + DATETIME_HUMAN = "%Y/%m/%d %H:%M:%S" def iso8601_format(dt: datetime.datetime, milliseconds: bool=False) -> str: - timespec = "seconds" - if milliseconds: - timespec = "milliseconds" + dt_format = dt.strftime(ISO8601_FORMAT_DT) + tz_format = dt.strftime(ISO8601_FORMAT_TZ) - formatted = dt.isoformat(timespec=timespec) - return "%sZ" % formatted -def iso8601_format_now() -> str: - return iso8601_format(datetime.datetime.utcnow()) + ms_format = "" + if milliseconds: + ms_format = ".%d" % (dt.microsecond/1000) + return "%s%s%s" % (dt_format, ms_format, tz_format) +def iso8601_format_now(milliseconds: bool=False) -> str: + now = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc) + return iso8601_format(now, milliseconds=milliseconds) 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)