/// <summary> /// 按交节干支生成 html 年历 /// </summary> /// <param name="y"></param> /// <returns></returns> public string nianLi2HTML(double y) { int i, j; StringBuilder s = new StringBuilder(); // C#: 为提高字符串处理效率, 使用 StringBuilder StringBuilder s1 = new StringBuilder(); // C#: 为提高字符串处理效率, 使用 StringBuilder double v, v2, qi = 0; SSQ.calcY(LunarHelper.int2((y - 2000) * 365.2422 + 180)); for (i = 0; i < 14; i++) { if (SSQ.HS[i + 1] > SSQ.ZQ[24]) { break; //已包含下一年的冬至 } if (SSQ.leap != 0 && i == SSQ.leap) { s1.Append("闰"); } else { s1.Append("·"); } s1.Append(SSQ.ym[i]); if (s1.ToString().Length < 3) { s1.Append("月"); } s1.Append(SSQ.dx[i] > 29 ? "大" : "小"); v = SSQ.HS[i] + LunarHelper.J2000; s1.Append(" " + obb.Gan[(int)((v + 9) % 10)] + obb.Zhi[(int)((v + 1) % 12)]); s1.Append(" " + JD.setFromJD_str(v).Substring(6, 5)); for (j = -2; j < 24; j++) { if (j >= 0) { qi = SSQ.ZQ[j]; } if (j == -1) { qi = SSQ.ZQ.pe1; } if (j == -2) { qi = SSQ.ZQ.pe2; } if (qi < SSQ.HS[i] || qi >= SSQ.HS[i + 1]) { continue; } v2 = qi + LunarHelper.J2000; s1.Append(" " + obb.rmc[(int)(v2 - v)] + obb.Gan[(int)((v2 + 9) % 10)] + obb.Zhi[(int)((v2 + 1) % 12)]); s1.Append(obb.jqmc[(j + 24) % 24] + JD.setFromJD_str(qi + LunarHelper.J2000).Substring(6, 5)); } s.Append(s1.ToString() + "<br>"); s1.Remove(0, s1.Length); // C#: 在转换时将原来的字符串 s1 改写为 StringBuiler, 因此添加本句 } return(s.ToString()); }
/// <summary> /// 把太阳月亮信息形成纯文本字符串 /// </summary> /// <param name="fs">是否显示ΔT, 黄经章等信息</param> /// <returns></returns> public string toText(double fs) { StringBuilder sb = new StringBuilder(); sb.Append("平太阳 " + JD.timeStr(this.pty) + " 真太阳 " + JD.timeStr(this.zty) + "\r\n"); sb.Append("时差 " + LunarHelper.m2fm(this.sc * 86400, 2, 1) + " 月亮被照亮 " + (this.mIll * 100).ToString("F2") + "% "); sb.Append("\r\n"); sb.Append("\r\n表一 月亮 太阳\r\n"); sb.Append("视黄经 " + LunarHelper.rad2str(this.mHJ, 0) + " " + LunarHelper.rad2str(this.sHJ, 0) + "\r\n"); sb.Append("视黄纬 " + LunarHelper.rad2str(this.mHW, 0) + " " + LunarHelper.rad2str(this.sHW, 0) + "\r\n"); sb.Append("视赤经 " + LunarHelper.rad2str(this.mCJ, 1) + " " + LunarHelper.rad2str(this.sCJ, 1) + "\r\n"); sb.Append("视赤纬 " + LunarHelper.rad2str(this.mCW, 0) + " " + LunarHelper.rad2str(this.sCW, 0) + "\r\n"); sb.Append("距离 " + this.mR.ToString("F0") + "千米 " + this.sR.ToString("F6") + "AU" + "\r\n"); sb.Append("\r\n表二 月亮 太阳\r\n"); sb.Append("方位角 " + LunarHelper.rad2str(this.mPJ, 0) + " " + LunarHelper.rad2str(this.sPJ, 0) + "\r\n"); sb.Append("高度角 " + LunarHelper.rad2str(this.mPW, 0) + " " + LunarHelper.rad2str(this.sPW, 0) + "\r\n"); sb.Append("时角 " + LunarHelper.rad2str(this.mShiJ, 0) + " " + LunarHelper.rad2str(this.sShiJ, 0) + "\r\n"); sb.Append("视半径(观测点) " + LunarHelper.m2fm(this.mRad, 2, 0) + " " + LunarHelper.m2fm(this.sRad, 2, 0) + "\r\n"); if (fs != 0) { sb.Append("\r\n力学时 " + JD.setFromJD_str(this.T + LunarHelper.J2000)); sb.Append(" ΔT=" + (this.dt * 86400).ToString("F1") + "秒\r\n"); sb.Append("黄经章 " + (this.dL / LunarHelper.pi2 * 360 * 3600).ToString("F2") + "\" "); sb.Append("交角章 " + (this.dE / LunarHelper.pi2 * 360 * 3600).ToString("F2") + "\" "); sb.Append("\r\nε=" + LunarHelper.trim(LunarHelper.rad2str(this.E, 0))); } return(sb.ToString()); }
/// <summary> /// 把太阳月亮信息形成 HTML 字符串 /// </summary> /// <param name="fs">是否显示ΔT, 黄经章等信息</param> /// <returns></returns> public string toHTML(double fs) { StringBuilder sb = new StringBuilder(); sb.Append("<table width='100%' cellspacing=1 cellpadding=0 bgcolor='#FFC0C0'>"); sb.Append("<tr><td bgcolor=white align=center>"); sb.Append("平太阳 " + JD.timeStr(this.pty) + " 真太阳 <font color=red>" + JD.timeStr(this.zty) + "</font><br>"); sb.Append("时差 " + LunarHelper.m2fm(this.sc * 86400, 2, 1) + " 月亮被照亮 " + (this.mIll * 100).ToString("F2") + "% "); sb.Append("</td></tr>"); sb.Append("<tr><td bgcolor=white><center><pre style='margin-top: 0; margin-bottom: 0'><font color=blue><b>表一 月亮 太阳</b></font>\r\n"); sb.Append("视黄经 " + LunarHelper.rad2str(this.mHJ, 0) + " " + LunarHelper.rad2str(this.sHJ, 0) + "\r\n"); sb.Append("视黄纬 " + LunarHelper.rad2str(this.mHW, 0) + " " + LunarHelper.rad2str(this.sHW, 0) + "\r\n"); sb.Append("视赤经 " + LunarHelper.rad2str(this.mCJ, 1) + " " + LunarHelper.rad2str(this.sCJ, 1) + "\r\n"); sb.Append("视赤纬 " + LunarHelper.rad2str(this.mCW, 0) + " " + LunarHelper.rad2str(this.sCW, 0) + "\r\n"); sb.Append("距离 " + this.mR.ToString("F0") + "千米 " + this.sR.ToString("F6") + "AU" + "\r\n"); sb.Append("</pre></center></td></tr>"); sb.Append("<tr><td bgcolor=white><center><pre style='margin-top: 0; margin-bottom: 0'><font color=blue><b>表二 月亮 太阳</b></font>\r\n"); sb.Append("方位角 " + LunarHelper.rad2str(this.mPJ, 0) + " " + LunarHelper.rad2str(this.sPJ, 0) + "\r\n"); sb.Append("高度角 " + LunarHelper.rad2str(this.mPW, 0) + " " + LunarHelper.rad2str(this.sPW, 0) + "\r\n"); sb.Append("时角 " + LunarHelper.rad2str(this.mShiJ, 0) + " " + LunarHelper.rad2str(this.sShiJ, 0) + "\r\n"); sb.Append("视半径(观测点) " + LunarHelper.m2fm(this.mRad, 2, 0) + " " + LunarHelper.m2fm(this.sRad, 2, 0) + "\r\n"); sb.Append("</pre></center></td></tr>"); if (fs != 0) { sb.Append("<tr><td bgcolor=white align=center>"); sb.Append("力学时 " + JD.setFromJD_str(this.T + LunarHelper.J2000)); sb.Append(" ΔT=" + (this.dt * 86400).ToString("F1") + "秒<br>"); sb.Append("黄经章 " + (this.dL / LunarHelper.pi2 * 360 * 3600).ToString("F2") + "\" "); sb.Append("交角章 " + (this.dE / LunarHelper.pi2 * 360 * 3600).ToString("F2") + "\" "); sb.Append("ε=" + LunarHelper.trim(LunarHelper.rad2str(this.E, 0))); sb.Append("</td></tr>"); } sb.Append("</table>"); return(sb.ToString()); }
//public void yueLiHTML(int By, int Bm, double curJD) //{ // string sty_head = " style='font-family: 宋体; font-size: 14px; text-align: center; background-color: #E0E0FF; color: #000000; font-weight: bold' "; // string sty_body = " style='font-family: 宋体; font-size: 12px; text-align: center ' "; // string sty_date = " style='font-family: Arial Black; text-align: center;font-size: 20px' "; // string sty_date2 = " style='font-family: Arial Black; text-align: center;font-size: 20px; color: #FF0000' "; // string sty_cur = " style='background-color:#90D050' "; // int i, j; // string c, c2; // string cr = "", isM; // OB ob; // 日历物件 // this.yueLiCalc(By, Bm); // 农历计算 // // 年份处理 // c = this.nianhao + " 农历" + this.Ly + "年【" + this.ShX + "年】"; // 干支纪年 // if (c.Length > 33) c = "<span style='font-size:12px'>" + c + "</span>"; // else c = "<span style='font-size:16px;font-weight:bold'>" + c + "</span>"; // this.pg0 = c; // //月历处理 // string ta0 = "<tr>" // + "<td" + sty_head + "width='%14'>日</td>" // + "<td" + sty_head + "width='%14'>一</td>" // + "<td" + sty_head + "width='%14'>二</td>" // + "<td" + sty_head + "width='%14'>三</td>" // + "<td" + sty_head + "width='%14'>四</td>" // + "<td" + sty_head + "width='%14'>五</td>" // + "<td" + sty_head + "width='%14'>六</td><tr>"; // for (i = 0; i < this.dn; i++) // { // // 遍历本月各日(公历), 生成第 i 日的日历页面 // ob = (this.lun[i]); // if (i == 0) // { // for (j = 0; j < this.w0; j++) // 首行前面的空单元格(依据: 本月首日的星期) // cr += "<td" + sty_body + "></td>"; // } // c = ""; isM = ""; // 文字格式控制项 // if (ob.A.Length > 0) // c += "<font color=red>" + this.substr2(ob.A, 4, "..") + "</font>"; // if (c.Length <= 0 && ob.B.Length > 0) // c = "<font color=blue>" + this.substr2(ob.B, 4, "..") + "</font>"; // if (c.Length <= 0 && ob.Ldc == "初一") // c = ob.Lleap + ob.Lmc + "月" + (ob.Ldn == 30 ? "大" : "小"); // 农历历月(闰月及大小等) // if (c.Length <= 0) // c = ob.Ldc; // 取农历日名称 // if (ob.yxmc == "朔") isM = "<font color=#505000>●</font>"; // 取月相 // if (ob.yxmc == "望") isM = "<font color=#F0B000>●</font>"; // 取月相 // if (ob.yxmc == "上弦") isM = "<font color=#F0B000><b>∪</b></font>"; // if (ob.yxmc == "下弦") isM = "<font color=#F0B000><b>∩</b></font>"; // if (ob.jqmc.Length > 0) // isM += "<font color=#00C000>◆</font>"; // 定气标记 // if (ob.Fjia != 0) // c2 = sty_date2; //节日置红色 // else c2 = sty_date; // //c2 += " onmouseover='showMessD(" + i + ")'"; // C#: 注掉鼠标事件, 下同 // //c2 += " onmouseout ='showMessD(-1)'"; // c2 = "<span" + c2 + ">" + ob.d + "</span>"; //公历的日名称 // if (ob.d0 == curJD) // c2 = "<span" + sty_cur + ">" + c2 + "</span>"; // 今日标识 // //cr += "<td" + sty_body + "width='14%'>" + c2 + "<br>" + isM + c + "</td>"; // C#: 注释, 改写如下: // cr += "<td" + sty_body + "width='14%' onmouseover='changeBackcolor(this,1)' " + // "onmouseout='changeBackcolor(this,0)'>" + // c2 + "<br>" + isM + c + "</td>"; // C#: 新改写的语句, 增加鼠标事件处理 // if (i == this.dn - 1) // { // for (j = 0; j < 6 - ob.week; j++) // 末行后面的空单元格(依据: 本月末日的星期) // cr += "<td" + sty_body + "></td>"; // } // if (i == this.dn - 1 || ob.week == 6) // { // ta0 += "<tr>" + cr + "</tr>"; // cr = ""; // } // } // this.pg1 = "<table border=0 cellpadding=3 cellspacing=1 width='100%'>" + ta0 + "</table>"; // string b2 = "", b3 = "", b4 = ""; // int c__; // for (i = 0; i < this.dn; i++) // { // ob = (this.lun[i]); // c__ = i + 1; // if (c__ < 10) // c = " " + c__; // else // c = c__.ToString(); // //if(ob.Ldc =="初一") b1 += c +"日 "+ob.Lleap+ob.Lmc+"月" + (ob.Ldn==30?"大":"小")+" "; // if (ob.yxmc == "朔" || ob.yxmc == "望") b2 += c + "日 " + ob.yxsj + ob.yxmc + "月 "; // if (ob.yxmc == "上弦" || ob.yxmc == "下弦") b3 += c + "日 " + ob.yxsj + ob.yxmc + " "; // if (ob.jqmc.Length > 0) b4 += c + "日 " + ob.jqsj + ob.jqmc + " "; // } // this.pg2 = b2 + "<br>" + b3 + "<br>" + b4; // this.yueLiText(By, Bm, curJD); // C#: 转换时新增功能而增加的语句 //} #endregion 初次转换的 yueLiHTML() 方法, 因涉及大量的字符串操作, 故拟使用 StringBuiler 改写以提高效率, 但保留原代码如下 #region 原 Lunar.js 中的独立函数转换到类中 /// <summary> /// 按交节时刻生成 html 年历 /// </summary> /// <param name="y"></param> /// <returns></returns> public string nianLiHTML(double y) { int i, j; StringBuilder s = new StringBuilder(); // C#: 为提高字符串处理效率, 使用 StringBuilder StringBuilder s1 = new StringBuilder(); // C#: 为提高字符串处理效率, 使用 StringBuilder string s2; double v, qi = 0; SSQ.calcY(LunarHelper.int2((y - 2000) * 365.2422 + 180)); for (i = 0; i < 14; i++) { if (SSQ.HS[i + 1] > SSQ.ZQ[24]) { break; // 已包含下一年的冬至 } if (SSQ.leap != 0 && i == SSQ.leap) { s1.Append("闰"); } else { s1.Append("·"); } s1.Append(SSQ.ym[i]); if (s1.ToString().Length < 3) { s1.Append("月"); } s1.Append(SSQ.dx[i] > 29 ? "大" : "小"); s1.Append(" " + JD.setFromJD_str(SSQ.HS[i] + LunarHelper.J2000).Substring(6, 5)); // C#: 取实历初一的时间 v = obb.so_accurate2(SSQ.HS[i]); s2 = "(" + JD.setFromJD_str(v + LunarHelper.J2000).Substring(9, 11) + ")"; // C#: 取每月朔的时间(即初一) if (LunarHelper.int2(v + 0.5) != SSQ.HS[i]) { s2 = "<font color=red>" + s2 + "</font>"; } //v=(v+0.5+LunarHelper.J2000)%1; if(v>0.5) v=1-v; if(v<8/1440) s2 = "<u>"+s2+"</u>"; //对靠近0点的加注 s1.Append(s2); for (j = -2; j < 24; j++) { if (j >= 0) { qi = SSQ.ZQ[j]; } if (j == -1) { qi = SSQ.ZQ.pe1; } if (j == -2) { qi = SSQ.ZQ.pe2; } if (qi < SSQ.HS[i] || qi >= SSQ.HS[i + 1]) { continue; } s1.Append(" " + obb.jqmc[(j + 24) % 24] + JD.setFromJD_str(qi + LunarHelper.J2000).Substring(6, 5)); // C#: 取节气名称和实历交节日期 v = obb.qi_accurate2(qi); s2 = "(" + JD.setFromJD_str(v + LunarHelper.J2000).Substring(9, 11) + ")"; // C#: 取节气时间(上年大雪-本年大雪) if (LunarHelper.int2(v + 0.5) != qi) { s2 = "<font color=red>" + s2 + "</font>"; } //v=(v+0.5+LunarHelper.J2000)%1; if(v>0.5) v=1-v; if(v<8/1440) s2 = "<u>"+s2+"</u>"; //对靠近0点的加注 s1.Append(s2); } s.Append(s1.ToString() + "<br>"); s1.Remove(0, s1.Length); // C#: 在转换时将原来的字符串 s1 改写为 StringBuiler, 因此添加本句 } return(s.ToString()); }
/// <summary> /// 指定某日, 计算出它的所属节(气), 上一节(气), 下一节(气)信息, 并把计算结果保存在日对象中 /// </summary> /// <param name="ob"></param> /// <returns>计算成功返回 true, 否则返回 false </returns> public bool CalcJieQiInfo(OB ob, CalcJieQiType calcType) { const int JiQiInfo_Name = 0; const int JiQiInfo_JDTime = 1; const int JiQiInfo_Time = 2; const int JiQiInfo_HistoricalJDTime = 3; const int JiQiInfo_HistoricalTime = 4; const int JiQiInfo_DifferentTime = 5; const int JiQiInfo_JieOrQi = 6; const int JiQiInfo_YueJian = 7; const int JiQiInfo_DayDifference = 8; const int PrevJieQiFlag = 0; const int ThisJieQiFlag = 1; const int NextJieQiFlag = 2; double y = ob.y; int j, counter = 0, thisJie = -1, jieQiPos = 0; JieQiInfo jieqiInfo; int[] jieqiInfoPos = new int[3]; object[,] jieqiList = new object[31, 9]; double v, qi = 0; int num = SSQ.calcJieQi(LunarHelper.int2((y - 2000) * 365.2422 + 180), true); // 计算节气, 以"霜降"开始 for (j = 0; j < num; j++) // 建表 { if (calcType == CalcJieQiType.CalcJie) // 只计算节 { if (((j + 24) % 24) % 2 == 0) // 气(跳过, 只使用节) { continue; } } if (calcType == CalcJieQiType.CalcQi) // 只计算气 { if (((j + 24) % 24) % 2 == 1) // 节(跳过, 只使用气) { continue; } } qi = SSQ.ZQ[j]; v = obb.qi_accurate2(qi); jieqiList[counter, JiQiInfo_Time] = JD.setFromJD_str(v + LunarHelper.J2000); jieQiPos = (j + 24) % 24 + 20; // 与 obb.jqmc 对应, "霜降"距首位"冬至"的距离为 20 if (jieQiPos >= 24) { jieQiPos -= 24; } jieqiList[counter, JiQiInfo_JieOrQi] = true; jieqiList[counter, JiQiInfo_Name] = obb.jqmc[jieQiPos]; jieqiList[counter, JiQiInfo_YueJian] = obb.JieQiYueJian[jieQiPos]; jieqiList[counter, JiQiInfo_JDTime] = v + LunarHelper.J2000; jieqiList[counter, JiQiInfo_HistoricalTime] = JD.setFromJD_str(qi + LunarHelper.J2000); jieqiList[counter, JiQiInfo_HistoricalJDTime] = qi + LunarHelper.J2000; jieqiList[counter, JiQiInfo_DifferentTime] = (LunarHelper.int2(v + 0.5) != qi ? true : false); jieqiList[counter, JiQiInfo_DayDifference] = (int)(LunarHelper.int2(v + 0.5) - qi); counter++; } for (j = 0; j < SSQ.ZQ.Count; j++) // △重要: 由于调用了 SSQ.calcJieQi 方法, 计算了 31 个节气(超出年周期)数据, 故应清零 { SSQ.ZQ[j] = 0; } if (ob.y >= 0) { int ymd = ob.y * 10000 + ob.m * 100 + ob.d; // 转换成为数值表示的日期: 如 20090422 for (j = counter - 1; j >= 0; j--) // 逆序查表(表中的交节日期数据是由小到大排列的) { string jieqiTime = (string)jieqiList[j, JiQiInfo_Time]; int jieqiYear = int.Parse(jieqiTime.Substring(0, 5)); int jieqiMonth = int.Parse(jieqiTime.Substring(6, 2)); int jieqiDay = int.Parse(jieqiTime.Substring(9, 2)); if (jieqiYear * 10000 + jieqiMonth * 100 + jieqiDay <= ymd) // 找到所属的节气 { thisJie = j; break; } } } else { int ymd = ob.y * 10000 - (99 - ob.m) * 100 - (99 - ob.d); for (j = 0; j < counter; j++) // 顺序查表(表中的交节日期数据是由小到大排列的, 计算为数值时也要严格递增) { string jieqiTime = (string)jieqiList[j, JiQiInfo_Time]; int jieqiYear = int.Parse(jieqiTime.Substring(0, 5)); int jieqiMonth = int.Parse(jieqiTime.Substring(6, 2)); int jieqiDay = int.Parse(jieqiTime.Substring(9, 2)); int jieqiYmd = jieqiYear * 10000 - (99 - jieqiMonth) * 100 - (99 - jieqiDay); if (jieqiYmd >= ymd) { if (jieqiYmd > ymd) { thisJie = j - 1; } else { thisJie = j; } break; } } } if (thisJie > 0 && thisJie < counter) { jieqiInfoPos[ThisJieQiFlag] = thisJie; jieqiInfoPos[PrevJieQiFlag] = jieqiInfoPos[ThisJieQiFlag] - 1; jieqiInfoPos[NextJieQiFlag] = jieqiInfoPos[ThisJieQiFlag] + 1; for (j = 0; j < jieqiInfoPos.Length; j++) { switch (j) { case PrevJieQiFlag: jieqiInfo = ob.PreviousJieQi; break; case ThisJieQiFlag: jieqiInfo = ob.ThisJieQi; break; case NextJieQiFlag: jieqiInfo = ob.NextJieQi; break; default: jieqiInfo = null; break; } if (jieqiInfo != null) { jieqiInfo.JieOrQi = (bool)jieqiList[jieqiInfoPos[j], JiQiInfo_JieOrQi]; jieqiInfo.Name = (string)jieqiList[jieqiInfoPos[j], JiQiInfo_Name]; jieqiInfo.YueJian = (string)jieqiList[jieqiInfoPos[j], JiQiInfo_YueJian]; jieqiInfo.Time = (string)jieqiList[jieqiInfoPos[j], JiQiInfo_Time]; jieqiInfo.JDTime = (double)jieqiList[jieqiInfoPos[j], JiQiInfo_JDTime]; jieqiInfo.HistoricalTime = (string)jieqiList[jieqiInfoPos[j], JiQiInfo_HistoricalTime]; jieqiInfo.HistoricalJDTime = (double)jieqiList[jieqiInfoPos[j], JiQiInfo_HistoricalJDTime]; jieqiInfo.DifferentTime = (bool)jieqiList[jieqiInfoPos[j], JiQiInfo_DifferentTime]; jieqiInfo.DayDifference = (int)jieqiList[jieqiInfoPos[j], JiQiInfo_DayDifference]; } } return(true); } return(false); }