Python - Calcular días entre fechas basado en un año de 360 días

 
Vista:
Imágen de perfil de Carlos

Calcular días entre fechas basado en un año de 360 días

Publicado por Carlos (1 intervención) el 20/09/2019 21:39:44
He tenido que aplicar este tipo de cálculos en la Universidad en la que trabajo, es muy útil sobre todo para cálculos contables. Busque información en la web sin éxito, asi que me tocó realizar estas funciones que les comparto, están un poco rústico, las hice al apuro, solo es cuestión de mejorarlas; espero a alguien le sirva:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def proximafecha(fecha, periocidad):
    day = fecha.day
    month = fecha.month
    year = fecha.year
    if (month + periocidad) > 12:
        sobrante = (month + periocidad) - 12
        nextmonth = sobrante
        nextyear = year + 1
    elif month == 12 and periocidad == 1:
        nextmonth = 1
        nextyear = year + 1
    else:
        nextmonth = month + periocidad
        nextyear = year
    dia = fecha.day
    while dia >= 0:
        try:
            return datetime(nextyear, nextmonth, dia)
        except:
            dia -= 1
 
 
 
 
 
def dias360(fechai, fechaf):
    import calendar
    dias = 0
    sumar = 0
    entro = False
    sumar2 = False
    while fechai < fechaf:
        if sumar2:
            try:
                fechai = fechai + timedelta(days=3)
            except:
                fechai = fechai + timedelta(days=2)
            sumar2 = False
        if (fechaf - fechai).days >= 28:
            fecha = fechaf
            sumar = (fechaf - fechai).days
            if sumar > 30:
                sumar = 30
            if sumar == 28:
                sumar = 30
            entro = True
        else:
            entro = False
            sumar = (fechaf - fechai).days
            if fechai.month == 2 and fechaf.month != 2:
                if calendar.isleap(fechai.year) and fechai.day >= 28:
                    sumar += 1
                elif not calendar.isleap(fechai.year) and fechai.day < 28:
                    sumar += 2
            if sumar > 30:
                sumar = 30
        dias += sumar
        if fechai.month == 2 and fechai.day == 28 and not entro:
            break
        if fechai.month == 2 and fechai.day == 28:
            sumar2 = True
        fechai = proximafecha(fechai, 1).date()
    return dias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder