summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-09-07 20:43:15 +0200
committerxengineering <me@xengineering.eu>2024-09-08 17:21:32 +0200
commit36936455e7805f824cb07fe9b23500a882426f98 (patch)
tree5059f8b55ffcee5bcf6f4fa5d5ef91e6274524db
parent226f5afa968ead223baa5a1b0fafb481f9761f0f (diff)
downloadfinance-py-36936455e7805f824cb07fe9b23500a882426f98.tar
finance-py-36936455e7805f824cb07fe9b23500a882426f98.tar.zst
finance-py-36936455e7805f824cb07fe9b23500a882426f98.zip
Implement Flow.since and Flow.until
That way a flow can be limited to a certain time frame.
-rw-r--r--finance/flow.py10
-rw-r--r--finance/test_flow.py12
2 files changed, 21 insertions, 1 deletions
diff --git a/finance/flow.py b/finance/flow.py
index a5817af..9ea72c8 100644
--- a/finance/flow.py
+++ b/finance/flow.py
@@ -9,12 +9,22 @@ class Flow:
"""Time-discrete flow of money paid on the first day of a month"""
amount: Decimal
+ since: None | datetime
+ until: None | datetime
def integrate(self, start: datetime, end: datetime) -> Decimal:
"""Integrate the flow between two dates to an amount of money"""
payments: int = 0
+ if self.since is not None:
+ if start < self.since:
+ start = self.since
+
+ if self.until is not None:
+ if end > self.until:
+ end = self.until
+
for candidate in monthly_candidates(start):
if start <= candidate <= end:
payments += 1
diff --git a/finance/test_flow.py b/finance/test_flow.py
index aad4e74..02b225c 100644
--- a/finance/test_flow.py
+++ b/finance/test_flow.py
@@ -5,13 +5,23 @@ from finance import flow
def test_income_integration() -> None:
- inc = flow.Flow(amount=Decimal(3000.0))
+ inc = flow.Flow(
+ amount=Decimal(3000.0),
+ since=datetime(2023, 1, 1),
+ until=datetime(2026, 1, 5),
+ )
tests = (
(datetime(2024, 3, 12), datetime(2024, 4, 2), Decimal(3000.0)),
(datetime(2024, 3, 1), datetime(2024, 3, 15), Decimal(3000.0)),
(datetime(2024, 2, 25), datetime(2024, 3, 1), Decimal(3000.0)),
(datetime(2024, 2, 25), datetime(2024, 6, 12), Decimal(12000.0)),
+ (datetime(2022, 1, 5), datetime(2022, 9, 14), Decimal(0.0)),
+ (datetime(2022, 1, 5), datetime(2023, 1, 1), Decimal(3000.0)),
+ (datetime(2022, 7, 4), datetime(2024, 12, 8), Decimal(72000.0)),
+ (datetime(2025, 11, 4), datetime(2026, 5, 9), Decimal(6000.0)),
+ (datetime(2026, 1, 5), datetime(2027, 1, 1), Decimal(0.0)),
+ (datetime(2027, 5, 28), datetime(2028, 7, 7), Decimal(0.0)),
)
for test in tests: