def format(dt, usec=True, offset=True):
    """
    Format a provided datetime object and return ISO8601 string.

    :type dt: ``datetime.datetime``
    """
    # pylint: disable=no-member
    if isinstance(dt, six.string_types):
        dt = parse(dt)
    elif isinstance(dt, int):
        # unix epoch
        dt = datetime.datetime.fromtimestamp(dt, tzi.tzutc())

    fmt = ISO8601_FORMAT_MICROSECOND if usec else ISO8601_FORMAT
    if offset:
        ost = dt.strftime("%z")
        ost = (ost[:3] + ":" + ost[3:]) if ost else "+00:00"
    else:
        tz = dt.tzinfo.tzname(dt) if dt.tzinfo else "UTC"
        ost = "Z" if tz == "UTC" else tz
    return dt.strftime(fmt) + ost

    Format a provided datetime object and return ISO8601 string.

    :type dt: ``datetime.datetime``
    
st2common/st2common/util/isotime.py