blob: 94759ff49de54c70d407f3ccba2e10ff76c88f5a (
plain)
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
|
import dataclasses
from datetime import datetime
@dataclasses.dataclass(kw_only=True, frozen=True)
class Income:
"""Income models financial income paid on the first day of a month"""
amount: float
def integrate(self, start: datetime, end: datetime) -> float:
"""Integrate the income from a start to an end date"""
retval = 0.0
current = datetime(start.year, start.month, 1)
if start == current:
retval += self.amount
while True:
if current.month == 12:
current = datetime(current.year + 1, 1, 1)
else:
current = datetime(current.year, current.month + 1, 1)
if current <= end:
retval += self.amount
else:
break
return retval
|