def format_sec(sec):
    """
    format a time

    Parameters
    ----------
    sec : float
        time in seconds

    Returns
    -------
        string :
            formatted time in days, hours, minutes and seconds
    """
    m, s = divmod(sec, 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)
    if d:
        return '%d d %02d h %02d m %02d s' % (d, h, m, s)
    if h:
        return '%02d h %02d m %02d s' % (h, m, s)
    if m:
        return '%02d m %02d s' % (m, s)
    return '%.3f s' % s