python实现的阳历转阴历(农历)算法
本文导语: 搜索了好几个python实现的万年历多有部分时间有问题,好多是来自这个代码: 代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-'''Usage: ccal Month [4-Digit-Year] or: ccal 4-Digit-Year Month This Python script is to show Solar and Lunar calender at thesam...
搜索了好几个python实现的万年历多有部分时间有问题,好多是来自这个代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Usage: ccal Month [4-Digit-Year]
or: ccal 4-Digit-Year Month
This Python script is to show Solar and Lunar calender at the
same time. You need to have Python (2.0 or above) installed.
Acceptable date range: 1900/2 -- 2049/12
Output contains Chinese characters (mainland GB2312 encoding),
must be viewed in a Chinese-enabled system or "cxterm" etc.
programms under UNIX X-Windows.
The major reference for me to compose this program is:
lunar-2.1.tgz (1992), composed by
Fung F. Lee and
Ricky Yeung .
And Lee and Yeung refered to:
1. "Zhong1guo2 yin1yang2 ri4yue4 dui4zhao4 wan4nian2li4"
by Lin2 Qi3yuan2. 《中国阴阳日月对照万年历》.林
2. "Ming4li3 ge2xin1 zi3ping2 cui4yan2" by Xu2 Le4wu2.
《命理革新子平粹言》.徐
3. Da1zhong4 wan4nian2li4. 《大众万年历》
License:
GNU General Public License (GPL, see http://www.gnu.org).
In short, users are free to use and distribute this program
in whole. If users make revisions and distribute the revised
one, they are required to keep the revised source accessible
to the public.
Version:
0.3.2, Jan/16/2007, according to sprite's information, changed 3 codes:
1954: 0x0a5d0 --> 0x0a5b0, 1956: 0x052d0 --> 0x052b0
1916: 0x0d6a0 --> 0x056a0
0.3.1, Jan/15/2007, changed 1978's code from 0xb5a0 to 0xb6a0.
A young lady's birth day (lunar 1978/8/4) problem reported
on internet -- informed by sprite at linuxsir.org
0.3.0, Sep/25/2006, add coding line, prevent python to report warning
0.2.0, Jan/6/2002, ShengXiao(生肖), lunar leap month(闰月)
added.
0.1.0, Jan/4/2002
--- Changsen Xu
'''
#Remember, in this program:
# month=0 means Januaray, month=1 means February ...;
# day=0 means the first day of a month, day=1 means the second day,
# so as to ease manipulation of Python lists.
# year=0 is 1900, until the last step to output
daysInSolarMonth= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
lunarMonthDays = [29,30] # a short (long) lunar month has 29 (30) days */
shengXiaoEn = ["Mouse", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
shengXiaoGB = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡",
"狗", "猪"]
zhiGB = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉",
"戌", "亥"]
ganGB = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
monthEn = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November',
'December']
weekdayEn = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]
weekdayGB = ["一", "二", "三", "四", "五", "六", "日"]
numGB = ['○', "一", "二", "三", "四", "五", "六", "七", "八", "九",
"十"]
lunarHoliday = {'0_0':'春节', '4_4':'端午', '7_14':'中秋', '8_8':'重阳',
'0_14':'元宵'}
# encoding:
# b bbbbbbbbbbbb bbbb
# bit# 1 111111000000 0000
# 6 543210987654 3210
# . ............ ....
# month# 000000000111
# M 123456789012 L
#
# b_j = 1 for long month, b_j = 0 for short month
# L is the leap month of the year if 1>= 4
for iMonth in range(12):
yearInfo[iYear].monthDays[11-iMonth] = lunarMonthDays [code&0x1]
code >>= 1
if leapMonth>0:
yearInfo[iYear].leapMonth = leapMonth-1
yearInfo[iYear].monthDays.insert (leapMonth,
lunarMonthDays [code & 0x1])
def calcAllLunarYearsInfo():
for iYear in range(yearsCoded):
calcLunarDaysPerMonth (iYear)
for iMonth in range(13):
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 num10:
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 '[0;33;44m%s[0m '% lunarHoliday[tmp] +
' '*(6-len(lunarHoliday[tmp]))
elif dLunar.day==0:
tmp2 = '闰'*isLeapMonth + num2GB(dLunar.month+1) +'月'
return '[7m%s[0m' % tmp2 + ' '*(8-len(tmp2))
elif dLunar.day 2)):
nTheDate = nTheDate + 1
#–计算农历天干、地支、月、日—
nIsEnd = 0
m = 0
while nIsEnd != 1:
#if wNongliData[m+1] < 4095:
if wNongliData[m] < 4095:
k = 11
else:
k = 12
n = k
while n>=0:
nBit = wNongliData[m]
for i in range(n):
nBit = math.floor(nBit/2);
nBit = nBit % 2
if nTheDate wNongliData[m] / 65536 + 1:
wCurMonth = wCurMonth – 1
print '阳历', st["year"], st["mon"], st["day"]
print '农历', wCurYear, wCurMonth, wCurDay
#–生成农历天干、地支、属相 ==> wNongli–
szShuXiang = cShuXiang[(((wCurYear - 4) % 60) % 12) + 1]
szShuXiang = cShuXiang[(((wCurYear - 4) % 60) % 12) + 1]
zNongli = szShuXiang + '(' + cTianGan[(((wCurYear - 4) % 60) % 10)] + cDiZhi[(((wCurYear - 4) % 60) % 12)] + ')年'
#–szNongli,"%s(%s%s)年",szShuXiang,cTianGan[((wCurYear - 4) % 60) % 10],cDiZhi[((wCurYear - 4) % 60) % 12]);
#–生成农历月、日 ==> wNongliDay–*/
if wCurMonth < 1:
szNongliDay = "闰" + cMonName[(-1 * wCurMonth)]
else:
szNongliDay = cMonName[wCurMonth]
szNongliDay = szNongliDay + "月" + cDayName[wCurDay]
print szNongliDay
#return szNongli .. szNongliDay
def main():
st = {"year": 1989, "mon": 8, "day": 1}
GetDayOf(st)
st1 = {"year": 2013, "mon": 10, "day": 7}
GetDayOf(st1)
st1 = {"year": 2013, "mon": 10, "day": 1}
GetDayOf(st1)
#print("" .. GetDayOf(st))
main()
数据基本上正确了,根据自己的需要改一改程序就可以了。以后有时间在改好一点的。
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。