convert_time
Convert time between different timezones using IANA timezone names. Input a datetime in ISO format or use 'now' for current time conversion.
Instructions
Convert time from one timezone to another.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| datetime_str | Yes | DateTime string in ISO format (e.g., '2024-01-15T14:30:00') or 'now' for current time | |
| from_timezone | Yes | Source timezone (IANA timezone name) | |
| to_timezone | Yes | Target timezone (IANA timezone name) |
Implementation Reference
- The ConvertTimeToolHandler class defines and implements the 'convert_time' tool handler. It includes the tool name, input schema, and the full execution logic in run_tool for converting datetime between timezones.class ConvertTimeToolHandler(ToolHandler): """ Tool handler for converting time between different timezones. """ def __init__(self): super().__init__("convert_time") def get_tool_description(self) -> Tool: """ Return the tool description for time conversion. """ return Tool( name=self.name, description="""Convert time from one timezone to another.""", inputSchema={ "type": "object", "properties": { "datetime_str": { "type": "string", "description": "DateTime string in ISO format (e.g., '2024-01-15T14:30:00') or 'now' for current time" }, "from_timezone": { "type": "string", "description": "Source timezone (IANA timezone name)" }, "to_timezone": { "type": "string", "description": "Target timezone (IANA timezone name)" } }, "required": ["datetime_str", "from_timezone", "to_timezone"] } ) async def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]: """ Execute the time conversion tool. """ try: self.validate_required_args(args, ["datetime_str", "from_timezone", "to_timezone"]) datetime_str = args["datetime_str"] from_timezone_name = args["from_timezone"] to_timezone_name = args["to_timezone"] logger.info(f"Converting time '{datetime_str}' from {from_timezone_name} to {to_timezone_name}") # Get timezone objects from_timezone = utils.get_zoneinfo(from_timezone_name) to_timezone = utils.get_zoneinfo(to_timezone_name) # Parse the datetime if datetime_str.lower() == "now": source_time = datetime.now(from_timezone) else: # Parse the datetime string and localize it if datetime_str.endswith('Z'): # UTC time naive_time = datetime.fromisoformat(datetime_str[:-1]) source_time = naive_time.replace(tzinfo=from_timezone) else: # Assume local time in from_timezone naive_time = datetime.fromisoformat(datetime_str) source_time = naive_time.replace(tzinfo=from_timezone) # Convert to target timezone target_time = source_time.astimezone(to_timezone) conversion_result = { "original_datetime": source_time.isoformat(timespec="seconds"), "original_timezone": from_timezone_name, "converted_datetime": target_time.isoformat(timespec="seconds"), "converted_timezone": to_timezone_name, "time_difference_hours": (target_time.utcoffset().total_seconds() - source_time.utcoffset().total_seconds()) / 3600 } return [ TextContent( type="text", text=json.dumps(conversion_result, indent=2) ) ] except Exception as e: logger.exception(f"Error in convert_time: {str(e)}") return [ TextContent( type="text", text=f"Error converting time: {str(e)}" ) ]
- src/mcp_weather_server/server.py:93-93 (registration)Registers the ConvertTimeToolHandler instance in the tool_handlers registry during server initialization.add_tool_handler(ConvertTimeToolHandler())
- Defines the input schema and description for the 'convert_time' tool.return Tool( name=self.name, description="""Convert time from one timezone to another.""", inputSchema={ "type": "object", "properties": { "datetime_str": { "type": "string", "description": "DateTime string in ISO format (e.g., '2024-01-15T14:30:00') or 'now' for current time" }, "from_timezone": { "type": "string", "description": "Source timezone (IANA timezone name)" }, "to_timezone": { "type": "string", "description": "Target timezone (IANA timezone name)" } }, "required": ["datetime_str", "from_timezone", "to_timezone"] } )