def average_duration(total_duration, visits):
    """ Method to calculate and format an average duration safely """
    if not visits:
        seconds = 0
    else:
        seconds = int(round(total_duration / Decimal(visits)))
    duration = timedelta(seconds=seconds)
    return str(duration)
Method to calculate and format an average duration safely