Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate leap year dates correctly #57

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading