/// <summary> /// 计算本月所有日期的日十二建信息 /// </summary> private void CalcRiJianThisMonth() { OB lunOb; string yuejian = String.Empty; //OB ob = new OB(); for (int i = 0; i < this.dn; i++) // 遍历月 { lunOb = this.lun[i]; //if (i == 0 || lunOb.jqmc.Trim().Length > 0 || lunOb.Ljq.Trim().Length > 0) // 每月第 1 日, 或遇到了交节气日, 则计算该日的所属节气等 //{ // ob.y = lunOb.y; // ob.m = lunOb.m; // ob.d = lunOb.d; // this.CalcJieQiInfo(ob, CalcJieQiType.CalcJie); //} //yuejian = ob.ThisJieQi.YueJian; //if (ob.ThisJieQi.DifferentTime) // yuejian = LunarHelper.SUBSTRING(lunOb.Lmonth2, 1, 1); // 调整为实历 // 可直接使用该属性的月建而无需再次计算节气, 但上述被注释的代码也可用(主要为了测试 CalcJieQiInfo 方法, 暂保留) yuejian = LunarHelper.SUBSTRING(lunOb.Lmonth2, 1, 1); lunOb.Ri12Jian = this.GetRi12Jian(yuejian, LunarHelper.SUBSTRING(lunOb.Lday2, 1, 1)); // 计算日十二建 } }
/// <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); }