Skip to content

Commit

Permalink
Validate leap year dates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyhajj committed Nov 10, 2023
1 parent 85154b7 commit 19bc2dd
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions personnummer/personnummer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, ssn, options=None):
options = {}

self.options = options
self._ssn = ssn
self.parts = self.get_parts(ssn)

if self.valid() is False:
Expand Down Expand Up @@ -80,7 +81,11 @@ def is_male(self):
return int(gender_digit) % 2 != 0

def is_coordination_number(self):
return test_date(int(self.parts['year']), int(self.parts['month']), int(self.parts['day']) - 60)
return test_date(
int(self.parts['century'] + self.parts['year']),
int(self.parts['month']),
int(self.parts['day']) - 60,
)

@staticmethod
def get_parts(ssn):
Expand Down Expand Up @@ -133,6 +138,7 @@ def valid(self):
:return:
"""

century = self.parts['century']
year = self.parts['year']
month = self.parts['month']
day = self.parts['day']
Expand All @@ -144,10 +150,10 @@ def valid(self):

is_valid = luhn(year + month + day + num) == int(check)

if is_valid and test_date(int(year), int(month), int(day)):
if is_valid and test_date(int(century + year), int(month), int(day)):
return True

return is_valid and test_date(int(year), int(month), int(day) - 60)
return is_valid and test_date(int(century + year), int(month), int(day) - 60)


def luhn(data):
Expand Down Expand Up @@ -210,14 +216,8 @@ def test_date(year, month, day):
"""
Test if the input parameters are a valid date or not
"""
for x in ['19', '20']:
new_y = x.__str__() + year.__str__()
new_y = int(new_y)
try:
date = datetime.date(new_y, month, day)
if date.year == new_y and date.month == month and date.day == day:
return True
except ValueError:
continue

return False
try:
date = datetime.date(year, month, day)
return date.year == year and date.month == month and date.day == day
except ValueError:
return False

0 comments on commit 19bc2dd

Please sign in to comment.