Beispiel #1
0
        /**
         * 干支纪月计算
         */
        private void computeMonth()
        {
            Solar start = null;
            Solar end;
            //干偏移值(以立春当天起算)
            int gOffset = ((yearGanIndexByLiChun % 5 + 1) * 2) % 10;
            //干偏移值(以立春交接时刻起算)
            int gOffsetExact = ((yearGanIndexExact % 5 + 1) * 2) % 10;

            //序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
            int index = -2;

            foreach (string jie in LunarUtil.JIE)
            {
                end = jieQi[jie];
                string ymd  = solar.toYmd();
                string symd = null == start ? ymd : start.toYmd();
                string eymd = end.toYmd();
                if (ymd.CompareTo(symd) >= 0 && ymd.CompareTo(eymd) < 0)
                {
                    break;
                }
                start = end;
                index++;
            }
            if (index < 0)
            {
                index += 12;
            }

            monthGanIndex = (index + gOffset) % 10;
            monthZhiIndex = (index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;

            //序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
            int indexExact = -2;

            foreach (string jie in LunarUtil.JIE)
            {
                end = jieQi[jie];
                string time  = solar.toYmdhms();
                string stime = null == start ? time : start.toYmdhms();
                string etime = end.toYmdhms();
                if (time.CompareTo(stime) >= 0 && time.CompareTo(etime) < 0)
                {
                    break;
                }
                start = end;
                indexExact++;
            }
            if (indexExact < 0)
            {
                indexExact += 12;
            }
            monthGanIndexExact = (indexExact + gOffsetExact) % 10;
            monthZhiIndexExact = (indexExact + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
        }
Beispiel #2
0
        /// <summary>
        /// 计算节气表(冬至的太阳黄经是-90度或270度)
        /// </summary>
        private void computeJieQi()
        {
            //儒略日,冬至在阳历上一年,所以这里多减1年以从去年开始
            double jd = 365.2422 * (solar.getYear() - 2001);

            for (int i = 0, j = JIE_QI.Length; i < j; i++)
            {
                double t = calJieQi(jd + i * 15.2, i * 15 - 90) + Solar.J2000 + 8D / 24;
                jieQi.Add(JIE_QI[i], Solar.fromJulianDay(t));
            }
        }
Beispiel #3
0
        /// <summary>
        /// 获取本周的阳历日期列表(可能跨月)
        /// </summary>
        /// <returns>本周的阳历日期列表</returns>
        public List <Solar> getDays()
        {
            Solar        firstDay = getFirstDay();
            List <Solar> l        = new List <Solar>();

            l.Add(firstDay);
            for (int i = 1; i < 7; i++)
            {
                l.Add(firstDay.next(i));
            }
            return(l);
        }
Beispiel #4
0
 /// <summary>
 /// 获取气
 /// </summary>
 /// <returns>气</returns>
 public string getQi()
 {
     foreach (string qi in LunarUtil.QI)
     {
         Solar d = jieQi[qi];
         if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay())
         {
             return(qi);
         }
     }
     return("");
 }
Beispiel #5
0
 /// <summary>
 /// 获取节
 /// </summary>
 /// <returns>节</returns>
 public string getJie()
 {
     foreach (string jie in LunarUtil.JIE)
     {
         Solar d = jieQi[jie];
         if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay())
         {
             return(jie);
         }
     }
     return("");
 }
Beispiel #6
0
 /// <summary>
 /// 通过农历年月日时初始化
 /// </summary>
 /// <param name="lunarYear">年(农历)</param>
 /// <param name="lunarMonth">月(农历),1到12,闰月为负,即闰2月=-2</param>
 /// <param name="lunarDay">日(农历),1到31</param>
 /// <param name="hour">小时(阳历)</param>
 /// <param name="minute">分钟(阳历)</param>
 /// <param name="second">秒钟(阳历)</param>
 public Lunar(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second)
 {
     this.year   = lunarYear;
     this.month  = lunarMonth;
     this.day    = lunarDay;
     this.hour   = hour;
     this.minute = minute;
     this.second = second;
     dayOffset   = LunarUtil.computeAddDays(year, month, day);
     this.solar  = toSolar();
     compute();
 }
