get_horoscope
Calculate personalized Ziwei Doushu horoscope cycles (major and annual fortunes) using birth details, target dates, and location-based true solar time for precise Chinese astrology analysis.
Instructions
获取指定生辰在特定时间的运限信息(大限、流年等)。支持经纬度参数,将时间转换为真太阳时进行更精确的计算
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| birthday | Yes | 生日,格式: YYYY-MM-DD | |
| hour | No | 出生时辰(0-23) | |
| gender | No | 性别 | 男 |
| type | No | 日期类型 | solar |
| isLeapMonth | No | 是否闰月(仅农历有效) | |
| targetDate | No | 目标日期,格式: YYYY-MM-DD,不指定则使用当前日期 | |
| language | No | 语言设置 | zh-CN |
| longitude | No | 出生地经度(-180 到 180),用于真太阳时转换 | |
| latitude | No | 出生地纬度(-90 到 90),用于真太阳时转换 |
Implementation Reference
- ziwei_mcp_server.js:197-247 (handler)The core handler function for the 'get_horoscope' tool. It calculates the Ziwei Doushu (Purple Star Astrology) horoscope for a given birth date and time, supporting solar/lunar calendars, leap months, language, location-based true solar time correction, and a target date for the horoscope period. Uses the 'iztro' library's astro object to generate astrolabe and horoscope data.function getHoroscope(birthday, hour = 0, gender = '男', type = 'solar', isLeapMonth = false, targetDate = null, language = 'zh-CN', longitude = 116.4074, latitude = 39.9042) { try { // 如果提供了经纬度,进行真太阳时转换 let actualHour = hour; if (longitude !== undefined && latitude !== undefined) { const trueSolarTime = convertToTrueSolarTime(hour, 0, longitude, birthday); actualHour = trueSolarTime.hour; } // 将24小时制转换为12时辰序号 const timeIndex = hourToTimeIndex(actualHour); let astrolabe; if (type === 'solar') { astrolabe = astro.bySolar(birthday, timeIndex, gender, true, language); } else { astrolabe = astro.byLunar(birthday, timeIndex, gender, isLeapMonth, true, language); } if (!astrolabe) { throw new Error('无法生成星盘数据'); } // 如果没有指定目标日期,使用当前日期 const target = targetDate ? new Date(targetDate) : new Date(); const horoscope = astrolabe.horoscope(target); // 直接返回horoscope对象,添加请求参数信息 const result = { ...horoscope, requestParams: { birthday: birthday, originalHour: hour, actualHour: actualHour, timeIndex: timeIndex, gender: gender, type: type, isLeapMonth: isLeapMonth, targetDate: target.toISOString().slice(0, 10), language: language, longitude: longitude, latitude: latitude, trueSolarTimeUsed: (longitude !== undefined && latitude !== undefined) } }; return { success: true, data: result }; } catch (error) { return { success: false, error: error.message }; } }
- ziwei_mcp_server.js:362-426 (schema)The input schema and metadata (name, description) for the 'get_horoscope' tool as returned in the tools/list MCP method. Defines validation for all parameters including defaults and examples.name: "get_horoscope", description: "获取指定生辰在特定时间的运限信息(大限、流年等)。支持经纬度参数,将时间转换为真太阳时进行更精确的计算", inputSchema: { type: "object", properties: { birthday: { type: "string", description: "生日,格式: YYYY-MM-DD", examples: ["2000-08-16", "1990-12-25", "1985-06-15"] }, hour: { type: "integer", description: "出生时辰(0-23)", minimum: 0, maximum: 23, default: 0 }, gender: { type: "string", description: "性别", enum: ["男", "女"], default: "男" }, type: { type: "string", description: "日期类型", enum: ["solar", "lunar"], default: "solar" }, isLeapMonth: { type: "boolean", description: "是否闰月(仅农历有效)", default: false }, targetDate: { type: "string", description: "目标日期,格式: YYYY-MM-DD,不指定则使用当前日期", examples: ["2024-12-31", "2025-06-15"] }, language: { type: "string", description: "语言设置", enum: ["zh-CN", "zh-TW", "en-US", "ja-JP", "ko-KR", "vi-VN"], default: "zh-CN" }, longitude: { type: "number", description: "出生地经度(-180 到 180),用于真太阳时转换", minimum: -180, maximum: 180, default: 116.4074, examples: [116.4074, -74.0060, 139.6917] }, latitude: { type: "number", description: "出生地纬度(-90 到 90),用于真太阳时转换", minimum: -90, maximum: 90, default: 39.9042, examples: [39.9042, 40.7128, 35.6762] } }, required: ["birthday"] } }
- ziwei_mcp_server.js:506-549 (registration)Registration and dispatch logic in the 'tools/call' switch statement. Parses arguments from the MCP call, invokes the getHoroscope handler function, formats the response as MCP content, and handles errors.case 'get_horoscope': const hBirthday = arguments_.birthday; if (hBirthday) { try { const result = getHoroscope( hBirthday, arguments_.hour || 0, arguments_.gender || '男', arguments_.type || 'solar', arguments_.isLeapMonth || false, arguments_.targetDate || null, arguments_.language || 'zh-CN', arguments_.longitude, arguments_.latitude ); response = { jsonrpc: "2.0", id: request.id, result: { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], isError: false } }; } catch (error) { response = { jsonrpc: "2.0", id: request.id, result: { content: [{ type: "text", text: `运限计算错误: ${error.message}` }], isError: true } }; } } else { response = { jsonrpc: "2.0", id: request.id, result: { content: [{ type: "text", text: "错误: 需要提供 birthday 参数" }], isError: true } }; } break;
- ziwei_mcp_server.js:21-32 (helper)Helper function to convert 24-hour time to traditional Chinese 12 'shichen' (时辰) index (0-11), used in horoscope calculations for accurate astrolabe generation.function hourToTimeIndex(hour) { // 确保hour在0-23范围内 hour = Math.max(0, Math.min(23, Math.floor(hour))); // 时辰划分:每个时辰2小时,但子时跨越了23-1点 if (hour === 23 || hour === 0) { return 0; // 子时 } else { // 其他时辰:1-3点为丑时(1),3-5点为寅时(2),依此类推 return Math.floor((hour + 1) / 2); } }
- ziwei_mcp_server.js:42-94 (helper)Helper function to convert local clock time to true solar time based on longitude and approximate equation of time, improving accuracy for birth time in astrology calculations.function convertToTrueSolarTime(hour, minute, longitude, dateStr) { try { // 解析日期 const date = new Date(dateStr + 'T00:00:00'); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); // 计算儒略日 const a = Math.floor((14 - month) / 12); const y = year + 4800 - a; const m = month + 12 * a - 3; const julianDay = day + Math.floor((153 * m + 2) / 5) + 365 * y + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) - 32045; // 计算从J2000.0开始的儒略世纪数 const n = (julianDay - 2451545.0) / 36525; // 计算太阳的几何平均黄经(度) let L = (280.46646 + n * (36000.76983 + n * 0.0003032)) % 360; // 计算太阳的平近点角(度) let g = (357.52911 + n * (35999.05029 - n * 0.0001537)) % 360; // 计算日地距离的地球轨道偏心率 const ecc = 0.016708634 - n * (0.000042037 + n * 0.0000001267); // 计算太阳的方程时(分钟)- 简化的计算方法 // 1993年7月的方程时大约在 -5 到 +5 分钟之间 const equationOfTime = -5.3; // 1993年7月10日的精确值 // 计算经度修正(每15度经度对应1小时 = 60分钟) // 北京时间基准是东经120° const beijingLongitude = 120.0; const longitudeCorrection = (beijingLongitude - longitude) * 4; // 每经度4分钟 // 计算真太阳时(分钟) const clockTimeMinutes = hour * 60 + minute; let trueSolarTimeMinutes = clockTimeMinutes - longitudeCorrection + equationOfTime; // 规范化到0-1440分钟范围内 trueSolarTimeMinutes = ((trueSolarTimeMinutes % 1440) + 1440) % 1440; // 转换回小时和分钟 const trueHour = Math.floor(trueSolarTimeMinutes / 60); const trueMinute = Math.floor(trueSolarTimeMinutes % 60); return { hour: trueHour, minute: trueMinute }; } catch (error) { // 如果计算失败,返回原始时间 console.warn('真太阳时转换失败,使用原始时间:', error.message); return { hour: hour, minute: minute }; } }