python实现的阳历转阴历(农历)算法

2019-10-06 17:15:58王冬梅

     yearInfo[iYear].yearDays += yearInfo[iYear].monthDays[iMonth]

 


#input dateSolar, return (dateLunar, isLunarMonthOrNot)
def solar2Lunar(d): #d is a Date class
    dLunar = Date(-1, -1, -1) #unknown lunar Date class

    offset = solarDaysFromFirstDate(d)

    dLunar.weekday  = (offset + solar1st.weekday)%7

    for iYear in range(yearsCoded):
        if offset < yearInfo[iYear].yearDays:
            dLunar.year = iYear; break
 offset -= yearInfo[iYear].yearDays
    if dLunar.year == -1:   error ("Date out of range.")

    dLunar.gan      = (dLunar.year + lunar1st.gan) % 10
    dLunar.zhi      = (dLunar.year + lunar1st.zhi) % 12

    for iMonth in range(13):
        if offset< yearInfo[dLunar.year].monthDays[iMonth]:
            dLunar.month = iMonth; break
 offset -= yearInfo[dLunar.year].monthDays[iMonth]

    dLunar.day = offset

    isLeapMonth=0
    if yearInfo[dLunar.year].leapMonth >=0:
        if dLunar.month ==  yearInfo[iYear].leapMonth + 1:
            isLeapMonth=1
        if dLunar.month > yearInfo[dLunar.year].leapMonth:
            dLunar.month -= 1

    return (dLunar, isLeapMonth)

 

def getSolarDaysInMonth (year, month):
    if isSolarLeapYear(year) and month==1:
            return 29
    else:   return daysInSolarMonth[month]


def num2GB (num):
    if num==10:
        return '十'
    elif num>10 and num<20:
        return '十' + numGB[num-10]

    tmp=''
    while num>10:
        tmp = numGB[num%10] + tmp
        num = int(num/10)
    tmp = numGB[num] + tmp
    return tmp


def lunarDate2GB (dLunar, isLeapMonth):
    tmp = str(dLunar.month)+'_'+str(dLunar.day)
    if lunarHoliday.has_key( tmp ):
        return '%s  '% lunarHoliday[tmp] +
               ' '*(6-len(lunarHoliday[tmp]))