Beispiel #7
0
        /// <summary>
        /// 计算干支纪年
        /// </summary>
        private void computeYear()
        {
            yearGanIndex = (year + LunarUtil.BASE_YEAR_GANZHI_INDEX) % 10;
            yearZhiIndex = (year + LunarUtil.BASE_YEAR_GANZHI_INDEX) % 12;

            //以立春作为新一年的开始的干支纪年
            int g = yearGanIndex;
            int z = yearZhiIndex;

            //精确的干支纪年,以立春交接时刻为准
            int gExact = yearGanIndex;
            int zExact = yearZhiIndex;

            if (year == solar.getYear())
            {
                //获取立春的阳历时刻
                Solar liChun = jieQi["立春"];
                //立春日期判断
                if (solar.toYmd().CompareTo(liChun.toYmd()) < 0)
                {
                    g--;
                    if (g < 0)
                    {
                        g += 10;
                    }
                    z--;
                    if (z < 0)
                    {
                        z += 12;
                    }
                }
                //立春交接时刻判断
                if (solar.toYmdhms().CompareTo(liChun.toYmdhms()) < 0)
                {
                    gExact--;
                    if (gExact < 0)
                    {
                        gExact += 10;
                    }
                    zExact--;
                    if (zExact < 0)
                    {
                        zExact += 12;
                    }
                }
            }
            yearGanIndexByLiChun = g;
            yearZhiIndexByLiChun = z;

            yearGanIndexExact = gExact;
            yearZhiIndexExact = zExact;
        }
Beispiel #8
0
        /// <summary>
        /// 获取本月的阳历日期列表
        /// </summary>
        /// <returns>阳历日期列表</returns>
        public List <Solar> getDays()
        {
            List <Solar> l = new List <Solar>(31);
            Solar        d = new Solar(year, month, 1);

            l.Add(d);
            int days = SolarUtil.getDaysOfMonth(year, month);

            for (int i = 1; i < days; i++)
            {
                l.Add(d.next(i));
            }
            return(l);
        }
Beispiel #9
0
 /// <summary>
 /// 周推移
 /// </summary>
 /// <param name="weeks">推移的周数,负数为倒推</param>
 /// <param name="separateMonth">是否按月单独计算</param>
 /// <returns>推移后的阳历周</returns>
 public SolarWeek next(int weeks, bool separateMonth)
 {
     if (0 == weeks)
     {
         return(new SolarWeek(year, month, day, start));
     }
     if (separateMonth)
     {
         int       n    = weeks;
         DateTime  c    = new DateTime(year, month, day);
         SolarWeek week = new SolarWeek(c, start);
         int       m    = this.month;
         bool      plus = n > 0;
         while (0 != n)
         {
             c    = c.AddDays(plus ? 7 : -7);
             week = new SolarWeek(c, start);
             int weekMonth = week.getMonth();
             if (m != weekMonth)
             {
                 int index = week.getIndex();
                 if (plus)
                 {
                     if (1 == index)
                     {
                         Solar firstDay = week.getFirstDay();
                         week      = new SolarWeek(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start);
                         weekMonth = week.getMonth();
                     }
                     else
                     {
                         c    = new DateTime(week.getYear(), week.getMonth(), 1);
                         week = new SolarWeek(c, start);
                     }
                 }
                 else
                 {
                     int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start);
                     if (size == index)
                     {
                         Solar firstDay = week.getFirstDay();
                         Solar lastDay  = firstDay.next(6);
                         week      = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start);
                         weekMonth = week.getMonth();
                     }
                     else
                     {
                         c    = new DateTime(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth()));
                         week = new SolarWeek(c, start);
                     }
                 }
                 m = weekMonth;
             }
             n -= plus ? 1 : -1;
         }
         return(week);
     }
     else
     {
         DateTime c = new DateTime(year, month, day);
         c = c.AddDays(weeks * 7);
         return(new SolarWeek(c, start));
     }
 }
Beispiel #10
0
 /// <summary>
 /// 设置阳历日期
 /// </summary>
 /// <param name="solar">阳历日期</param>
 public void setSolar(Solar solar)
 {
     this.solar = solar;
 }
Beispiel #11
0
 public JieQi(string name, Solar solar)
 {
     setName(name);
     this.solar = solar;
 }
