import re


def set_time(bytearray_: bytearray, byte_index: int, time_string: str) -> bytearray:
    """Set value in bytearray to time

        Notes:
            Datatype `time` consists in 4 bytes in the PLC.
            Maximum possible value is T#24D_20H_31M_23S_647MS(2147483647).
            Lower posible value is T#-24D_20H_31M_23S_648MS(-2147483648).

        Args:
            bytearray_: buffer to write.
            byte_index: byte index from where to start writing.
            time_string: time value in string

        Examples:
            >>> data = bytearray(4)

            >>> snap7.util.set_time(data, 0, '-22:3:57:28.192')

            >>> data
                bytearray(b'\x8d\xda\xaf\x00')
        """
    sign = 1
    if re.fullmatch(r"(-?(2[0-3]|1?\d):(2[0-3]|1?\d|\d):([1-5]?\d):[1-5]?\d.\d{1,3})|"
                    r"(-24:(20|1?\d):(3[0-1]|[0-2]?\d):(2[0-3]|1?\d).(64[0-8]|6[0-3]\d|[0-5]\d{1,2}))|"
                    r"(24:(20|1?\d):(3[0-1]|[0-2]?\d):(2[0-3]|1?\d).(64[0-7]|6[0-3]\d|[0-5]\d{1,2}))", time_string):
        data_list = re.split('[: .]', time_string)
        days: str = data_list[0]
        hours: int = int(data_list[1])
        minutes: int = int(data_list[2])
        seconds: int = int(data_list[3])
        milli_seconds: int = int(data_list[4].ljust(3, '0'))
        if re.match(r'^-\d{1,2}$', days):
            sign = -1

        time_int = ((int(days) * sign * 3600 * 24 + (hours % 24) * 3600 + (minutes % 60) * 60 + seconds % 60) * 1000 + milli_seconds) * sign
        bytes_array = time_int.to_bytes(4, byteorder='big', signed=True)
        bytearray_[byte_index:byte_index + 4] = bytes_array
        return bytearray_
    else:
        raise ValueError('time value out of range, please check the value interval')