get-current-time
Retrieve the current time in your local timezone. This tool provides datetime information for AI agents and chat interfaces.
Instructions
Get the current time in the configured local timezone
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:19-28 (registration)Registration of the 'get-current-time' tool with an inline asynchronous handler function that retrieves the current timezone and formats the current time in that timezone using helper functions.server.tool( "get-current-time", "Get the current time in the configured local timezone", async () => ({ content: [{ type: "text", text: `The current time is ${getCurrentTimeInTimezone(getCurrentTimezone())}` }] }) );
- src/timezone-utils.ts:66-74 (helper)Helper function that returns the current system timezone by querying Intl.DateTimeFormat, with fallback to 'UTC' on error. Called by the get-current-time handler.export function getCurrentTimezone(): string { try { const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; return processTimezone(timezone); } catch (error) { console.error("Error getting current timezone:", error); return "UTC"; // Default to UTC if there's an error } }
- src/timezone-utils.ts:96-147 (helper)Helper function that formats the current date and time in ISO8601 format for the specified timezone using Intl APIs. Called by the get-current-time handler.export function getCurrentTimeInTimezone(timezone: string): string { try { const date = new Date(); // Create a formatter that includes the timezone const options: Intl.DateTimeFormatOptions = { timeZone: timezone, timeZoneName: 'short' }; // Get the timezone offset from the formatter const formatter = new Intl.DateTimeFormat('en-US', options); const formattedDate = formatter.format(date); const timezonePart = formattedDate.split(' ').pop() || ''; // Format the date in ISO8601 format with the timezone // First get the date in the specified timezone const tzFormatter = new Intl.DateTimeFormat('en-US', { timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false, fractionalSecondDigits: 3 }); const parts = tzFormatter.formatToParts(date); const dateParts: Record<string, string> = {}; parts.forEach(part => { if (part.type !== 'literal') { dateParts[part.type] = part.value; } }); // Format as YYYY-MM-DDTHH:MM:SS.sss±HH:MM (ISO8601) const isoDate = `${dateParts.year}-${dateParts.month}-${dateParts.day}T${dateParts.hour}:${dateParts.minute}:${dateParts.second}.${dateParts.fractionalSecond || '000'}`; // For proper ISO8601, we need to add the timezone offset // We can use the Intl.DateTimeFormat to get the timezone offset const tzOffset = new Date().toLocaleString('en-US', { timeZone: timezone, timeZoneName: 'longOffset' }).split(' ').pop() || ''; // Format the final ISO8601 string return `${isoDate}${tzOffset.replace('GMT', '')}`; } catch (error) { console.error(`Error formatting time for timezone ${timezone}:`, error); return 'Invalid timezone'; } }