From 19bc2dd8d419ac843ebb81dac746ec16064acb8c Mon Sep 17 00:00:00 2001 From: Danny Hajj Date: Wed, 8 Nov 2023 17:52:08 +0100 Subject: [PATCH] Validate leap year dates correctly --- personnummer/personnummer.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/personnummer/personnummer.py b/personnummer/personnummer.py index 2224db2..308d0c8 100644 --- a/personnummer/personnummer.py +++ b/personnummer/personnummer.py @@ -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: @@ -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): @@ -133,6 +138,7 @@ def valid(self): :return: """ + century = self.parts['century'] year = self.parts['year'] month = self.parts['month'] day = self.parts['day'] @@ -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): @@ -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