Beispiel #12
0
        /// <summary>
        /// 通过阳历日期初始化
        /// </summary>
        /// <param name="date">阳历日期</param>
        public Lunar(DateTime date)
        {
            solar = new Solar(date);
            int y = solar.getYear();
            int m = solar.getMonth();
            int d = solar.getDay();
            int startYear, startMonth, startDay;
            int lunarYear, lunarMonth, lunarDay;

            if (y < 2000)
            {
                startYear  = SolarUtil.BASE_YEAR;
                startMonth = SolarUtil.BASE_MONTH;
                startDay   = SolarUtil.BASE_DAY;
                lunarYear  = LunarUtil.BASE_YEAR;
                lunarMonth = LunarUtil.BASE_MONTH;
                lunarDay   = LunarUtil.BASE_DAY;
            }
            else
            {
                startYear  = SolarUtil.BASE_YEAR + 99;
                startMonth = 1;
                startDay   = 1;
                lunarYear  = LunarUtil.BASE_YEAR + 99;
                lunarMonth = 11;
                lunarDay   = 25;
            }
            int diff = 0;

            for (int i = startYear; i < y; i++)
            {
                diff += 365;
                if (SolarUtil.isLeapYear(i))
                {
                    diff += 1;
                }
            }
            for (int i = startMonth; i < m; i++)
            {
                diff += SolarUtil.getDaysOfMonth(y, i);
            }
            diff     += d - startDay;
            lunarDay += diff;
            int lastDate = LunarUtil.getDaysOfMonth(lunarYear, lunarMonth);

            while (lunarDay > lastDate)
            {
                lunarDay  -= lastDate;
                lunarMonth = LunarUtil.nextMonth(lunarYear, lunarMonth);
                if (lunarMonth == 1)
                {
                    lunarYear++;
                }
                lastDate = LunarUtil.getDaysOfMonth(lunarYear, lunarMonth);
            }
            year      = lunarYear;
            month     = lunarMonth;
            day       = lunarDay;
            hour      = solar.getHour();
            minute    = solar.getMinute();
            second    = solar.getSecond();
            dayOffset = LunarUtil.computeAddDays(year, month, day);
            compute();
        }
Beispiel #13
0
        /// <summary>
        /// 通过八字获取阳历列表
        /// </summary>
        /// <param name="yearGanZhi">年柱</param>
        /// <param name="monthGanZhi">月柱</param>
        /// <param name="dayGanZhi">日柱</param>
        /// <param name="timeGanZhi">时柱</param>
        /// <returns>符合的阳历列表</returns>
        public static List <Solar> fromBaZi(string yearGanZhi, string monthGanZhi, string dayGanZhi, string timeGanZhi)
        {
            List <Solar> l          = new List <Solar>();
            Solar        today      = new Solar();
            Lunar        lunar      = today.getLunar();
            int          offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact()) - LunarUtil.getJiaZiIndex(yearGanZhi);

            if (offsetYear < 0)
            {
                offsetYear = offsetYear + 60;
            }
            int    startYear = today.getYear() - offsetYear;
            int    hour      = 0;
            string timeZhi   = timeGanZhi.Substring(1);

            for (int i = 0, j = LunarUtil.ZHI.Length; i < j; i++)
            {
                if (LunarUtil.ZHI[i].Equals(timeZhi))
                {
                    hour = (i - 1) * 2;
                }
            }
            while (startYear >= SolarUtil.BASE_YEAR - 1)
            {
                int  year    = startYear - 1;
                int  counter = 0;
                int  month   = 12;
                int  day;
                bool found = false;
                while (counter < 15)
                {
                    if (year >= SolarUtil.BASE_YEAR)
                    {
                        day = 1;
                        if (year == SolarUtil.BASE_YEAR && month == SolarUtil.BASE_MONTH)
                        {
                            day = SolarUtil.BASE_DAY;
                        }
                        Solar solar = Solar.fromYmdHms(year, month, day, hour, 0, 0);
                        lunar = solar.getLunar();
                        if (lunar.getYearInGanZhiExact().Equals(yearGanZhi) && lunar.getMonthInGanZhiExact().Equals(monthGanZhi))
                        {
                            found = true;
                            break;
                        }
                    }
                    month++;
                    if (month > 12)
                    {
                        month = 1;
                        year++;
                    }
                    counter++;
                }
                if (found)
                {
                    counter = 0;
                    month--;
                    if (month < 1)
                    {
                        month = 12;
                        year--;
                    }
                    day = 1;
                    if (year == SolarUtil.BASE_YEAR && month == SolarUtil.BASE_MONTH)
                    {
                        day = SolarUtil.BASE_DAY;
                    }
                    Solar solar = Solar.fromYmdHms(year, month, day, hour, 0, 0);
                    while (counter < 61)
                    {
                        lunar = solar.getLunar();
                        if (lunar.getYearInGanZhiExact().Equals(yearGanZhi) && lunar.getMonthInGanZhiExact().Equals(monthGanZhi) && lunar.getDayInGanZhiExact().Equals(dayGanZhi) && lunar.getTimeInGanZhi().Equals(timeGanZhi))
                        {
                            l.Add(solar);
                            break;
                        }
                        solar = solar.next(1);
                        counter++;
                    }
                }
                startYear -= 60;
            }
            return(l);
        }