Skip to content

Commit

Permalink
Fix latest days of the month as birth day in italian SSN (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommasoLencioni authored Oct 31, 2023
1 parent d58f00a commit facfaac
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion faker/providers/ssn/it_IT/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8018,8 +8018,10 @@ def ssn(self) -> str:
surname: str = self._get_surname_letters()
name: str = self._get_name_letters(sex)
year: str = "%02d" % self.random_int(min=0, max=99)
is_leap_year: bool = self.is_leap_year(int(year))
month: str = self.random_element(MONTHS_LIST)
day: str = "%02d" % (self.random_int(min=1, max=28) + (40 if sex == 1 else 0))
max_day: int = self._get_max_day(is_leap_year=is_leap_year, month=month)
day: str = "%02d" % (self.random_int(min=1, max=max_day) + (40 if sex == 1 else 0))
municipality: str = self.random_element(MUNICIPALITIES_LIST)
code: str = f"{surname}{name}{year}{month}{day}{municipality}"
return code + checksum(code)
Expand Down Expand Up @@ -8125,3 +8127,25 @@ def _pad_shorter(sequence: str) -> str:
Pads shorter string with the allowed char
"""
return sequence.ljust(3, "X")

@staticmethod
def is_leap_year(year: int) -> bool:
"""
Checks if the one given is a leap year
"""
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
return False

@staticmethod
def _get_max_day(is_leap_year: bool, month: str) -> int:
"""
Returns the maximum day for the current month
"""
if month in ["D", "H", "P", "S"]:
max_day = 30
elif month == "B":
max_day = 29 if is_leap_year else 28
else:
max_day = 31
return max_day

0 comments on commit facfaac

Please sign in to comment.