Untitled

 avatar
unknown
plain_text
a month ago
1.6 kB
7
Indexable
def _calc_volume_and_count_date(self, line):
    if line.units == UNITS.KW_MONTHS:
        volume = line.volume * 1000
        count_date = get_count_months_just_period(line.vintage_from, line.vintage_to)
    elif line.units == UNITS.MW_DAYS:
        volume = line.volume
        count_date = get_count_days(line.vintage_from, line.vintage_to)
    else:
        volume, count_date = 0, 0
    return volume, count_date


def get_count_months_just_period(period_from, period_to):
    if period_from > period_to:
        return Decimal('0.0')

    if (period_from.year, period_from.month) == (period_to.year, period_to.month):
        days_in_month = calendar.monthrange(period_from.year, period_from.month)[1]
        days_diff = (period_to - period_from).days + 1
        total = Decimal(days_diff) / Decimal(days_in_month)
    else:
        days_in_start_month = calendar.monthrange(period_from.year, period_from.month)[1]
        days_first = days_in_start_month - period_from.day + 1
        total = Decimal(days_first) / Decimal(days_in_start_month)

        days_in_end_month = calendar.monthrange(period_to.year, period_to.month)[1]
        days_last = period_to.day
        total += Decimal(days_last) / Decimal(days_in_end_month)

        current = (period_from.replace(day=1) + relativedelta(months=1))
        target = period_to.replace(day=1)

        while current < target:
            total += Decimal('1.0')
            current += relativedelta(months=1)

    return total

def get_count_days(period_from, period_to):
    return (period_to.date() - period_from.date()).days + 1
Editor is loading...
Leave a Comment