There are multiple ways to approach the problem as usual, based on what you already have I would consider first validating the date, if the date entered is not valid the rest does not make sense. Done this we can raise a very simple algorithm considering that we are only going to add up to 5 days (so we are only going to move between two months at the most):
-
If the day of the month plus the number of days increased is less than the maximum number of days we simply add the days.
-
If the above is not fulfilled we must add one a month with the proviso that if we are in December the month will become January and we add one a year.
The day of the new date will be the days that exceed the maximum of days of the month.
The code might look something like this, if I have not screwed up somewhere:
def cinco_dias(fecha):
año, mes, dia = (int(n) for n in fecha.split("-"))
if año < 1 or not isinstance(año, int):
raise ValueError("El año debe ser un entero mayor de 0")
if mes in (1, 3, 5, 7, 8, 10, 12):
dias_mes = 31
elif mes == 2:
if año % 4 == 0 and (año % 100 != 0 or año % 400 == 0):
dias_mes = 29
else:
dias_mes = 28
elif mes in (4, 6, 9, 11):
dias_mes = 30
else:
raise ValueError("El mes debe ser un entero entre 1 y 12 incluidos")
if not 1 <= dia <= dias_mes:
raise ValueError("{} no es un día válido para el {:04d}/{:02d}".format(dia, año, mes))
dias = []
for n in range(1, 6):
if dia + n <= dias_mes:
dias.append('{:04d}-{:02d}-{:02d}'.format(año, mes, dia + n))
else:
if mes != 12:
dias.append('{:04d}-{:02d}-{:02d}'.format(año, mes+1, n - (dias_mes - dia)))
else:
dias.append('{:04d}-01-{:02d}'.format(año + 1, n - (dias_mes - dia)))
return dias
fecha = input("Introduce una fecha: ")
print(cinco_dias(fecha))
Examples of departures:
Introduce una fecha: 2020-02-27
['2020-02-28', '2020-02-29', '2020-03-01', '2020-03-02', '2020-03-03']
Introduce una fecha: 2018-12-29
['2018-12-30', '2018-12-31', '2019-01-01', '2019-01-02', '2019-01-03']
Introduce una fecha: 2023-02-29
Traceback (most recent call last):
File "prueba.py", line 38, in <module>
print(cinco_dias(fecha))
File "prueba.py", line 23, in cinco_dias
raise ValueError("{} no es un día válido para el {}/{}".format(dia, mes, año))
ValueError: 29 no es un día válido para el 2023/02
Introduce una fecha: 2018-04-31
Traceback (most recent call last):
File "prueba.py", line 38, in <module>
print(cinco_dias(fecha))
File "prueba.py", line 23, in cinco_dias
raise ValueError("{} no es un día válido para el {:04d}/{:02d}".format(dia, año, mes))
ValueError: 31 no es un día válido para el 2018/04
It should be noted that the above does not make much sense, except in academic or recreational settings, when Python already provides this functionality thanks to the datetime
module and the datetime.timedelta
method:
import datetime
def añadir_dias(fecha, dias):
fecha = datetime.datetime.strptime(fecha, "%Y-%m-%d")
fechas = [datetime.datetime.strftime(fecha + datetime.timedelta(days=d), "%Y-%m-%d")
for d in range(1, días + 1)]
return fechas
fecha = input("Introduce una fecha: ")
print(añadir_dias(fecha), 5)