Handle UTC+00:00 in datetime strings

In some cases, the following:

  datetime.datetime.now(tz=iso8601.iso8601.UTC).tzinfo.tzname()

returns:

  'UTC+00:00'

rather than:

  'UTC'

resulting in strings that look like:

  2013-03-04T12:00:01.000000UTC+00:00

That is just flatly invalid. The code here accounts for a tzname of
"UTC" and normalizes to to being a trailing Z as-per the ISO 8601 spec,
but it does not account for UTC+00:00. Add support for that so that we
don't produce invalid date strings.

Most of this can be avoided by replacing use of this function with the
isoformat method of datetime instead.

  datetime.datetime.now(tz=iso8601.iso8601.UTC).isoformat()

Produces

  2013-03-04T12:00:01.000000+00:00

Which while different from

  2013-03-04T12:00:01.000000Z

is still a valid iso8601 string.

Change-Id: I52ca7561abee158285c2c98ba63d84c62e12360f
This commit is contained in:
Monty Taylor
2017-12-02 11:11:23 -06:00
parent 789301a388
commit f0c9b20e0f

View File

@@ -114,7 +114,7 @@ def isotime(at=None, subsecond=False):
if not subsecond
else _ISO8601_TIME_FORMAT_SUBSECOND)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
st += ('Z' if tz == 'UTC' else tz)
st += ('Z' if (tz == 'UTC' or tz == 'UTC+00:00') else tz)
return st