Fix time server to default to UTC when tzinfo is unavailable (#2738)

When get_localzone_name() returns None, the server now defaults to UTC
instead of raising an error. This makes the server more robust in
environments where the local timezone cannot be determined.
This commit is contained in:
adam jones
2025-09-23 05:09:58 +01:00
committed by GitHub
parent 2cd2c0f3b8
commit 28427d8cd1
2 changed files with 5 additions and 4 deletions

View File

@@ -46,7 +46,8 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
local_tzname = get_localzone_name()
if local_tzname is not None:
return ZoneInfo(local_tzname)
raise McpError("Could not determine local timezone - tzinfo is None")
# Default to UTC if local timezone cannot be determined
return ZoneInfo("UTC")
def get_zoneinfo(timezone_name: str) -> ZoneInfo:

View File

@@ -486,10 +486,10 @@ def test_get_local_tz_with_valid_iana_name(mock_get_localzone):
@patch('mcp_server_time.server.get_localzone_name')
def test_get_local_tz_when_none_returned(mock_get_localzone):
"""Test error when tzlocal returns None."""
"""Test default to UTC when tzlocal returns None."""
mock_get_localzone.return_value = None
with pytest.raises(McpError, match="Could not determine local timezone"):
get_local_tz()
result = get_local_tz()
assert str(result) == "UTC"
@patch('mcp_server_time.server.get_localzone_name')