import datetime import re def sane_year(yearAsString, lookahead=20): """Sanity-check years entered as less than four digits. If users want to enter years between 0 A.D. and 99 A.D., they need to enter it as a four-digit year (0000-0099). Dates entered as less than four digits take the current century, unless that places them past the current year by (lookahead) years or more, in which case they take the previous century.""" yearAsInt = int(yearAsString) if yearAsInt < 100 and len(yearAsString) < 4: currentYear = datetime.date.today().year currentCentury, rem = divmod(currentYear, 100) yearAsInt += (currentCentury * 100) if yearAsInt >= (currentYear + lookahead): yearAsInt -= 100 return yearAsInt def parse_date(dateAsString): atoms = re.split(r"\D", dateAsString) if len(atoms) != 3: raise ValueError("Date values must be in the form: mm dd yy.") atoms = sane_year(atoms[2]), int(atoms[0]), int(atoms[1]) return atoms