import re
from datetime import datetime


def matroska_date_to_datetime(date):
    """
    Converts a date in Matroska's date format to a python datetime object.
    Returns the given date string if it could not be converted.
    """
    # From the specs:
    #   The fields with dates should have the following format: YYYY-MM-DD
    #   HH:MM:SS.MSS [...] To store less accuracy, you remove items starting
    #   from the right. To store only the year, you would use, "2004". To store
    #   a specific day such as May 1st, 2003, you would use "2003-05-01".
    format = re.split(r'([-:. ])', '%Y-%m-%d %H:%M:%S.%f')
    while format:
        try:
            return datetime.strptime(date, ''.join(format))
        except ValueError:
            format = format[:-2]
    return date