From 59e4d53215f27b6c54707ce8ed5ffcf681851974 Mon Sep 17 00:00:00 2001 From: padreug Date: Sat, 5 Jul 2025 16:06:00 +0200 Subject: [PATCH] Enhance timestamp parsing in transaction processing: Improved handling of PostgreSQL timestamp formats to ensure consistency in UTC. Added error logging for invalid timestamps and implemented a fallback mechanism to use the current UTC time when parsing fails, enhancing robustness in transaction time management. --- transaction_processor.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/transaction_processor.py b/transaction_processor.py index 58d251d..3f75adf 100644 --- a/transaction_processor.py +++ b/transaction_processor.py @@ -503,8 +503,24 @@ class LamassuTransactionProcessor: processed_row[key] = float(value) if value else 0.0 elif key == 'transaction_time': from datetime import datetime - # Parse timestamp and ensure it's in UTC for consistency - dt = datetime.fromisoformat(value.replace('Z', '+00:00')) + # Parse PostgreSQL timestamp format and ensure it's in UTC for consistency + # Handle formats like: '2025-07-04 23:12:42.627+00' (PostgreSQL format) + timestamp_str = value + + # Fix PostgreSQL timezone format: +00 -> +00:00 + if timestamp_str.endswith('+00'): + timestamp_str = timestamp_str + ':00' + elif timestamp_str.endswith('Z'): + timestamp_str = timestamp_str.replace('Z', '+00:00') + + try: + dt = datetime.fromisoformat(timestamp_str) + except ValueError as e: + logger.error(f"Failed to parse timestamp '{value}': {e}") + # Fallback to current time with warning + dt = datetime.now(timezone.utc) + logger.warning(f"Using current UTC time as fallback for invalid timestamp: {dt}") + # Convert to UTC if not already if dt.tzinfo is None: # Assume UTC if no timezone info