csharp/6tail/lunar-csharp/lunar/eightchar/Yun.cs

Yun.cs
using com.nlf.calendar.util;
using System;

namespace com.nlf.calendar.eightchar
{
    /// 
    /// 运
    /// 
    public clast Yun
    {
        /// 
        /// 性别(1男,0女)
        /// 
        private int gender;

        /// 
        /// 起运年数
        /// 
        private int startYear;

        /// 
        /// 起运月数
        /// 
        private int startMonth;

        /// 
        /// 起运天数
        /// 
        private int startDay;

        /// 
        /// 是否顺推
        /// 
        private bool forward;

        private Lunar lunar;

        public Yun(EightChar eightChar, int gender)
        {
            this.lunar = eightChar.getLunar();
            this.gender = gender;
            bool yang = 0 == lunar.getYearGanIndexExact() % 2;
            bool man = 1 == gender;
            forward = (yang && man) || (!yang && !man);
            computeStart();
        }

        /// 
        /// 起运计算
        /// 
        private void computeStart()
        {
            JieQi prev = lunar.getPrevJie();
            JieQi next = lunar.getNextJie();
            Solar current = lunar.getSolar();
            Solar start = forward ? current : prev.getSolar();
            Solar end = forward ? next.getSolar() : current;
            int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().Substring(11, 5));
            int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().Substring(11, 5));
            // 时辰差
            int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
            DateTime endCalendar = ExactDate.fromYmd(end.getYear(), end.getMonth(), end.getDay());
            DateTime startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
            int dayDiff = endCalendar.Subtract(startCalendar).Days;
            if (hourDiff < 0)
            {
                hourDiff += 12;
                dayDiff--;
            }
            int monthDiff = hourDiff * 10 / 30;
            int month = dayDiff * 4 + monthDiff;
            int day = hourDiff * 10 - monthDiff * 30;
            int year = month / 12;
            month = month - year * 12;
            this.startYear = year;
            this.startMonth = month;
            this.startDay = day;
        }

        /// 
        /// 获取性别
        /// 
        /// 性别(1男 , 0女)
        public int getGender()
        {
            return gender;
        }

        /// 
        /// 获取起运年数
        /// 
        /// 起运年数
        public int getStartYear()
        {
            return startYear;
        }

        /// 
        /// 获取起运月数
        /// 
        /// 起运月数
        public int getStartMonth()
        {
            return startMonth;
        }

        /// 
        /// 获取起运天数
        /// 
        /// 起运天数
        public int getStartDay()
        {
            return startDay;
        }

        /// 
        /// 是否顺推
        /// 
        /// true/false
        public bool isForward()
        {
            return forward;
        }

        public Lunar getLunar()
        {
            return lunar;
        }

        /// 
        /// 获取起运的阳历日期
        /// 
        /// 阳历日期
        public Solar getStartSolar()
        {
            Solar birth = lunar.getSolar();
            DateTime c = ExactDate.fromYmd(birth.getYear(), birth.getMonth(), birth.getDay());
            c = c.AddYears(startYear);
            c = c.AddMonths(startMonth);
            c = c.AddDays(startDay);
            return Solar.fromDate(c);
        }

        /// 
        /// 获取大运
        /// 
        /// 轮数
        /// 大运
        public DaYun[] getDaYun(int n)
        {
            DaYun[] l = new DaYun[n];
            for (int i = 0; i < n; i++)
            {
                l[i] = new DaYun(this, i);
            }
            return l;
        }

        /// 
        /// 获取10轮大运
        /// 
        /// 大运
        public DaYun[] getDaYun()
        {
            return getDaYun(10);
        }

    }

}