Skip to content

Commit 28427d8

Browse files
authored
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.
1 parent 2cd2c0f commit 28427d8

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/time/src/mcp_server_time/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
4646
local_tzname = get_localzone_name()
4747
if local_tzname is not None:
4848
return ZoneInfo(local_tzname)
49-
raise McpError("Could not determine local timezone - tzinfo is None")
49+
# Default to UTC if local timezone cannot be determined
50+
return ZoneInfo("UTC")
5051

5152

5253
def get_zoneinfo(timezone_name: str) -> ZoneInfo:

src/time/test/time_server_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,10 @@ def test_get_local_tz_with_valid_iana_name(mock_get_localzone):
486486

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

494494

495495
@patch('mcp_server_time.server.get_localzone_name')

0 commit comments

Comments
 (0)