import math


def format_time(time, n_time_steps):
    """Format time index based on data availability.

    Formats a time index according to a maximum number of time steps in
    in the data. This is useful in order to ensure consistent filenames
    of the *same* length.

    Parameters
    ----------
    time:
        Current time step that should be formatted

    n_time_steps:
        Maximum number of time steps

    Returns
    -------
    Formatted string with added zeroes for padding. For example, if
    there are 23 time steps in total, time step 5 will be formatted
    as  '05', thereby ensuring that a lexicographical ordering will
    be sufficient to sort the resulting files.
    """
    n_digits = int(math.log10(n_time_steps) + 1)
    return f'{time:0{n_digits}d}'