e standard formatting chars: %Nf Limit the fractional seconds to N digits. The default is N=6. %Ez Format +/-HHMM offsets as ISO RFC 3339 Z for +0000 otherwise +/-HH:MM. %Oz Format +/-HHMM offsets as ISO RFC 3339 +/-HH:MM. NOTE: The standard Python 2 strftime() borks non-ascii time parts. It does so by encoding non-ascii names to bytes, presumably under the assumption that the return value will be immediately output. This code works around that by decoding strftime() values to unicode if necessary and then returning either an ASCII or UNICODE string. Args: dt: The datetime object to be formatted. fmt: The strftime(3) format string, None for the RFC 3339 format in the dt timezone ('%Y-%m-%dT%H:%M:%S.%3f%Ez'). tzinfo: Format dt relative to this timezone. Raises: DateTimeValueError: A DateTime numeric constant exceeded its range. Returns: A string of a datetime object formatted by an extended strftime(). z