def encode_datetime(dt):
    """Converts provided datetime object into ISO 8601/RFC3339-compliant string

    :param datetime.datetime dt: datetime object
    :return: string representation of dt
    :rtype: str
    """
    s = dt.isoformat()
    if s.endswith('+00:00'):
        s = s[:-6] + 'Z'
    if dt.tzinfo is None:
        # Treat datetimes with no tzinfo as UTC
        s += 'Z'
    return s