from datetime import datetime


def parse_timestamp(time_str):    
    """
    There are three datetime string formats in eMammal, and some have an empty field.
    Args:
        time_str: text in the tag ImageDateTime

    Returns:
        datetime object, error (None if no error)
    """

    if time_str == '' or time_str is None:
        return '', 'empty or None'
    try:
        res = datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S')
        return res, None
    except Exception:
        try:
            res = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
            return res, None
        except Exception:
            try:
                res = datetime.strptime(time_str, '%m/%d/%Y %H:%M')
                return res, None
            except:
                print('WARNING, time_str cannot be parsed {}.'.format(time_str))
                return time_str, 'cannot be parsed {}'.format(time_str)