get_current_time
Retrieve the current time in a specified timezone with customizable formatting options, including UTC offset, for precise time management and synchronization.
Instructions
Get current time in specified timezone with formatting options
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | date-fns format string | |
| include_offset | No | Include UTC offset (default: true) | |
| timezone | No | IANA timezone (default: system timezone) |
Implementation Reference
- src/tools/getCurrentTime.ts:71-106 (handler)The main handler function that executes the get_current_time tool logic: validates input, resolves timezone, applies caching, formats the current time, and returns structured result.export function getCurrentTime(params: GetCurrentTimeParams): GetCurrentTimeResult { debug.timezone('getCurrentTime called with params: %O', params); // Validate format string length first if (params.format) { validateStringLength(params.format, LIMITS.MAX_FORMAT_LENGTH, 'format'); } // Resolve timezone with proper empty string handling const config = getConfig(); const timezone = resolveTimezone(params.timezone, config.defaultTimezone); const formatStr = params.format ?? "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; const includeOffset = params.include_offset !== false; // Use withCache wrapper instead of manual cache management return withCache(getCacheKey(timezone, formatStr, includeOffset), CacheTTL.CURRENT_TIME, () => { // Validate timezone if (!validateTimezone(timezone)) { throw new TimezoneError(`Invalid timezone: ${timezone}`, timezone); } const now = new Date(); try { // Format time with appropriate options const formattedTime = formatTimeWithOptions(now, timezone, params, formatStr); // Build result const result = buildTimeResult(now, formattedTime, timezone); return result; } catch (error: unknown) { handleFormatError(error, params.format ?? formatStr); } }); }
- src/index.ts:42-58 (registration)Tool registration in TOOL_DEFINITIONS: defines name 'get_current_time', description, and inputSchema for MCP tools/list.name: 'get_current_time', description: 'Get current time in specified timezone with formatting options', inputSchema: { type: 'object' as const, properties: { timezone: { type: 'string' as const, description: 'IANA timezone (default: system timezone)', }, format: { type: 'string' as const, description: 'date-fns format string' }, include_offset: { type: 'boolean' as const, description: 'Include UTC offset (default: true)', }, }, }, },
- src/index.ts:258-259 (registration)Handler mapping in TOOL_FUNCTIONS: maps tool name to the getCurrentTime function for execution during tools/call.get_current_time: (params: unknown) => getCurrentTime(params as Parameters<typeof getCurrentTime>[0]),
- src/types/index.ts:29-41 (schema)TypeScript interfaces defining input (GetCurrentTimeParams) and output (GetCurrentTimeResult) shapes for type safety in the handler.export interface GetCurrentTimeParams { timezone?: string; format?: string; include_offset?: boolean; } export interface GetCurrentTimeResult { time: string; timezone: string; offset: string; unix: number; iso: string; }
- src/tools/getCurrentTime.ts:23-39 (helper)Helper function to format the time based on parameters like include_offset and custom format.export function formatTimeWithOptions( now: Date, timezone: string, params: GetCurrentTimeParams, defaultFormat: string ): string { if (params.include_offset !== false && !params.format) { // Default format includes offset return formatInTimeZone(now, timezone, defaultFormat); } else if (params.include_offset === false && params.format) { // Custom format without offset - use the format as-is return formatInTimeZone(now, timezone, params.format); } else { // Use format string as provided (default or custom) return formatInTimeZone(now, timezone, defaultFormat); } }