From 74c3410a78f41a709a1de222a60283cf82adcac4 Mon Sep 17 00:00:00 2001 From: Np3ir <53929508+Np3ir@users.noreply.github.com> Date: Mon, 2 Jun 2025 20:48:55 -0400 Subject: [PATCH 1/2] Update dates.py Change Summary: Improved str_to_datetime_obj() for safer date parsing Previous behavior: The function only handled the specific case "0000-00-00" by replacing it with "1980-01-01". It did not handle other malformed or missing inputs and could raise exceptions without logging. New behavior: The updated version introduces input validation and exception handling: Handles None, empty strings, and any value starting with "0000" Logs a warning when an invalid date is found and defaults to "1980-01-01" Catches parsing errors and logs them as errors Returns None on failure Benefits: Prevents crashes from malformed or missing date strings Adds meaningful logs for debugging Improves reliability when consuming date data from external APIs --- deemon/utils/dates.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/deemon/utils/dates.py b/deemon/utils/dates.py index 7de68ae..9b68586 100644 --- a/deemon/utils/dates.py +++ b/deemon/utils/dates.py @@ -34,11 +34,15 @@ def format_date_string(d: str): def ui_date(d: datetime): return datetime.strftime(d, '%b %d, %Y') -def str_to_datetime_obj(d: str) -> datetime: - if d == "0000-00-00": - d = "1980-01-01" - return datetime.strptime(d, "%Y-%m-%d") - +ddef str_to_datetime_obj(d: str) -> datetime | None: + try: + if not d or d.strip() == "" or d.startswith("0000"): + logger.warning(f"[dates] Fecha inválida detectada ('{d}'), usando 1980-01-01 como valor predeterminado.") + return datetime.strptime("1980-01-01", "%Y-%m-%d") + return datetime.strptime(d, "%Y-%m-%d") + except Exception as e: + logger.error(f"[dates] Error al convertir fecha '{d}': {e}") + return None def get_friendly_date(d: int): input_date = datetime.fromtimestamp(d).date() From 48ada3aee9482e95d9c703624d60f6b8bd038157 Mon Sep 17 00:00:00 2001 From: Np3ir <53929508+Np3ir@users.noreply.github.com> Date: Mon, 2 Jun 2025 21:23:30 -0400 Subject: [PATCH 2/2] Update dates.py --- deemon/utils/dates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deemon/utils/dates.py b/deemon/utils/dates.py index 9b68586..ac2f4cf 100644 --- a/deemon/utils/dates.py +++ b/deemon/utils/dates.py @@ -37,8 +37,8 @@ def ui_date(d: datetime): ddef str_to_datetime_obj(d: str) -> datetime | None: try: if not d or d.strip() == "" or d.startswith("0000"): - logger.warning(f"[dates] Fecha inválida detectada ('{d}'), usando 1980-01-01 como valor predeterminado.") - return datetime.strptime("1980-01-01", "%Y-%m-%d") + logger.warning(f"[dates] Invalid date detected ('{d}'), using 1900-01-01 as default value.") + return datetime.strptime("1900-01-01", "%Y-%m-%d") return datetime.strptime(d, "%Y-%m-%d") except Exception as e: logger.error(f"[dates] Error al convertir fecha '{d}': {e}")