Example #1
0
        public static Lunar SolarToLunar(Solar solar)
        {
            var lunar   = new Lunar();
            int index   = solar.solarYear - solar_1_1[0];
            int data    = (solar.solarYear << 9) | (solar.solarMonth << 5) | (solar.solarDay);
            int solar11 = 0;

            if (solar_1_1[index] > data)
            {
                index--;
            }
            solar11 = solar_1_1[index];
            int  y      = GetBitInt(solar11, 12, 9);
            int  m      = GetBitInt(solar11, 4, 5);
            int  d      = GetBitInt(solar11, 5, 0);
            long offset = SolarToInt(solar.solarYear, solar.solarMonth, solar.solarDay) - SolarToInt(y, m, d);

            int days = lunar_month_days[index];
            int leap = GetBitInt(days, 4, 13);

            int lunarY = index + solar_1_1[0];
            int lunarM = 1;
            int lunarD = 1;

            offset += 1;

            for (int i = 0; i < 13; i++)
            {
                int dm = GetBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
                if (offset > dm)
                {
                    lunarM++;
                    offset -= dm;
                }
                else
                {
                    break;
                }
            }
            lunarD           = (int)(offset);
            lunar.lunarYear  = lunarY;
            lunar.lunarMonth = lunarM;
            lunar.isleap     = false;
            if (leap != 0 && lunarM > leap)
            {
                lunar.lunarMonth = lunarM - 1;
                if (lunarM == leap + 1)
                {
                    lunar.isleap = true;
                }
            }

            lunar.lunarDay = lunarD;
            return(lunar);
        }