get_current_time
Retrieve the current date and time in your local timezone using system-detected settings. Ideal for applications needing accurate time data without manual configuration.
Instructions
Get the current date and time in your local timezone
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.js:143-181 (handler)The handler for the 'get_current_time' tool. It gets the current date/time using Intl.DateTimeFormat in the detected local timezone (TIMEZONE), with fallback, and returns formatted time, ISO, Unix timestamp, and UTC offset as JSON.case 'get_current_time': { const now = new Date(); let localTime; try { localTime = 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, weekday: 'long', timeZoneName: 'long' }).format(now); } catch (e) { // Fallback if timezone is invalid localTime = now.toString(); } const isoString = now.toISOString(); return { content: [ { type: 'text', text: JSON.stringify({ current_time: localTime, timezone: TIMEZONE, iso_time: isoString, unix_timestamp: Math.floor(now.getTime() / 1000), utc_offset: new Date().getTimezoneOffset() }, null, 2), }, ], }; }
- server.js:118-125 (registration)Registration of the 'get_current_time' tool in the ListTools response, specifying name, description (no input parameters).{ name: 'get_current_time', description: 'Get the current date and time in your local timezone', inputSchema: { type: 'object', properties: {}, }, },
- server.js:121-124 (schema)Input schema for 'get_current_time': an empty object (no parameters).inputSchema: { type: 'object', properties: {}, },
- server.js:13-34 (helper)Helper function detectTimezone() used to initialize the TIMEZONE variable for the handler.function detectTimezone() { // Try environment variable first if (process.env.TZ) { return process.env.TZ; } // Try reading /etc/timezone (Linux) try { const tz = readFileSync('/etc/timezone', 'utf8').trim(); if (tz) return tz; } catch (e) {} // Try reading /etc/localtime symlink (Linux/Mac) try { const result = execSync('readlink /etc/localtime', { encoding: 'utf8' }).trim(); const match = result.match(/zoneinfo\/(.+)$/); if (match) return match[1]; } catch (e) {} // Default fallback return 'UTC'; }
- server.js:92-99 (helper)Async initializer that sets TIMEZONE, potentially overriding from location detection.(async () => { LOCATION = await detectLocation(); if (LOCATION.timezone && LOCATION.timezone !== 'Unknown') { TIMEZONE = LOCATION.timezone; } console.error(`Detected timezone: ${TIMEZONE}`); console.error(`Detected location: ${LOCATION.city}, ${LOCATION.province}, ${LOCATION.country}`); })();