Search code examples
pythondatetimetimeassertrfc3339

What is the difference between the two time formats?


My goal is to use a single string time format (UTC).

I start with two string timestamps:

tf1 = '2020-07-11T17:25:48.675541+0000'
tf2 = '2020-07-11T17:25:49.125175Z'

RFC3339_FORMAT = '%Y-%m-%dT%H:%M:%S.%f%z'

When I assert if they both are in rfc3339 format, they both pass:

from datetiem import datetime

assert datetime.strptime(tf1, RFC3339_FORMAT)
assert datetime.strptime(tf2, RFC3339_FORMAT)

Why is that?

What is the difference between tf1 and tf2?

More generally, what is wrong with my assertion here?


Solution

  • The %z format specifier in strptime() accepts timezones in ±HHMM[SS[.ffffff]] format, ±HH:MM[:SS[.ffffff]] format, or Z, which is equivalent to +00:00. See note (6) in the documentation.

    Note that this is not the same as RFC 3339, which only allows ±HH:MM or Z. Timezones without colons are ISO 8601 format.