/// <summary> /// 计算八字 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ob">日对象</param> /// <param name="type">八字类型</param> /// <param name="curTZ">时区</param> /// <param name="year">年</param> /// <param name="month">月</param> /// <param name="day">日</param> /// <param name="time">时间串</param> /// <param name="longitudeStr">经度(度分秒格式)</param> /// <returns>八字字符串</returns> public static string ML_calc <T>(OB ob, BaZiType type, double curTZ, T year, T month, T day, string time, string longitudeStr, BaZiTypeS baziTypes) { double y = LunarHelper.year2Ayear(year); if (y == -10000) { return(String.Empty); } string timeName = (type == BaZiType.ZtyBaZi ? "真太阳 " : (type == BaZiType.PtyBaZi ? "平太阳 " : "北京时间 ")); double t = LunarHelper.timeStr2hour(time); double longitude; if (type == BaZiType.EL120BaZi) { longitude = LunarHelper.str2rad("-120°"); // 解析东经120°经度为弧度 } else { longitude = LunarHelper.str2rad(longitudeStr); // 解析经度为弧度 } double jd = JD.JD__(y, LunarHelper.VAL(month.ToString()), LunarHelper.VAL(day.ToString()) + t / 24); if (type == BaZiType.ZtyBaZi) { obb.mingLiBaZi(jd + curTZ / 24 - LunarHelper.J2000, longitude, ob, baziTypes); // 八字计算, 独立于 Lunar 类 timeName += ob.bz_zty; } else { obb.mingLiBaZiNormal(jd + curTZ / 24 - LunarHelper.J2000, longitude, ob, baziTypes); // 八字计算, 独立于 Lunar 类 timeName += ob.bz_pty; } // C#: 新增的代码段 JD.setFromJD(jd); double yearAjusted = JD.Y; double monthAjusted = JD.M; double dayAjusted = JD.D; return("[日标]:" + "公历 " + yearAjusted + "-" + monthAjusted + "-" + dayAjusted + " 儒略日数 " + LunarHelper.int2(jd + 0.5) + " 距2000年首" + LunarHelper.int2(jd + 0.5 - LunarHelper.J2000) + "日" + "\r\n[八字]:" + ob.bz_jn + "年 " + ob.bz_jy + "月 " + ob.bz_jr + "日 " + ob.bz_js + "时 " + timeName + "\r\n[纪时]:" + ob.bz_JS + "\r\n[时标]:" + "23 01 03 05 07 09 11 13 15 17 19 21 23"); }
/// <summary> /// 计算公历某一个月的"公农回"三合历, 并把相关信息保存到月对象 lun, 以及日对象 lun[?] 中 /// </summary> /// <param name="By">要计算月历的年</param> /// <param name="Bm">要计算月历的月</param> public void yueLiCalc(int By, int Bm) { int i, j; double c, Bd0, Bdn; // 日历物件初始化 JD.h = 12; JD.m = 0; JD.s = 0.1; JD.Y = By; JD.M = Bm; JD.D = 1; Bd0 = LunarHelper.int2(JD.toJD()) - LunarHelper.J2000; // 公历某年的月首,中午 JD.M++; if (JD.M > 12) { JD.Y++; JD.M = 1; } // C#: 如果月份大于 12, 则年数 + 1, 月数取 1 Bdn = LunarHelper.int2(JD.toJD()) - LunarHelper.J2000 - Bd0; // 本月天数(公历) this.w0 = (Bd0 + LunarHelper.J2000 + 1) % 7; //本月第一天的星期 this.y = By; // 公历年份 this.m = Bm; // 公历月分 this.d0 = Bd0; this.dn = Bdn; // 所属公历年对应的农历干支纪年 c = By - 1984 + 9000; this.Ly = obb.Gan[(int)(c % 10)] + obb.Zhi[(int)(c % 12)]; //干支纪年 this.ShX = obb.ShX[(int)(c % 12)]; // 该年对应的生肖 this.nianhao = obb.getNH(By); double D, w; OB ob, ob2; // 循环提取各日信息 for (i = 0, j = 0; i < Bdn; i++) { ob = (this.lun[i]); ob.d0 = Bd0 + i; // 儒略日,北京时12:00 ob.di = i; // 公历月内日序数 ob.y = By; // 公历年 ob.m = Bm; // 公历月 ob.dn = Bdn; // 公历月天数 ob.week0 = this.w0; // 月首的星期 ob.week = (this.w0 + i) % 7; // 当前日的星期 ob.weeki = LunarHelper.int2((this.w0 + i) / 7); // 本日所在的周序号 ob.weekN = LunarHelper.int2((this.w0 + Bdn - 1) / 7) + 1; // 本月的总周数 JD.setFromJD(ob.d0 + LunarHelper.J2000); ob.d = (int)JD.D; //公历日名称 // 农历月历 if (SSQ.ZQ.Count == 0 || ob.d0 < SSQ.ZQ[0] || ob.d0 >= SSQ.ZQ[24]) // 如果d0已在计算农历范围内则不再计算 { SSQ.calcY(ob.d0); } int mk = (int)LunarHelper.int2((ob.d0 - SSQ.HS[0]) / 30); if (mk < 13 && SSQ.HS[mk + 1] <= ob.d0) { mk++; // 农历所在月的序数 } ob.Ldi = (int)(ob.d0 - SSQ.HS[mk]); // 距农历月首的编移量,0对应初一 ob.Ldc = obb.rmc[(int)(ob.Ldi)]; // 农历日名称 ob.cur_dz = ob.d0 - SSQ.ZQ[0]; // 距冬至的天数 ob.cur_xz = ob.d0 - SSQ.ZQ[12]; // 距夏至的天数 ob.cur_lq = ob.d0 - SSQ.ZQ[15]; // 距立秋的天数 ob.cur_mz = ob.d0 - SSQ.ZQ[11]; // 距芒种的天数 ob.cur_xs = ob.d0 - SSQ.ZQ[13]; // 距小暑的天数 if (ob.d0 == SSQ.HS[mk] || ob.d0 == Bd0) { // 月的信息 ob.Lmc = SSQ.ym[mk]; // 月名称 ob.Ldn = SSQ.dx[mk]; // 月大小 ob.Lleap = (SSQ.leap != 0 && SSQ.leap == mk) ? "闰" : ""; // 闰状况 ob.Lmc2 = mk < 13 ? SSQ.ym[mk + 1] : "未知"; // 下个月名称,判断除夕时要用到 } else { ob2 = (this.lun[i - 1]); ob.Lmc = ob2.Lmc; ob.Ldn = ob2.Ldn; ob.Lleap = ob2.Lleap; ob.Lmc2 = ob2.Lmc2; } int qk = (int)LunarHelper.int2((ob.d0 - SSQ.ZQ[0] - 7) / 15.2184); if (qk < 23 && ob.d0 >= SSQ.ZQ[qk + 1]) { qk++; //节气的取值范围是0-23 } if (ob.d0 == SSQ.ZQ[qk]) { ob.Ljq = obb.jqmc[qk]; } else { ob.Ljq = ""; } ob.yxmc = ob.yxjd = ob.yxsj = ""; // 月相名称,月相时刻(儒略日),月相时间串 ob.jqmc = ob.jqjd = ob.jqsj = ""; // 定气名称,节气时刻(儒略日),节气时间串 // 干支纪年处理 // 以立春为界定年首 D = SSQ.ZQ[3] + (ob.d0 < SSQ.ZQ[3] ? -365 : 0) + 365.25 * 16 - 35; //以立春为界定纪年 ob.Lyear = Math.Floor(D / 365.2422 + 0.5); //农历纪年(10进制,1984年起算) // 以下几行以正月初一定年首 D = SSQ.HS[2]; // 一般第3个月为春节 for (j = 0; j < 14; j++) { // 找春节 if (SSQ.ym[j] != "正") { continue; } D = SSQ.HS[j]; if (ob.d0 < D) { D -= 365; break; } // 无需再找下一个正月 } D = D + 5810; // 计算该年春节与1984年平均春节(立春附近)相差天数估计 ob.Lyear0 = Math.Floor(D / 365.2422 + 0.5); // 农历纪年(10进制,1984年起算) D = ob.Lyear + 9000; ob.Lyear2 = obb.Gan[(int)(D % 10)] + obb.Zhi[(int)(D % 12)]; // 干支纪年(立春) D = ob.Lyear0 + 9000; ob.Lyear3 = obb.Gan[(int)(D % 10)] + obb.Zhi[(int)(D % 12)]; // 干支纪年(正月) ob.Lyear4 = ob.Lyear0 + 1984 + 2698; // 黄帝纪年 // 纪月处理,1998年12月7(大雪)开始连续进行节气计数,0为甲子 mk = (int)LunarHelper.int2((ob.d0 - SSQ.ZQ[0]) / 30.43685); if (mk < 12 && ob.d0 >= SSQ.ZQ[2 * mk + 1]) { mk++; //相对大雪的月数计算,mk的取值范围0-12 } D = mk + LunarHelper.int2((SSQ.ZQ[12] + 390) / 365.2422) * 12 + 900000; //相对于1998年12月7(大雪)的月数,900000为正数基数 ob.Lmonth = D % 12; ob.Lmonth2 = obb.Gan[(int)(D % 10)] + obb.Zhi[(int)(D % 12)]; // 纪日,2000年1月7日起算 D = ob.d0 - 6 + 9000000; ob.Lday2 = obb.Gan[(int)(D % 10)] + obb.Zhi[(int)(D % 12)]; // 星座 mk = (int)LunarHelper.int2((ob.d0 - SSQ.ZQ[0] - 15) / 30.43685); if (mk < 11 && ob.d0 >= SSQ.ZQ[2 * mk + 2]) { mk++; //星座所在月的序数,(如果j=13,ob.d0不会超过第14号中气) } ob.XiZ = obb.XiZ[(int)((mk + 12) % 12)] + "座"; // 回历 oba.getHuiLi(ob.d0, ob); // 节日 ob.A = ob.B = ob.C = ""; ob.Fjia = 0; oba.getDayName(ob, ob); //公历 obb.getDayName(ob, ob); //农历 } // 以下是月相与节气的处理 double d, jd2 = Bd0 + JD.deltatT2(Bd0) - 8d / 24d; int xn; // 月相查找 w = XL.MS_aLon(jd2 / 36525, 10, 3); w = LunarHelper.int2((w - 0.78) / Math.PI * 2) * Math.PI / 2; do { d = obb.so_accurate(w); D = LunarHelper.int2(d + 0.5); xn = (int)LunarHelper.int2(w / LunarHelper.pi2 * 4 + 4000000.01) % 4; w += LunarHelper.pi2 / 4; if (D >= Bd0 + Bdn) { break; } if (D < Bd0) { continue; } ob = (this.lun[(int)(D - Bd0)]); ob.yxmc = obb.yxmc[xn]; // 取得月相名称 ob.yxjd = d.ToString(); ob.yxsj = JD.timeStr(d); } while (D + 5 < Bd0 + Bdn); // 节气查找 w = XL.S_aLon(jd2 / 36525, 3); w = LunarHelper.int2((w - 0.13) / LunarHelper.pi2 * 24) * LunarHelper.pi2 / 24; do { d = obb.qi_accurate(w); D = LunarHelper.int2(d + 0.5); xn = (int)LunarHelper.int2(w / LunarHelper.pi2 * 24 + 24000006.01) % 24; w += LunarHelper.pi2 / 24; if (D >= Bd0 + Bdn) { break; } if (D < Bd0) { continue; } ob = (this.lun[(int)(D - Bd0)]); ob.jqmc = obb.jqmc[xn]; // 取得节气名称 ob.jqjd = d.ToString(); ob.jqsj = JD.timeStr(d); } while (D + 12 < Bd0 + Bdn); // C#: 转换时新增的代码行 this.CalcRiJianThisMonth(); // 计算本月所有日期的日十二建信息 }
/// <summary> /// 儒略日数转公历, 并且返回日期转化的时间串 /// </summary> /// <param name="jd"></param> /// <returns></returns> public static string setFromJD_str(double jd) { JD.setFromJD(jd); return(JD.toStr()); }