Beispiel #1
0
        private static 忌日 Calc月破(LnDate date)
        {
            var 月 = new GanZhi(date.MonthGZ);
            var 日 = new GanZhi(date.DayGZ);

            return(Math.Abs(月.Zhi.Index - 日.Zhi.Index) == 6 ? LnBase.忌日.月破 : 忌日.百无禁忌);
        }
Beispiel #2
0
        private BaZiList <GanZhi> InitBaZiFromDateTime(DateTime date, bool 确定时辰)
        {
            this.datetime = new LnDate(date);
            this.time     = 确定时辰 ? date.TimeOfDay : TimeSpan.Zero;

            GanZhi 年 = new GanZhi(this.datetime.YearGZ);
            GanZhi 月 = new GanZhi(this.datetime.MonthGZ);
            GanZhi 日 = new GanZhi(this.datetime.DayGZ);

            LnDate 明天 = this.datetime.Add(1);

            if (date.Hour >= 23)
            {
                日 = 日.Add(1);
                年 = new GanZhi(明天.YearGZ);
                月 = new GanZhi(明天.MonthGZ);
            }

            GanZhi 时 = GanZhi.Zero;

            if (确定时辰)
            {
                Zhi 时支 = Zhi.Get((int)((date.Hour + 1) / 2) % 12);
                时 = 日.Gan.起月时(时支, 柱位.时);
            }

            return(BaZiList.Create(年, 月, 日, 时));
        }
Beispiel #3
0
        private static 忌日 Calc岁破(LnDate date)
        {
            var 年 = new GanZhi(date.YearGZ);
            var 日 = new GanZhi(date.DayGZ);

            return(Math.Abs(年.Zhi.Index - 日.Zhi.Index) == 6 ? LnBase.忌日.岁破 : 忌日.百无禁忌);
        }
Beispiel #4
0
        public static HuangLi 黄历日(LnDate date)
        {
            HuangLi huanli = new HuangLi();

            huanli.忌日 |= LnBase.Calc岁破(date);
            huanli.忌日 |= LnBase.Calc月破(date);
            huanli.忌日 |= LnBase.Calc上朔(date);
            huanli.忌日 |= LnBase.Calc杨公忌日(date);

            GanZhi yue = new GanZhi(date.MonthGZ);
            GanZhi ri  = new GanZhi(date.DayGZ);

            huanli.建除 = JianChu.Get(yue.Zhi, ri.Zhi);
            return(huanli);
        }
Beispiel #5
0
        /// <summary>
        /// 计算年份下标差值
        /// </summary>
        /// <param name="year">要查找的年干支</param>
        /// <param name="startYear">开始的年份</param>
        /// <param name="forward">方向</param>
        /// <returns></returns>
        private static int CalcYearDiff(GanZhi year, int startYear, 方向 forward)
        {
            LnDate lndate   = new LnDate(startYear, 2, 10);
            int    yearDiff = year.Index - (new GanZhi(lndate.YearGZ)).Index;

            if (forward == 方向.逆行)
            {
                yearDiff = yearDiff < 0 ? yearDiff : (yearDiff - 60);
            }
            else
            {
                yearDiff = yearDiff < 0 ? (yearDiff + 60) : yearDiff;
            }

            return(yearDiff);
        }
Beispiel #6
0
        public static DateTime 查找农历(int year, string yue, string day, bool leap = false)
        {
            LnDate lndate = new LnDate(year, 1, 1);

            while (lndate.Year == year)
            {
                if (lndate.MonthNL == yue && lndate.DayNL == day && string.IsNullOrEmpty(lndate.Leap) != leap)
                {
                    return(lndate.datetime);
                }

                lndate = lndate.Add(1);
            }

            throw new Exception("找不到结果!");
        }
