Last active
December 5, 2022 05:14
-
-
Save juanarrivillaga/64ad7de226ca2761d930d329450e7307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
def is_leap_year(year: int) -> int: | |
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) | |
def add_months(date: datetime.date, months: int) -> datetime.date: | |
for _ in range(1, months + 1): | |
year, month = date.year, date.month | |
n_days = DAYS_IN_MONTH[month % 12] # the *next* month's days | |
if is_leap_year(year) and date.month == 2: # febrero | |
n_days += 1 # 28 --> 29 | |
date += datetime.timedelta(days=n_days) | |
return date | |
if __name__ == "__main__": | |
print(add_months(datetime.date(2022, 12, 30), 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment