Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/time/src/mcp_server_time/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,7 +43,26 @@ 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 ZoneInfoNotFoundError:
# If it fails (e.g., when tzinfo is just an offset like '-03'),
# find a matching IANA timezone based on offset

# 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")


Expand Down