import re


def generate_file_name(pattern, ts):
    """Function that generates a single file name from pattern and timestamp.

    Parameters
    ----------
    pattern : str
        File name pattern, e.g. 'file_name_[yyyyMMdd]'. Must include
        file date component wrapped in square brackets using a subset of
        Java's SimpleDateFormat date formats symbols.
        Allowed date format sequences: 'yy', 'yyyy', 'MM', 'dd', 'HH', 'mm', 'ss'.
        Default: 'test_data_[yyyyMMdd].csv'
    ts : datatime.datetime
        Timestamp for file date

    Returns
    -------
    str
        File name
    """

    date_pattern = re.findall(r".*(\[[yMdmHs\-:_ ]*\]).*", pattern)
    if len(date_pattern) < 1:
        return pattern
    date_str = date_pattern[0]
    date_str = date_str\
        .replace("[", "").replace("]", "")\
        .replace("yyyy", "{:04d}".format(ts.year))\
        .replace("yy", "{:02d}".format(ts.year % 1000))\
        .replace("MM", "{:02d}".format(ts.month))\
        .replace("dd", "{:02d}".format(ts.day))\
        .replace("HH", "{:02d}".format(ts.hour))\
        .replace("mm", "{:02d}".format(ts.minute))\
        .replace("ss", "{:02d}".format(ts.second))

    file_name = pattern.replace(date_pattern[0], date_str)
    return file_name