def get_age(nin): # Should sanitize first try: nin = sanitize(nin) except: return None year_part = int(nin[4:6]) day_part = nin[0:2] month_part = nin[2:4] century = None # Century indicator i = int(nin[6:7]) if i in (0, 1, 2, 3): century = "19" elif i == 4 and year_part < 37: century = "20" elif i == 4 and year_part > 36: century = "19" elif i in (5, 6, 7, 8) and year_part < 58: century = "20" elif i in (5, 6, 7, 8) and year_part > 57: century = "18" elif i == 9 and year_part < 38: century = "20" else: century = "19" # Get year from nin again, because we need it as a string year = int("{0}{1}".format(century, nin[4:6])) born = datetime.date(year, int(month_part), int(day_part)) return calculate_age(born)
def get_age(nin): nin = sanitize(nin) if len(nin) not in (10, 11, 12, 13): return None nin = humanize(nin) try: born = date(year=int(nin['year']), day=int(nin['day']), month=int(nin['month'])) except Exception: return None return calculate_age(born)
def get_age(nin): nin = sanitize(nin) if len(nin) != 11: return None nin = huamnize(nin) if int(nin['year']) < 1800: return None try: born = date(year=int(nin['year']), day=int(nin['day']), month=int(nin['month'])) except Exception: return None return calculate_age(born)