Skip to content

Commit

Permalink
Fix handling leap year in en_US Passport provider (#1872)
Browse files Browse the repository at this point in the history
* Refactor common code in passport_dates() in en_US Passport provider

* Fix handling 29 Feb in en_US Passport provider

Fix handling the issue date of 29 Feb (of a leap year).  Since
the passports expire in 5 or 10 years, the expiration year will not be
a leap year, and therefore the date needs to be adjusted to 28 Feb.

Fixes #1870
  • Loading branch information
mgorny authored Jun 2, 2023
1 parent cbea493 commit ee3ed38
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions faker/providers/passport/en_US/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,9 @@ def passport_dates(self, birthday: date = date.today()) -> Tuple[str, str, str]:
# Checks if age is less than 5 so issue date is not before birthdate
if age < 5:
issue_date = self.generator.date_time_between(birthday, today)
expiry_date = issue_date.replace(year=issue_date.year + expiry_years)

issue_date_fromat = issue_date.strftime("%d ") + issue_date.strftime("%b ") + issue_date.strftime("%Y")
expiry_date_format = expiry_date.strftime("%d ") + expiry_date.strftime("%b ") + expiry_date.strftime("%Y")
return birth_date, issue_date_fromat, expiry_date_format
elif age >= 26:
expiry_years = 10
issue_date = self.generator.date_time_between(today - timedelta(days=expiry_years * 365 - 1), today)
expiry_date = issue_date.replace(year=issue_date.year + expiry_years)
issue_date_fromat = issue_date.strftime("%d ") + issue_date.strftime("%b ") + issue_date.strftime("%Y")
expiry_date_format = expiry_date.strftime("%d ") + expiry_date.strftime("%b ") + expiry_date.strftime("%Y")
return birth_date, issue_date_fromat, expiry_date_format

else:
# In cases between age 16 and 26, the issue date is 5 years ago, but expiry may be in 10 or 5 years
expiry_years = 5
Expand All @@ -66,11 +56,13 @@ def passport_dates(self, birthday: date = date.today()) -> Tuple[str, str, str]:
issue_date = self.generator.date_time_between(today - timedelta(days=expiry_years * 365 - 1), today)
expiry_years = 10

expiry_date = issue_date.replace(year=issue_date.year + expiry_years)
if issue_date.day == 29 and issue_date.month == 2:
issue_date -= timedelta(days=1)
expiry_date = issue_date.replace(year=issue_date.year + expiry_years)

issue_date_fromat = issue_date.strftime("%d ") + issue_date.strftime("%b ") + issue_date.strftime("%Y")
expiry_date_format = expiry_date.strftime("%d ") + expiry_date.strftime("%b ") + expiry_date.strftime("%Y")
return birth_date, issue_date_fromat, expiry_date_format
issue_date_format = issue_date.strftime("%d ") + issue_date.strftime("%b ") + issue_date.strftime("%Y")
expiry_date_format = expiry_date.strftime("%d ") + expiry_date.strftime("%b ") + expiry_date.strftime("%Y")
return birth_date, issue_date_format, expiry_date_format

def passport_gender(self, seed: int = 0) -> str:
"""Generates a string representing the gender displayed on a passport
Expand Down

0 comments on commit ee3ed38

Please sign in to comment.