@@ -474,21 +474,35 @@ def decode(code: str) -> dict[str, Any]:
474474 birthdate_year = int (f"{ current_year_century_prefix } { birthdate_year_suffix } " )
475475 if birthdate_year > current_year :
476476 birthdate_year -= 100
477- birthdate_str = f"{ birthdate_year } /{ birthdate_month } /{ birthdate_day } "
478- birthdate = _get_date (birthdate_str , separator = "/" )
479- if not birthdate :
480- raise ValueError (f"[codicefiscale] invalid date: { birthdate_str } " )
481477
482- birthplace_code = raw ["birthplace" ][0 ] + raw ["birthplace" ][1 :].translate (
483- _OMOCODIA_DECODE_TRANS
484- )
485- birthplace = _get_birthplace (birthplace_code , birthdate )
486- # print(birthplace)
487- if not birthplace :
488- raise ValueError (
489- "[codicefiscale] wrong birthplace code: "
490- f"{ birthplace_code !r} / birthdate: { birthdate .isoformat ()!r} ."
491- )
478+ birthdate_or_birthplace_error = None
479+ for _ in range (2 ):
480+ birthdate_str = f"{ birthdate_year } /{ birthdate_month } /{ birthdate_day } "
481+ try :
482+ birthdate = _get_date (birthdate_str , separator = "/" )
483+ if not birthdate :
484+ raise ValueError (f"[codicefiscale] invalid date: { birthdate_str } " )
485+
486+ birthplace_code = raw ["birthplace" ][0 ] + raw ["birthplace" ][1 :].translate (
487+ _OMOCODIA_DECODE_TRANS
488+ )
489+ birthplace = _get_birthplace (birthplace_code , birthdate )
490+ # print(birthplace)
491+ if not birthplace :
492+ raise ValueError (
493+ "[codicefiscale] wrong birthplace code: "
494+ f"{ birthplace_code !r} / birthdate: { birthdate .isoformat ()!r} ."
495+ )
496+ break
497+ except ValueError as error :
498+ # attempt to handle people over 100 years old
499+ if birthdate_or_birthplace_error is None :
500+ birthdate_or_birthplace_error = error
501+ birthdate_year -= 100
502+ else :
503+ # raise the first raised error
504+ if birthdate_or_birthplace_error :
505+ raise birthdate_or_birthplace_error
492506
493507 cin = raw ["cin" ]
494508 cin_check = encode_cin (code )
0 commit comments