convert_time
Convert timestamps between timezones for accurate weather data interpretation, using ISO format or current time with IANA timezone names.
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 run_tool method that implements the core logic of the 'convert_time' tool: validates args, parses datetime (handling 'now' and ISO formats), converts timezone using astimezone, computes time difference, and returns JSON result.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)}" ) ]
- Defines the tool schema including input parameters: datetime_str (ISO or 'now'), from_timezone, to_timezone (all required).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"] } )
- src/mcp_weather_server/server.py:93-93 (registration)Registers the ConvertTimeToolHandler instance in the server's tool registry.add_tool_handler(ConvertTimeToolHandler())
- src/mcp_weather_server/server.py:35-35 (registration)Imports the ConvertTimeToolHandler class.ConvertTimeToolHandler,
- The ConvertTimeToolHandler class definition, which inherits from ToolHandler and sets the tool name to 'convert_time'.class ConvertTimeToolHandler(ToolHandler): """ Tool handler for converting time between different timezones. """ def __init__(self): super().__init__("convert_time")