import time
import calendar


def format_time(time_str, format):
    """
    This will take a time from Jira and format it into the specified string.  The time will
    also be converted from UTC to local time.

    Jira time format is assumed to be, "YYYY-MM-DDTHH:MM:SS.ssss+0000". Where "T" is the
    literal character guessing it means "time". "ssss" are interpreted as fractions of
    seconds "+0000" is the timezone offset from UTC.

    Args:
        time_str (string): Time returned from :class:`jira.issue`

        format (string): String to specify how to format the resultant time.  You'll want to
                       look at :func:`time.strftime` for the format options.

    Returns: A string representing the local version of `time` formated according to
             `format`.

    """

    # Strip off the trailing timezone
    # HACK FOR NOW ALWAYS ASSUME "+0000"
    just_time, zone = time_str.split('+')

    # Just drop the fractional seconds
    just_time, _ = just_time.split('.')

    parsed_time = time.strptime(just_time, "%Y-%m-%dT%H:%M:%S")

    # Convert from UTC to seconds and from seconds to local
    local_time = time.gmtime(calendar.timegm(parsed_time))

    return time.strftime(format, local_time)