From 332072b3f46ce1eeecb7127da2177031ec8f60ef Mon Sep 17 00:00:00 2001 From: Andres Babino Date: Sat, 5 Apr 2025 08:07:18 -0300 Subject: [PATCH 1/2] Enhance timezone detection in get_local_tz function to handle tzinfo offsets and improve error handling --- src/time/src/mcp_server_time/server.py | 28 +++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/time/src/mcp_server_time/server.py b/src/time/src/mcp_server_time/server.py index 67c9024b52..d1ddc3526b 100644 --- a/src/time/src/mcp_server_time/server.py +++ b/src/time/src/mcp_server_time/server.py @@ -42,7 +42,33 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo: # Get local timezone from datetime.now() tzinfo = datetime.now().astimezone(tz=None).tzinfo if tzinfo is not None: - return ZoneInfo(str(tzinfo)) + try: + # Try to directly use the tzinfo string representation + return ZoneInfo(str(tzinfo)) + except Exception: + # If it fails (e.g., when tzinfo is just an offset like '-03'), + # find a matching IANA timezone based on offset + try: + from zoneinfo import available_timezones + + # Get the current UTC offset in seconds + offset_seconds = datetime.now().astimezone().utcoffset().total_seconds() + + # Find the first timezone that matches our offset + for tz_name in available_timezones(): + try: + tz = ZoneInfo(tz_name) + dt = datetime.now(tz) + if dt.utcoffset().total_seconds() == offset_seconds: + return ZoneInfo(tz_name) + except Exception: + continue + + # If we get here, no matching timezone was found + raise McpError(f"Could not find a matching IANA timezone for offset {offset_seconds/3600} hours") + except Exception as e: + raise McpError(f"Failed to determine IANA timezone: {str(e)}") + raise McpError("Could not determine local timezone - tzinfo is None") From 90cc193bcd1c57ef97d9cc22c7e1fefd3c3110b2 Mon Sep 17 00:00:00 2001 From: Andres Babino Date: Sat, 5 Apr 2025 08:34:55 -0300 Subject: [PATCH 2/2] Refactor timezone detection in get_local_tz function to improve error handling and streamline logic for matching IANA timezones based on UTC offset --- src/time/src/mcp_server_time/server.py | 38 +++++++++++--------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/time/src/mcp_server_time/server.py b/src/time/src/mcp_server_time/server.py index d1ddc3526b..fe43fd8853 100644 --- a/src/time/src/mcp_server_time/server.py +++ b/src/time/src/mcp_server_time/server.py @@ -3,7 +3,8 @@ import json from typing import Sequence -from zoneinfo import ZoneInfo +from zoneinfo import ZoneInfo, available_timezones +from zoneinfo._common import ZoneInfoNotFoundError from mcp.server import Server from mcp.server.stdio import stdio_server from mcp.types import Tool, TextContent, ImageContent, EmbeddedResource @@ -45,29 +46,22 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo: try: # Try to directly use the tzinfo string representation return ZoneInfo(str(tzinfo)) - except Exception: + except ZoneInfoNotFoundError: # If it fails (e.g., when tzinfo is just an offset like '-03'), # find a matching IANA timezone based on offset - try: - from zoneinfo import available_timezones - - # Get the current UTC offset in seconds - offset_seconds = datetime.now().astimezone().utcoffset().total_seconds() - - # Find the first timezone that matches our offset - for tz_name in available_timezones(): - try: - tz = ZoneInfo(tz_name) - dt = datetime.now(tz) - if dt.utcoffset().total_seconds() == offset_seconds: - return ZoneInfo(tz_name) - except Exception: - continue - - # If we get here, no matching timezone was found - raise McpError(f"Could not find a matching IANA timezone for offset {offset_seconds/3600} hours") - except Exception as e: - raise McpError(f"Failed to determine IANA timezone: {str(e)}") + + # Get the current UTC offset in seconds + offset_seconds = datetime.now().astimezone().utcoffset().total_seconds() + + # Find the first timezone that matches our offset + for tz_name in available_timezones(): + tz = ZoneInfo(tz_name) + dt = datetime.now(tz) + if dt.utcoffset().total_seconds() == offset_seconds: + return ZoneInfo(tz_name) + + # If we get here, no matching timezone was found + raise McpError(f"Could not find a matching IANA timezone for offset {offset_seconds/3600} hours") raise McpError("Could not determine local timezone - tzinfo is None")