formats duration in a human readable format.
    accepts either minutes or timedelta

def format_duration(minutes, human = True):
    """formats duration in a human readable format.
    accepts either minutes or timedelta"""

    if isinstance(minutes, dt.timedelta):
        minutes = duration_minutes(minutes)

    if not minutes:
        if human:
            return ""
        else:
            return "00:00"

    if minutes < 0:
        # format_duration did not work for negative values anyway
        # return a warning
        return "NEGATIVE"

    hours = minutes / 60
    minutes = minutes % 60
    formatted_duration = ""

    if human:
        if minutes % 60 == 0:
            # duration in round hours
            formatted_duration += ("%dh") % (hours)
        elif hours == 0:
            # duration less than hour
            formatted_duration += ("%dmin") % (minutes % 60.0)
        else:
            # x hours, y minutes
            formatted_duration += ("%dh %dmin") % (hours, minutes % 60)
    else:
        formatted_duration += "%02d:%02d" % (hours, minutes)


    return formatted_duration