2023-01-11 22:12:58 +00:00
|
|
|
def isLeapYear(year):
|
|
|
|
return year % 400 == 0 if year % 100 == 0 else year % 4 == 0
|
|
|
|
|
|
|
|
|
|
|
|
def monthDays(year, month):
|
|
|
|
assert month > 0
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
MONTHDAYS = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
|
|
|
|
days = MONTHDAYS[month]
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
return days + 1 if (isLeapYear(year) and month == 2) else days
|
|
|
|
|
|
|
|
|
|
|
|
def nextDay(year, month, day):
|
|
|
|
if not dateIsValid(year, month, day): return
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Verifica se é o último dia do ano
|
|
|
|
if (month, day) == (12, 31):
|
|
|
|
year += 1
|
|
|
|
month = 1
|
|
|
|
day = 1
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Verifica se é o último dia do mês
|
|
|
|
elif (monthDays(year, month) == day):
|
|
|
|
month += 1
|
|
|
|
day = 1
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Dia comum
|
|
|
|
else:
|
|
|
|
day += 1
|
|
|
|
|
|
|
|
return year, month, day
|
|
|
|
|
|
|
|
|
|
|
|
def dateIsValid(year, month, day):
|
|
|
|
return False if (month > 12 or month <= 0) or (day > monthDays(year, month) or day <= 0) else True
|
|
|
|
|
|
|
|
|
|
|
|
def previousDay(year, month, day):
|
|
|
|
if not dateIsValid(year, month, day): return
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Primeiro dia do ano
|
|
|
|
if (month, day) == (1, 1):
|
|
|
|
year -= 1
|
|
|
|
month = 12
|
|
|
|
day = 31
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Primeiro dia do mês (sem ser janeiro)
|
|
|
|
elif day == 1:
|
|
|
|
day = monthDays(year, month - 1)
|
|
|
|
month -= 1
|
2023-05-16 20:00:37 +00:00
|
|
|
|
2023-01-11 22:12:58 +00:00
|
|
|
# Dia comum
|
|
|
|
else:
|
|
|
|
day -= 1
|
2023-05-16 20:00:37 +00:00
|
|
|
|
|
|
|
return year, month, day
|