-
Notifications
You must be signed in to change notification settings - Fork 4.9k
[time] Cross-platform support for device timezones via tzlocal
#925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||||||||||
from enum import Enum | ||||||||||||||
import json | ||||||||||||||
from typing import Sequence | ||||||||||||||
|
||||||||||||||
from tzlocal import get_localzone | ||||||||||||||
from zoneinfo import ZoneInfo | ||||||||||||||
from mcp.server import Server | ||||||||||||||
from mcp.server.stdio import stdio_server | ||||||||||||||
|
@@ -39,10 +39,18 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo: | |||||||||||||
if local_tz_override: | ||||||||||||||
return ZoneInfo(local_tz_override) | ||||||||||||||
|
||||||||||||||
# Get local timezone from datetime.now() | ||||||||||||||
# First, try to get local timezone from tzlocal | ||||||||||||||
try: | ||||||||||||||
local_tz = get_localzone() | ||||||||||||||
return ZoneInfo(str(local_tz)) | ||||||||||||||
except Exception as e: | ||||||||||||||
pass | ||||||||||||||
|
||||||||||||||
# If that fails, try getting local timezone from datetime.now() | ||||||||||||||
tzinfo = datetime.now().astimezone(tz=None).tzinfo | ||||||||||||||
if tzinfo is not None: | ||||||||||||||
return ZoneInfo(str(tzinfo)) | ||||||||||||||
|
||||||||||||||
raise McpError("Could not determine local timezone - tzinfo is None") | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't leave comments or suggestions on unchanged lines, but I think you also want to change this in the
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
from freezegun import freeze_time | ||
from mcp.shared.exceptions import McpError | ||
import pytest | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion to add try/catch if the local override results in an exception, plus it looks like McpError expects an object with a message and not a string:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will also need to add ErrorData and INVALID_PARAMS fom mcp.types, for example:
from mcp.types import Tool, TextContent, ImageContent, EmbeddedResource, ErrorData, INVALID_PARAMS