convert_datetime_to_unix
Convert human-readable datetime to Unix timestamp for accurate time calculations. Specify date, time, and optional timezone to generate the corresponding Unix epoch value.
Instructions
Convert datetime time to unixtime (e.g. 2025-01-01 01:01:01 to 1746627290)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time | Yes | ||
| timezone | No |
Implementation Reference
- src/index.ts:78-88 (handler)The handler function that implements the core logic: parses the input datetime string using dayjs, applies the specified timezone (or guessed), computes the Unix timestamp in milliseconds using valueOf(), and returns it as a string in the tool response format.async ({ time, timezone }) => { const unixtime = dayjs(time).tz(getTZ(timezone)).valueOf(); return { content: [ { type: "text", text: String(unixtime), }, ], }; },
- src/index.ts:74-77 (schema)Input schema defined using Zod, validating 'time' as a required string (datetime) and 'timezone' as an optional string.{ time: z.string(), timezone: z.string().optional(), },
- src/index.ts:71-89 (registration)The server.tool() call that registers the 'convert_datetime_to_unix' tool, including its name, description, input schema, and handler function.server.tool( "convert_datetime_to_unix", "Convert datetime time to unixtime (e.g. 2025-01-01 01:01:01 to 1746627290)", { time: z.string(), timezone: z.string().optional(), }, async ({ time, timezone }) => { const unixtime = dayjs(time).tz(getTZ(timezone)).valueOf(); return { content: [ { type: "text", text: String(unixtime), }, ], }; }, );
- src/index.ts:12-14 (helper)Helper function getTZ used in the handler to determine the timezone: uses provided or guesses using dayjs.tz.guess().const getTZ = (timezon?: string) => { return timezon || dayjs.tz.guess();