def toString(self, i):
        """
        Serialize a C{datetime.datetime} instance to a string in the specified
        wire format.
        """
        offset = i.utcoffset()
        if offset is None:
            raise ValueError(
                'amp.DateTime cannot serialize naive datetime instances.  '
                'You may find amp.utc useful.')

        minutesOffset = (offset.days * 86400 + offset.seconds) // 60

        if minutesOffset > 0:
            sign = '+'
        else:
            sign = '-'

        # strftime has no way to format the microseconds, or put a ':' in the
        # timezone. Surprise!

        # Python 3.4 cannot do % interpolation on byte strings so we pack into
        # an explicitly Unicode string then encode as ASCII.
        packed = u'%04i-%02i-%02iT%02i:%02i:%02i.%06i%s%02i:%02i' % (
            i.year,
            i.month,
            i.day,
            i.hour,
            i.minute,
            i.second,
            i.microsecond,
            sign,
            abs(minutesOffset) // 60,
            abs(minutesOffset) % 60)

        return packed.encode("ascii")

        Serialize a C{datetime.datetime} instance to a string in the specified
        wire format.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/protocols/amp.py