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.
This commit is contained in:
parent
c5227100b8
commit
59e4d53215
1 changed files with 18 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue