From 46f04fbbe588f91667a5a03796e8d796cdadb9de Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 11 Nov 2025 09:36:29 -0800 Subject: [PATCH] switched from using the bot's local time instead of the message sent time for keyword replacement --- modules/command_manager.py | 31 ++++++++++++++++++++++--------- modules/commands/base_command.py | 30 +++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/modules/command_manager.py b/modules/command_manager.py index d37bdc1..92ac4d3 100644 --- a/modules/command_manager.py +++ b/modules/command_manager.py @@ -8,6 +8,8 @@ import re import time import asyncio from typing import List, Dict, Tuple, Optional, Any +from datetime import datetime +import pytz from meshcore import EventType from .models import MeshMessage @@ -98,15 +100,26 @@ class CommandManager: try: connection_info = self.build_enhanced_connection_info(message) - # Format timestamp - if message.timestamp and message.timestamp != 'unknown': - try: - from datetime import datetime - dt = datetime.fromtimestamp(message.timestamp) - time_str = dt.strftime("%H:%M:%S") - except: - time_str = str(message.timestamp) - else: + # Format timestamp - use bot's local time instead of message timestamp + # to avoid issues when sender's clock is wrong + try: + # Get configured timezone or use system timezone + timezone_str = self.bot.config.get('Bot', 'timezone', fallback='') + + if timezone_str: + try: + # Use configured timezone + tz = pytz.timezone(timezone_str) + dt = datetime.now(tz) + except pytz.exceptions.UnknownTimeZoneError: + # Fallback to system timezone if configured timezone is invalid + dt = datetime.now() + else: + # Use system timezone + dt = datetime.now() + + time_str = dt.strftime("%H:%M:%S") + except: time_str = "Unknown" # Format the response with available message data diff --git a/modules/commands/base_command.py b/modules/commands/base_command.py index 5fb400d..f9f0af4 100644 --- a/modules/commands/base_command.py +++ b/modules/commands/base_command.py @@ -6,6 +6,8 @@ Provides common functionality and interface for command implementations from abc import ABC, abstractmethod from typing import Optional, List, Dict, Any, Tuple +from datetime import datetime +import pytz from ..models import MeshMessage @@ -154,15 +156,25 @@ class BaseCommand(ABC): return connection_info def format_timestamp(self, message: MeshMessage) -> str: - """Format message timestamp for display""" - if message.timestamp and message.timestamp != 'unknown': - try: - from datetime import datetime - dt = datetime.fromtimestamp(message.timestamp) - return dt.strftime("%H:%M:%S") - except: - return str(message.timestamp) - else: + """Format current bot time for display (not sender's timestamp to avoid clock issues)""" + try: + # Get configured timezone or use system timezone + timezone_str = self.bot.config.get('Bot', 'timezone', fallback='') + + if timezone_str: + try: + # Use configured timezone + tz = pytz.timezone(timezone_str) + dt = datetime.now(tz) + except pytz.exceptions.UnknownTimeZoneError: + # Fallback to system timezone if configured timezone is invalid + dt = datetime.now() + else: + # Use system timezone + dt = datetime.now() + + return dt.strftime("%H:%M:%S") + except: return "Unknown" def format_response(self, message: MeshMessage, response_format: str) -> str: