import re, datetime
def add_months(datestr, months):
ref_year, ref_month = "", ""
ref_year_is_leap_year = False
aux_date = str(datetime.datetime.strptime(datestr, "%Y-%m-%d"))
print(repr(aux_date))
for i_month in range(int(months)):
# I add a unit since the months are "numerical quantities",
# that is, they are expressed in natural numbers, so I need it
# to start from 1 and not from 0 like the iter variable in python
i_month = i_month + 1
m1 = re.search(
r"(?P<year>\d*)-(?P<month>\d{2})-(?P<startDay>\d{2})",
aux_date,
re.IGNORECASE,
)
if m1:
ref_year, ref_month = (
str(m1.groups()[0]).strip(),
str(m1.groups()[1]).strip(),
)
number_of_days_in_each_month = {
"01": "31",
"02": "28",
"03": "31",
"04": "30",
"05": "31",
"06": "30",
"07": "31",
"08": "31",
"09": "30",
"10": "31",
"11": "30",
"12": "31",
}
n_days_in_this_i_month = number_of_days_in_each_month[ref_month]
print(n_days_in_this_i_month) # nro days to increment in each i month iteration
if (
int(ref_year) % 4 == 0
and int(ref_year) % 100 == 0
and int(ref_year) % 400 != 0
):
ref_year_is_leap_year = True # divisible entre 4 y 10 y no entre 400, para determinar que sea un año bisciesto
if ref_year_is_leap_year == True and ref_month == "02":
n_days_in_this_i_month = str(int(n_days_in_this_i_month) + 1) # 28 --> 29
aux_date = (
datetime.datetime.strptime(datestr, "%Y-%m-%d")
+ datetime.timedelta(days=int(n_days_in_this_i_month))
).strftime("%Y-%m-%d")
print(repr(aux_date))
return aux_date
print(repr(add_months("2022-12-30", "3")))
为什么aux_date
变量,而不是progressively increasing,过go 月份的天数,只限于将1月份的31天相加,然后将它们添加回原始数量,停留在那里,而不是推进for
循环的每个迭代?
这个for
循环的目标是实现一个incremental iteration loop,其中添加了天数,而不是总是返回到原始数量来反复添加相同的内容.
Updated function 算法rithm个
在这次编辑中,我修改了一些细节和冗余,还修复了原始代码中存在的一些错误.
def add_months(datestr, months):
ref_year, ref_month = "", ""
ref_year_is_leap_year = False #condicional booleano, cuya logica binaria intenta establecer si es o no bisiesto el año tomado como referencia
aux_date = datetime.datetime.strptime(datestr, "%Y-%m-%d")
for i_month in range(int(months)):
i_month = i_month + 1 # I add a unit since the months are "numerical quantities", that is, they are expressed in natural numbers, so I need it to start from 1 and not from 0 like the iter variable in python
m1 = re.search( r"(?P<year>\d*)-(?P<month>\d{2})-(?P<startDay>\d{2})", str(aux_date), re.IGNORECASE, )
if m1:
ref_year, ref_month = ( str(m1.groups()[0]).strip(), str( int(m1.groups()[1]) + 1).strip(), )
if( len(ref_month) == 1 ): ref_month = "0" + ref_month
if( int(ref_month) > 12 ): ref_month = "01"
print(ref_month)
number_of_days_in_each_month = {
"01": "31",
"02": "28",
"03": "31",
"04": "30",
"05": "31",
"06": "30",
"07": "31",
"08": "31",
"09": "30",
"10": "31",
"11": "30",
"12": "31",
}
n_days_in_this_i_month = number_of_days_in_each_month[ref_month]
if ( int(ref_year) % 4 == 0 and int(ref_year) % 100 != 0 ) or ( int(ref_year) % 400 == 0 ): ref_year_is_leap_year = True ref_year_is_leap_year = True # divisible entre 4 y 10 y no entre 400, para determinar que sea un año bisciesto
if ref_year_is_leap_year == True and ref_month == "02": n_days_in_this_i_month = str(int(n_days_in_this_i_month) + 1) # 28 --> 29
print(n_days_in_this_i_month) # nro days to increment in each i month iteration
aux_date = aux_date + datetime.timedelta(days=int(n_days_in_this_i_month))
return datetime.datetime.strftime(aux_date, "%Y-%m-%d")