get_timezone_info
Get current time and UTC offset for any IANA timezone name.
Instructions
Get information about a specific timezone including current time and UTC offset.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timezone_name | Yes | IANA timezone name (e.g., 'America/New_York', 'Europe/London') |
Implementation Reference
- The GetTimeZoneInfoToolHandler class that implements the 'get_timezone_info' tool. Contains the run_tool method (lines 108-150) which executes the logic: validates args, gets zoneinfo, computes current local time, UTC time, UTC offset, DST status, and timezone abbreviation, then returns the result as JSON.
class GetTimeZoneInfoToolHandler(ToolHandler): """ Tool handler for getting information about timezones. """ def __init__(self): super().__init__("get_timezone_info") def get_tool_description(self) -> Tool: """ Return the tool description for timezone information lookup. """ return Tool( name=self.name, description="""Get information about a specific timezone including current time and UTC offset.""", inputSchema={ "type": "object", "properties": { "timezone_name": { "type": "string", "description": "IANA timezone name (e.g., 'America/New_York', 'Europe/London')" } }, "required": ["timezone_name"] } ) async def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]: """ Execute the timezone info tool. """ try: self.validate_required_args(args, ["timezone_name"]) timezone_name = args["timezone_name"] logger.info(f"Getting timezone info for: {timezone_name}") # Get timezone info timezone = utils.get_zoneinfo(timezone_name) current_time = datetime.now(timezone) utc_time = datetime.utcnow() # Calculate UTC offset offset = current_time.utcoffset() offset_hours = offset.total_seconds() / 3600 if offset else 0 timezone_info = { "timezone_name": timezone_name, "current_local_time": current_time.isoformat(timespec="seconds"), "current_utc_time": utc_time.isoformat(timespec="seconds"), "utc_offset_hours": offset_hours, "is_dst": current_time.dst() is not None and current_time.dst().total_seconds() > 0, "timezone_abbreviation": current_time.strftime("%Z"), } return [ TextContent( type="text", text=json.dumps(timezone_info, indent=2) ) ] except Exception as e: logger.exception(f"Error in get_timezone_info: {str(e)}") return [ TextContent( type="text", text=f"Error getting timezone info: {str(e)}" ) ] - The get_tool_description method defining the input schema for 'get_timezone_info'. Takes a required 'timezone_name' string parameter (IANA timezone name).
def get_tool_description(self) -> Tool: """ Return the tool description for timezone information lookup. """ return Tool( name=self.name, description="""Get information about a specific timezone including current time and UTC offset.""", inputSchema={ "type": "object", "properties": { "timezone_name": { "type": "string", "description": "IANA timezone name (e.g., 'America/New_York', 'Europe/London')" } }, "required": ["timezone_name"] } ) - src/mcp_weather_server/server.py:92-92 (registration)Registration of GetTimeZoneInfoToolHandler in the register_all_tools() function, which instantiates and adds it to the global tool_handlers registry.
add_tool_handler(GetTimeZoneInfoToolHandler()) - src/mcp_weather_server/server.py:33-36 (registration)Import of GetTimeZoneInfoToolHandler from tools.tools_time module into server.py.
GetCurrentDateTimeToolHandler, GetTimeZoneInfoToolHandler, ConvertTimeToolHandler, ) - The get_zoneinfo helper function used by GetTimeZoneInfoToolHandler.run_tool to resolve an IANA timezone name string to a ZoneInfo object.
def get_zoneinfo(timezone_name: str) -> ZoneInfo: try: return ZoneInfo(timezone_name) except Exception as e: error_data = ErrorData(code=-1, message=f"Invalid timezone: {str(e)}") raise McpError(error_data)