c#实现阳历转干支历

using CheckIn.ToolsHost.Dtos;
using Lunar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheckIn.ToolsHost.Infrastructures
{
    public class ChineseEraConverter
    {
        static string[] TianGans = new string[] { "癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬" };
        static string[] DiZhis = new string[] { "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌" };
        static string[] MonthDiZhis = new string[] { "","寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子", "丑" };
        static string[] HourDiZhis = new string[] {    "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        /// <summary>
        /// 年转干支年
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static TianganDizhiResult GetYearEra(DateTime date)
        {
            /*
             假定农历年份为y,天干所对应的数为a,地支所对应的数为b,i=y mod 60
①   if    0≤i≤3
         i=i-3+60
         a=i mod 10,b=i mod 12

              if    4≤i≤12
         i=i-3
         a=i,b=i


   if    i>12
       i=i-3
          if  i=10
            a=0,b=10
          else if  i=11
             a=1,b=11
          else, a=i mod 10,b=i mod 12(即i>11的情况)
             */



            // 阴历
            var solar = new Solar(date.Year, date.Month, date.Day);
            var lunar = solar.Lunar;
            var year = lunar.Year;

            var i = year % 60;
            int a, b;
            if (i <= 3)
            {
                i = i - 3 + 60;
                a = i % 10;
                b = i % 12;
            }
            else if (i >= 4 && i <= 12)
            {
                i = i - 3;
                a = i;
                b = i;
            }
            else
            {
                i = i - 3;
                if (i == 10)
                {
                    a = 0;
                    b = 10;
                }
                else if (i == 11)
                {
                    a = 1;
                    b = 11;
                }
                else
                {
                    a = i % 10;
                    b = i % 12;
                }
            }

            return new TianganDizhiResult
            {
                TianGan = a,
                DiZhi = b,
                TianGanString = TianGans[a],
                DiZhiString = DiZhis[b]
            };
        }

        public static TianganDizhiResult GetMonthEra(DateTime birthday)
        {
            /*
              假定,年份天干所对应的数为i,月份天干所对应的数为j,月份为m
        那么,j=2i+m
       ① if  j<10
              j=j
          例如:天干为乙的年份所对应的3月份的干支?
                天干为乙,所对应的数i=2,
                那么,此年3月天干所对应的数j=2×2+3=7
                所以,此年3月的天干为庚,3月地支为辰
                故,天干为乙的年份所对应的3月干支为庚辰。
        ② if  j≥10,
j=j mod 10
           例如:天干为庚的年份所对应的6月干支?
                 天干为庚,所对应的数i=7,
                 那么,此年6月天干所对应的数j=2×7+6=20,此时,j≥10,
                 那么,j=20 mod 10=0,
                 所以,此年6月的天干为癸,6月的地支为未。
                 故,天干为庚的年份所对应的6月干支为癸未。
             */
            var yearRet = GetYearEra(birthday);
            var j = 2 * yearRet.TianGan + birthday.Month;
            int a;
            if(j < 10)
            {
                a = j;
            }
            else
            {
                a = j % 10;
            }

            var ret = new TianganDizhiResult { 
            TianGan = a,  
            TianGanString = TianGans[a],
            DiZhi = GetMonthDiZhi(birthday)
            };
            ret.DiZhiString = MonthDiZhis[ret.DiZhi];

            return ret;
        }

        /// <summary>
        /// 获取月份的地支
        /// </summary>
        /// <param name="birthday">生日</param>
        /// <returns></returns>
        static int GetMonthDiZhi(DateTime birthday)
        {
            /*
             十二地支与月份的对应关系‌:
‌子月‌:对应农历十二月,即阳历1月1日~1月5日。
‌丑月‌:对应农历十一月,即阳历1月6日~1月31日
‌丑月‌:对应农历十一月,即阳历2月1日~2月3日。
‌寅月‌:对应农历正月,即阳历2月4日~最后。
寅月‌:对应农历正月,即阳历3月1日~3月5日。
‌卯月‌:对应农历二月,即阳历3月6日~最后
卯月‌:对应农历二月,即阳历4月1日~4月4日。
‌辰月‌:对应农历三月,即阳历4月5日~-------。
辰月‌:对应农历三月,即阳历5月1日~5月4日。
‌巳月‌:对应农历四月,即阳历5月5日~----------
巳月‌:对应农历四月,即阳历6月1日~6月4日。
‌午月‌:对应农历五月,即阳历6月5日~-------------
午月‌:对应农历五月,即阳历7月1日~7月6日。
‌未月‌:对应农历六月,即阳历7月7日~------------
未月‌:对应农历六月,即阳历8月1日~8月6日。
‌申月‌:对应农历七月,即阳历8月7日~-------------
‌申月‌:对应农历七月,即阳历9月1日~9月7日。
‌酉月‌:对应农历八月,即阳历9月8日~---------------
‌酉月‌:对应农历八月,即阳历10月1日~10月7日。
‌戌月‌:对应农历九月,即阳历10月8日~------------
‌戌月‌:对应农历九月,即阳历11月1日~11月6日。
‌亥月‌:对应农历十月,即阳历11月7日~-------------------
‌亥月‌:对应农历十月,即阳历12月1日~12月6日。
‌子月‌:对应农历十月,即阳历12月7日~12月31日。
             */
            // 1"寅",2 "卯", 3"辰", 4"巳", 5"午", 6"未", 7"申", 8"酉", 9"戌", 10"亥", 11"子", 12"丑" };

            switch (birthday.Month)
            {
                case 1:
                    if (birthday.Day <= 5)
                        return 11;
                    else
                        return 12;
                case 2:
                    if (birthday.Day <= 3)
                        return 12;
                    else
                        return 1;
                case 3:
                    if (birthday.Day <= 5)
                        return 1;
                    else
                        return 2;
                case 4:
                    if (birthday.Day <= 4)
                        return 2;
                    else
                        return 3;
                case 5:
                    if (birthday.Day <= 4)
                        return 3;
                    else
                        return 4;
                case 6:
                    if (birthday.Day <= 4)
                        return 4;
                    else
                        return 5;
                case 7:
                    if (birthday.Day <= 6)
                        return 5;
                    else
                        return 6;
                case 8:
                    if (birthday.Day <= 6)
                        return 6;
                    else
                        return 7;
                case 9:
                    if (birthday.Day <= 7)
                        return 7;
                    else
                        return 8;
                case 10:
                    if (birthday.Day <= 7)
                        return 8;
                    else
                        return 9;
                case 11:
                    if (birthday.Day <= 6)
                        return 9;
                    else
                        return 10;
                case 12:
                    if (birthday.Day <= 6)
                        return 10;
                    else
                        return 11;
            }

            return 0;
        }

        public static TianganDizhiResult GetDayEra(DateTime date)
        {
            /*
             C =世纪数-1;Y =年份后两位;M =阳历月份;D=阳历日期;
i=0(奇数月); i=6(偶数月)
             */
            var year = date.Year;
            var M = date.Month;
            if (M <= 2)
                M += 12;
            year--;
            var D = date.Day;
            var yearStr = year.ToString();
            var C = Convert.ToInt32(yearStr.Substring(0, 2));
            var Y = Convert.ToInt32(yearStr.Substring(2));

            var i = 0;
            if (M % 2 == 0)
                i = 6;

            var G = 4*C + (C / 4) + 5*Y + (Y / 4) + (3 * (M + 1) / 5) + D - 3;
            var Z = G + 4*C + 10 + i;
            TianganDizhiResult ret = new TianganDizhiResult();
            ret.TianGan = G % 10;
            ret.DiZhi = Z % 12;
            ret.TianGanString = TianGans[ret.TianGan];
            ret.DiZhiString = DiZhis[ret.DiZhi];

            return ret;
        }

        public static TianganDizhiResult GetHourEra(DateTime date)
        {
            var dayEra = GetDayEra(date);
            /*
             23:00-01:00 — 子;       01:00-03:00 —丑;       03:00-05:00 —寅;      
05:00-07:00 —卯 ;       07:00-09:00 —辰;       09:00-11:00 —巳;
11:00-13:00 —午;        13:00-15:00 —未;       15:00-17:00 —申;
17:00-19:00 —酉;        19:00-21:00 —戌;       21:00-23:00 —亥;
             */
            int b = 0;
            switch (date.Hour)
            {
                case 0:
                case 23:
                    b = 0;
                    break;
                case 1:
                case 2:
                    b = 1;
                    break;
                case 3:
                case 4:
                    b = 2;
                    break;
                case 5:
                case 6:
                    b = 3;
                    break;
                case 7:
                case 8:
                    b = 4;
                    break;
                case 9:
                case 10:
                    b = 5;
                    break;
                case 11:
                case 12:
                    b = 6;
                    break;
                case 13:
                case 14:
                    b = 7;
                    break;
                case 15:
                case 16:
                    b = 8;
                    break;
                case 17:
                case 18:
                    b = 9;
                    break;
                case 19:
                case 20:
                    b = 10;
                    break;
                case 21:
                case 22:
                    b = 11;
                    break;
            }

            TianganDizhiResult ret= new TianganDizhiResult();
            ret.DiZhi = b;
            ret.DiZhiString = HourDiZhis[b];

            var j = 2*dayEra.TianGan + (b + 1) - 2;
            if(j < 10)
            {
                ret.TianGan = j;
            }
            else
            {
                ret.TianGan = j%10;
              
            }
            ret.TianGanString = TianGans[j];
            return ret;
        }
 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值