get_datetime
Retrieve current date and time with an optional timezone parameter, defaulting to 'Asia/Shanghai'. Ideal for synchronizing timestamps in weather forecasts or applications requiring precise time data.
Instructions
获取当前的日期和时间。可以提供一个可选的时区参数,默认为 'Asia/Shanghai'。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timezone | No | 可选的时区,例如 'America/New_York', 'Europe/London'。如果未提供,默认为 'Asia/Shanghai'。 |
Implementation Reference
- src/index.ts:324-344 (handler)Handler implementation for the 'get_datetime' tool. Parses input arguments using the schema, retrieves current date/time using JavaScript Date object adjusted for the specified timezone (defaults to 'Asia/Shanghai'), handles invalid timezone errors with fallback, and returns a formatted string response.} else if (name === "get_datetime") { const { timezone } = getDatetimeSchema.parse(args); const currentDateTime = new Date(); let dateTimeString: string; const targetTimezone = timezone || 'Asia/Shanghai'; // Default to Asia/Shanghai try { // Use toLocaleString with options to get time in the target timezone dateTimeString = currentDateTime.toLocaleString('zh-CN', { timeZone: targetTimezone }); } catch (error) { // Fallback to a known good default if specific timezone fails logger.warn(`无效的时区 "${targetTimezone}" 或格式化错误:`, error); const fallbackTimezone = 'Asia/Shanghai'; dateTimeString = currentDateTime.toLocaleString('zh-CN', { timeZone: fallbackTimezone }); dateTimeString += ` (无法识别提供的时区 "${timezone}", 已回退到 ${fallbackTimezone})`; } return { content: [{ type: "text", text: `当前日期时间 (${targetTimezone}): ${dateTimeString}` }], }; }
- src/index.ts:173-175 (schema)Zod schema definition for the 'get_datetime' tool input: optional 'timezone' string parameter with description.const getDatetimeSchema = z.object({ timezone: z.string().optional().describe("可选的时区,例如 'America/New_York', 'Europe/London'。如果未提供,默认为 'Asia/Shanghai'。"), });
- src/index.ts:183-186 (registration)Registration of the 'get_datetime' tool within the 'tools' object, specifying its description and referencing the schema.get_datetime: { description: "获取当前的日期和时间。可以提供一个可选的时区参数(如 'America/New_York', 'Europe/London'),默认为 'Asia/Shanghai'。", schema: getDatetimeSchema, },