import re


def change_datetime_format(timestamp):
    """
    Change format of timestamp to be in valid format for neo4j datetime function.
    :param timestamp: datetime which will be changed
    :return: Valid datetime string
    """
    pattern = r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} *$'
    if re.match(pattern, timestamp) is None:
        raise AttributeError('Invalid input for change_datetime_format() function')
    valid = timestamp.replace(" ", "T", 1)
    valid += "Z"
    valid = valid.replace(" ", "")
    pattern2 = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$'
    if re.match(pattern2, valid) is None:
        raise AttributeError('Invalid output of change_datetime_format() function')
    return valid