/// <summary> /// 从 Xml 对象中读取农历节日的定义 /// </summary> /// <returns></returns> private static xList <OB> getLunarFeasts() { const string xPath = "SharpSxwnl/SxwnlData/Data[@Id = 'obb_getDayName']"; xList <OB> result = new xList <OB>(); if (LunarHelper.SxwnlXmlData != null) { XmlNodeList foundNodeList = LunarHelper.SxwnlXmlData.SelectNodes(xPath); if (foundNodeList.Count > 0) { for (int i = 0; i < foundNodeList.Count; i++) { for (int j = 0; j < foundNodeList[i].ChildNodes.Count; j++) { result.Add(new OB()); // 添加日对象来记录节点信息 XmlAttributeCollection xmlAttr = foundNodeList[i].ChildNodes[j].Attributes; result[result.Count - 1].Lmc = xmlAttr.GetNamedItem("Day").InnerText; result[result.Count - 1].A = xmlAttr.GetNamedItem("A").InnerText; result[result.Count - 1].B = xmlAttr.GetNamedItem("B").InnerText; result[result.Count - 1].C = xmlAttr.GetNamedItem("C").InnerText; result[result.Count - 1].Fjia = LunarHelper.VAL(xmlAttr.GetNamedItem("Fjia").InnerText) == 0 ? 0 : 1; } } } } return(result); }
/// <summary> /// 将度分秒转换为弧度值(只作简单转化, 要求传递的格式严格遵守"度分秒"的格式, 如: 0°0'31.49" /// </summary> /// <param name="d"></param> /// <returns></returns> public static double str2rad(string d) { double result = 0; string strSpliter = "°'\""; char[] spliters = strSpliter.ToCharArray(); string[] strD = d.Split(spliters, StringSplitOptions.RemoveEmptyEntries); if (strD.Length > 0) { double a = 0, b = 0, c = 0; a = LunarHelper.VAL(strD[0]) / 180 * Math.PI; // 1°= 1/180*PI ≈ 0.017453292519943 弧度 if (strD.Length > 1) { b = LunarHelper.VAL(strD[1]) / 60 / 180 * Math.PI; // 1' = (1/60)°≈ 0.000290888208666 弧度 if (strD.Length > 2) { c = LunarHelper.VAL(strD[2]) / 60 / 180 / 60 * Math.PI; // 1" = (1/60)' ≈ 0.000004848136811 弧度 } } if (a > 0) { result = a + b + c; } else { result = a - b - c; } } return(result); }
/// <summary> /// 传入普通纪年或天文纪年,传回天文纪年 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="c">普通纪年或天文纪年, 泛型, 支持数值或字符串</param> /// <returns></returns> public static int year2Ayear <T>(T c) { int y; Regex regexToReplace = new Regex(@"[^0-9Bb\*-]"); // C#: 匹配字符: 数字0-9, B, b, *, - string strC = regexToReplace.Replace(c.ToString(), ""); // C#: 去除无效字符 string q = strC.Substring(0, 1); if (q == "B" || q == "b" || q == "*") //通用纪年法(公元前) { y = 1 - LunarHelper.VAL(strC.Substring(1), 1); if (y > 0) { MessageBox.Show("通用纪法的公元前纪法从 B.C.1 年开始,并且没有公元 0 年!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(-10000); } } else { y = LunarHelper.VAL(strC, 1); } if (y < -4712) { MessageBox.Show("不得小于 B.C.4713 年!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(-10000); } if (y > 9999) { MessageBox.Show("超过9999年的农历计算很不准。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(y); }
/// <summary> /// 传入天文纪年,传回显示用的常规纪年 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="y">天文纪年, 泛型, 支持数值或字符串</param> /// <returns></returns> public static string Ayear2year <T>(T y) { int result = LunarHelper.VAL(y.ToString(), 1); if (result <= 0) { return("B" + (-result + 1)); } return(result.ToString()); }
/// <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> /// 时间串转为小时 /// </summary> /// <param name="s">时间串</param> /// <returns></returns> public static double timeStr2hour(string s) { Regex regexToReplace = new Regex(@"[^0-9:]"); // C#: 匹配字符: 数字0-9, : int a, b, c; string[] timeStr = regexToReplace.Replace(s, "").Split(':'); // C#: 去除无效字符后, 按 : 分隔字符串 for (int i = 0; i < timeStr.Length; i++) // C#: 即使参数 s 为空串, 也会产生一个数组元素 { if (timeStr[i].Length == 0) // C#: 把空串设置为 "0" { timeStr[i] = "0"; } } switch (timeStr.Length) { case 1: { // C#: 为避免 Substring 方法超出范围取子串引发异常, 改用本类中的静态方法 SUBSTRING a = LunarHelper.VAL(LunarHelper.SUBSTRING(timeStr[0], 0, 2), 1); b = LunarHelper.VAL(LunarHelper.SUBSTRING(timeStr[0], 2, 2), 1); c = LunarHelper.VAL(LunarHelper.SUBSTRING(timeStr[0], 4, 2), 1); break; } case 2: { a = LunarHelper.VAL(timeStr[0], 1); b = LunarHelper.VAL(timeStr[1], 1); c = 0; break; } default: { a = LunarHelper.VAL(timeStr[0], 1); b = LunarHelper.VAL(timeStr[1], 1); c = LunarHelper.VAL(timeStr[2], 1); break; } } return(a + b / 60d + c / 3600d); }
public static long VAL(string strExpression, long getIntegerFlag) { return((long)LunarHelper.VAL(strExpression)); }
public static int VAL(string strExpression, int getIntegerFlag) { return((int)LunarHelper.VAL(strExpression)); }