//农历转公历 public static bool ToSolar(Lunar_t lunar, ref int y, ref int m, ref int d) { int year = lunar.Year, month = lunar.Month, day = lunar.Day; int byNow, xMonth, i; int spring_solar_m = 0; int spring_solar_d = 0; if (!LunarDateCheck(lunar)) { return(false); } LunarSpringDate(year, ref spring_solar_m, ref spring_solar_d); byNow = spring_solar_d - 1; if (spring_solar_m == 2) { byNow += 31; } for (i = 1; i < month; i++) { byNow += LunarMonthDays(year, i); } byNow += day; xMonth = LunarLeapMonth(year); if (xMonth != 0) { if (month > xMonth || (month == xMonth && lunar.Is_leap)) { byNow += LunarLeapMonthDays(year); } } if (byNow > 366 || (year % 4 != 0 && byNow == 365)) { year += 1; if (year % 4 == 0) { byNow -= 366; } else { byNow -= 365; } } for (i = 1; i < 13; i++) //这行代码可能有错 { if (MonthTotal[i] >= byNow) { month = i; break; } } d = byNow - MonthTotal[month - 1]; m = month; y = year; return(true); }
//判断给定的农历日期是否是,在MIN_YEAR年到MAX_YEAR年之间正确的日期 private static bool LunarDateCheck(Lunar_t lunar) { int leap = 0; if ((lunar.Year < MIN_YEAR) || (lunar.Year > MAX_YEAR)) { return(false); } if ((lunar.Month < 1) || (lunar.Month > 12)) { return(false); } if ((lunar.Day < 1) || (lunar.Day > 30)) //农历的月最多有30天 { return(false); } if (LunarMonthDays(lunar.Year, lunar.Month) < lunar.Day) { return(false); } if (lunar.Is_leap) { leap = LunarLeapMonth(lunar.Year); if (leap == 0) { return(false); } else if (lunar.Month != leap) { return(false); } else if (LunarLeapMonthDays(lunar.Year) < lunar.Day) { return(false); } } return(true); }
//公历转农历 public static bool ToLunar(int year, int month, int day, ref Lunar_t lunar) { int bySpring, bySolar, daysPerMonth; int index = 0; bool flag; int spring_solar_m = 0; int spring_solar_d = 0; if (!SolarDateCheck(year, month, day)) { return(false); } //bySpring 记录春节离当年元旦的天数。 //bySolar 记录阳历日离当年元旦的天数。 LunarSpringDate(year, ref spring_solar_m, ref spring_solar_d); if (spring_solar_m == 1) { bySpring = spring_solar_d - 1; } else { bySpring = spring_solar_d - 1 + 31; } bySolar = MonthTotal[month - 1] + day - 1; if ((year % 4 == 0) && (month > 2)) { bySolar++; } //daysPerMonth记录大小月的天数 29 或30 //index 记录从哪个月开始来计算。 //flag 是用来对闰月的特殊处理。 //判断阳历日在春节前还是春节后 if (bySolar >= bySpring) //阳历日在春节后(含春节那天) { bySolar -= bySpring; month = 1; flag = false; daysPerMonth = LunarMonthDays(year, month); while (bySolar >= daysPerMonth) { bySolar -= daysPerMonth; if (month == LunarLeapMonth(year)) { flag = !flag; if (!flag) { month++; } } else { month++; } index++; daysPerMonth = LunarMonthDays(year, index); } day = bySolar + 1; } else { //阳历日在春节前 bySpring -= bySolar; year--; month = 12; if (LunarLeapMonth(year) == 0) { index = 12; } else { index = 13; } flag = false; daysPerMonth = LunarMonthDays(year, index); while (bySpring > daysPerMonth) { bySpring -= daysPerMonth; index--; if (!flag) { month--; } if (month == LunarLeapMonth(year)) { flag = !flag; } daysPerMonth = LunarMonthDays(year, index); } day = daysPerMonth - bySpring + 1; } lunar.Day = day; lunar.Month = month; lunar.Year = year; if (month == LunarLeapMonth(year)) { lunar.Is_leap = true; } else { lunar.Is_leap = false; } return(true); }