import re


def validate_time_string(value, is_list):
    """
    Checks that a "time string" quant is correctly formatted.
    A time string can contain three kinds of expressions, separated by + or -:
        Base values, which are just numeric (with an optional exponent on e form)
        Delta values, which are the same as above with a '*i' or 'i' suffix
        Variables, which are strings that are formatted according to the variable name rules
    If the string is correctly formatted, a polished version is returned.
    Otherwise, the original string is returned with an 'INVALID: ' prefix.
    """
    # Strip the input down to the essential part
    value = value.replace('INVALID:', '').strip()
    input_str = value.replace(' ', '')

    # Split the string if the quant accepts multiple values
    if is_list:
        strings = input_str.split(',')
    else:
        strings = [input_str]

    result = ''
    for idx, s in enumerate(strings):
        accum_string = '' if idx == 0 else ', '
        first = True
        if len(s) == 0:
            return f'INVALID: {value}'
        while len(s) > 0:
            prefix = '[+-]?' if first else '[+-]'
            var_rex = re.compile(prefix + r'[A-Za-z_]+[0-9A-Za-z_]*(\*i)?')
            var_match = var_rex.match(s)
            if var_match:
                # Remove the matched part from input
                match_str = var_match.group()
                match_len = len(match_str)
                s = s[match_len:]
                first = False
                if match_str[0] in ('+', '-'):
                    match_str = f'{match_str[0]} {match_str[1:]}'
                # Add a space after variable name
                accum_string += f'{match_str} '
                continue

            # No variable match, check for numeric value
            num_rex = re.compile(prefix + r'(([0-9]*\.[0-9]+)|([0-9]+))(e-?[0-9]+)?(\*?i)?', re.I)
            num_match = num_rex.match(s)
            if num_match:
                # Remove the matched part from input
                match_str = num_match.group()
                match_len = len(match_str)
                s = s[match_len:]
                first = False

                # Temporarily remove first char if it's a + or -
                if match_str[0] in ('+', '-'):
                    # Put a space after the sign
                    prefix_char = f'{match_str[0]} '
                    match_str = match_str[1:]
                else:
                    prefix_char = ''

                # Perform some cleanup
                while match_str.startswith('0') and len(match_str) > 1 and match_str[1].isnumeric():
                    match_str = match_str[1:]
                # Insert a zero if the number starts with a period
                if match_str.startswith('.'):
                    match_str = '0' + match_str

                match_str = f'{prefix_char}{match_str} '
                match_str = match_str.replace('I', 'i')
                match_str = match_str.replace('e', 'E')
                accum_string += match_str
                continue

            # No match, invalid input
            return f'INVALID: {value}'

        result += accum_string.strip()
    return result