From f486114d6e029e1ecb4130351d7792e0925b8bcd Mon Sep 17 00:00:00 2001 From: uvok Date: Fri, 18 Apr 2025 19:24:55 +0200 Subject: Trade needs timestamp for sorting --- trade.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'trade.py') diff --git a/trade.py b/trade.py index 9e782a5..30a0030 100644 --- a/trade.py +++ b/trade.py @@ -1,3 +1,4 @@ +from datetime import datetime from decimal import Decimal from enum import Enum @@ -14,7 +15,7 @@ class Trade: """ def __init__( - self, amount: Decimal, total_cost: Decimal, date: str, refid: str | None = None + self, amount: Decimal, total_cost: Decimal, timestamp: str, refid: str | None = None ) -> None: """ Initialize a new Trade instance. @@ -22,7 +23,7 @@ class Trade: Args: amount (Decimal): The amount of cryptocurrency traded. total_cost (Decimal): The total cost of the trade. - date (str): The date of the trade, formatted as a string. + timestamp (str): The date or timestamp of the trade, formatted as a string. """ if amount < 0 and total_cost < 0: @@ -32,9 +33,17 @@ class Trade: else: raise ValueError("Amount and tota> cost must be same sign") + # force timestamp + try: + datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") + except ValueError: + datetime.strptime(timestamp, "%Y-%m-%d") + timestamp = timestamp + " 00:00:00" + + self.__amount: Decimal = Decimal(amount) self.__total_cost: Decimal = Decimal(total_cost) - self.__date: str = date + self.__timestamp: str = timestamp self.__refid: str | None = refid def remove_coins( @@ -94,7 +103,7 @@ class Trade: Returns: str: The trade date as a string. """ - return self.__date + return self.__timestamp.split(" ")[0] @property def price_per_coin(self) -> Decimal: -- cgit v1.2.3