convert_time
Convert date and time between timezones using IANA timezone names. Input source and target timezones with a 24-hour format time to get the converted result. Enhance time-aware applications with precise conversions.
Instructions
Convert time between timezones.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceTimezone | Yes | The source timezone. IANA timezone name, e.g. Asia/Shanghai | |
| targetTimezone | Yes | The target timezone. IANA timezone name, e.g. Europe/London | |
| time | Yes | Date and time in 24-hour format. e.g. 2025-03-23 12:30:00 |
Implementation Reference
- src/index.ts:109-124 (handler)Main handler for the 'convert_time' tool call within the CallToolRequestSchema. Validates arguments, invokes the conversion logic, and formats the response.case 'convert_time': { if (!checkConvertTimeArgs(args)) { throw new Error(`Invalid arguments for tool: [${name}]`); } const { sourceTimezone, targetTimezone, time } = args; const { sourceTime, targetTime, timeDiff } = convertTime(sourceTimezone, targetTimezone, time); return { success: true, content: [ { type: 'text', text: `Current time in ${sourceTimezone} is ${sourceTime}, and the time in ${targetTimezone} is ${targetTime}. The time difference is ${timeDiff} hours.`, }, ], }; }
- src/index.ts:192-201 (handler)Core handler function that performs the actual time conversion between source and target timezones using dayjs library.function convertTime(sourceTimezone: string, targetTimezone: string, time?: string) { const sourceTime = time ? dayjs(time).tz(sourceTimezone) : dayjs().tz(sourceTimezone); const targetTime = sourceTime.tz(targetTimezone); const formatString = 'YYYY-MM-DD HH:mm:ss'; return { sourceTime: sourceTime.format(formatString), targetTime: targetTime.format(formatString), timeDiff: dayjs(targetTime).diff(dayjs(sourceTime), 'hours'), }; }
- src/tools.ts:78-99 (schema)Schema definition for the 'convert_time' tool, including input properties and requirements.export const CONVERT_TIME: Tool = { name: 'convert_time', description: 'Convert time between timezones.', inputSchema: { type: 'object', properties: { sourceTimezone: { type: 'string', description: 'The source timezone. IANA timezone name, e.g. Asia/Shanghai', }, targetTimezone: { type: 'string', description: 'The target timezone. IANA timezone name, e.g. Europe/London', }, time: { type: 'string', description: 'Date and time in 24-hour format. e.g. 2025-03-23 12:30:00', }, }, required: ['sourceTimezone', 'targetTimezone', 'time'], }, };
- src/index.ts:30-34 (registration)Registers all tools including 'convert_time' by including CONVERT_TIME in the response to ListToolsRequest.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [CURRENT_TIME, RELATIVE_TIME, DAYS_IN_MONTH, GET_TIMESTAMP, CONVERT_TIME, GET_WEEK_YEAR], }; });
- src/index.ts:241-252 (helper)Helper function to validate the input arguments for the 'convert_time' tool against the expected schema.function checkConvertTimeArgs(args: unknown): args is { sourceTimezone: string, targetTimezone: string, time: string } { return ( typeof args === 'object' && args !== null && 'sourceTimezone' in args && typeof args.sourceTimezone === 'string' && 'targetTimezone' in args && typeof args.targetTimezone === 'string' && 'time' in args && typeof args.time === 'string' ); }