Beispiel #7
0
        public static TimeSpan 计算节气时间差(DateTime date, 方向 f)
        {
            LnDate   节气日  = LnBase.查找节气(date.Year, date.Month);
            DateTime 具体时间 = 节气日.datetime + 节气日.JieQiTime;

            if (f == 方向.顺行 && date > 具体时间)
            {
                节气日 = LnBase.查找节气(date.Year, date.Month + 1);
            }
            else if (f == 方向.逆行 && date < 具体时间)
            {
                节气日 = LnBase.查找节气(date.Year, date.Month - 1);
            }

            具体时间 = 节气日.datetime + 节气日.JieQiTime;
            return(具体时间 - date);
        }
Beispiel #8
0
        /// <summary>
        /// 用八字寻找公历时间。
        /// </summary>
        /// <param name="year">年干支</param>
        /// <param name="month">月干支</param>
        /// <param name="day">日干支</param>
        /// <param name="startYear">开始时间</param>
        /// <param name="forward">方向: -1 往以前日子, 1 往后面的日子</param>
        /// <returns></returns>
        public static DateTime 查找八字(string year, string month, string day, int startYear, 方向 forward)
        {
            // 看年月是否匹配
            GanZhi n = new GanZhi(year);
            GanZhi m = new GanZhi(month);

            if (n.Gan.起月时(m.Zhi, 柱位.月) != m)
            {
                throw new ArgumentException($"'{year}'年不存在'{month}'月。");
            }

            // 开始运算
            int yearDiff   = CalcYearDiff(new GanZhi(year), startYear, forward);
            int monthIndex = m.Zhi.Index == 0 ? 12 : m.Zhi.Index;

            startYear += m.Zhi.Index == 1 ? 1 : 0;

            // 上下搜索600年
            for (int periode = 0; periode < 10; periode++)
            {
                int    f      = forward == 方向.顺行 ? 1 : -1;
                LnDate lndate = new LnDate(startYear + yearDiff + f * 60 * periode, monthIndex, 1);
                while (lndate.JieQiTime == TimeSpan.Zero)
                {
                    lndate = lndate.Add(1);
                }

                if (lndate.YearGZ != year || lndate.MonthGZ != month)
                {
                    throw new Exception("计算思路有错误!");
                }

                while (lndate.MonthGZ == month)
                {
                    if (lndate.DayGZ == day)
                    {
                        return(lndate.datetime);
                    }

                    lndate = lndate.Add(1);
                }
            }

            throw new Exception("六百年内找不到结果!");
        }
Beispiel #9
0
        private IEnumerable <ShiYun> 起流年(DateTime start, DateTime end)
        {
            List <ShiYun> res = new List <ShiYun>();

            for (int i = 0; i <= 10; i++)
            {
                LnDate d  = new LnDate(start.AddYears(i));
                ShiYun ln = new ShiYun(new GanZhi(d.YearGZ), ShiYun.YunType.流年, this.time.Bazi);

                LnDate 立春 = LnBase.查找节气(start.AddYears(i).Year, 2);
                ln.Start = 立春.datetime + 立春.JieQiTime;
                ln.End   = ((DateTime)ln.Start).AddYears(1);

                res.Add(ln);

                // 超过时限,退出。主要是为起运前的流年考虑的。其它都是十年期。
                if (((DateTime)ln.End).Year > end.Year)
                {
                    break;
                }
            }

            return(res);
        }
Beispiel #10
0
        private static 忌日 Calc杨公忌日(LnDate date)
        {
            string flag = date.MonthNL + "月" + date.DayNL;

            return(LnBase.杨公Def.FirstOrDefault(y => y == flag) != null ? LnBase.忌日.杨公十三忌 : 忌日.百无禁忌);
        }
Beispiel #11
0
        /// <summary>
        /// 甲年癸亥日,乙年己巳日。。。。。。
        /// var arr = ["癸亥", "己巳", "乙亥", "辛巳", "丁亥", "癸巳", "己亥", "乙巳", "辛亥", "丁巳"];
        /// </summary>
        /// <param name="date"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static 忌日 Calc上朔(LnDate date)
        {
            var 年 = new GanZhi(date.YearGZ);

            return(LnBase.朔Def[年.Gan.Index] == date.DayGZ ? LnBase.忌日.朔 : 忌日.百无禁忌);
        }