Search for several Python implementations of the perpetual calendar there are some time problems, much is from this code:
Copy the Code code as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
'''
Usage:ccal Month [4-digit-year]
Or:ccal 4-digit-year Month
This Python script was to show Solar and Lunar calender at the
Same time. You need to having Python (2.0 or above) installed.
Acceptable date RANGE:1900/2--2049/12
Output contains Chinese characters (mainland GB2312 encoding),
Must is viewed in a chinese-enabled system or "cxterm" etc.
Programms under UNIX x-windows.
The major reference for me and 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. "China Yin and Yang Sun and moon Calendar". Forest
2. "Ming4li3 ge2xin1 zi3ping2 cui4yan2" by Xu2 Le4wu2.
"The Life of Innovation son Pingjian". Xu
3. Da1zhong4 Wan4nian2li4. "The Volkswagen Perpetual calendar"
License:
GNU general public License (GPL, see http://www.gnu.org).
In short, users is free to use and distribute the This program
In whole. If users make revisions and distribute the revised
One, they is 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 the Internet--informed by sprites at linuxsir.org
0.3.0, sep/25/2006, add coding line, prevent Python to report warning
0.2.0, jan/6/2002, Shengxiao (zodiac), Lunar Leap Month (leap month)
Added.
0.1.0, jan/4/2002
---changsen Xu
'''
#Remember, in the 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 have (+) days */
Shengxiaoen = ["Mouse", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
SHENGXIAOGB = ["Rat", "ox", "Tiger", "Rabbit", "dragon", "Snake", "horse", "sheep", "monkey", "Chicken",
"Dog", "pig")
ZHIGB = ["Zi", "ugly", "Yin", "Mao", "Chen", "WU", "noon", "not", "Shen", "unitary",
"Xu", "Hai"]
GANGB = ["A", "B", "C", "Ding", "e", "own", "Geng", "Xin", "Ren", "GUI"]
Monthen = [' January ', ' February ', ' March ', ' April ', ' may ', ' June ',
' July ', ' August ', ' September ', ' October ', ' November ',
' December ']
Weekdayen = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]
WEEKDAYGB = ["One", "two", "three", "four", "Five", "VI", "Day")
NUMGB = [' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine ',
"Ten"]
Lunarholiday = {' 0_0 ': ' Spring Festival ', ' 4_4 ': ' Dragon Boat Festival ', ' 7_14 ': ' Mid-Autumn Festival ', ' 8_8 ': ' Chongyang ',
' 0_14 ': ' Yuanxiao '}
# 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 If 1<=l<=12; NO Leap month If L = 0.
# The Leap month (if exists) is a long one if M = 1.
Yearcode = [
0x04bd8, # 1900
0X04AE0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, # 1905
0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, # 1910
0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, # 1915
0x056a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, # 1920
0x0a4b0, 0x0b4b5, 0x06a50, 0X06D40, 0x1ab54, # 1925
0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, # 1930
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, # 1935
0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, # 1940
0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, # 1945
0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0X06CA0, # 1950
0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, # 1955
0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, # 1960
0X0AB50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, # 1965
0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, # 1970
0X04DD5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, # 1975
0x0d558, 0x0b540, 0x0b6a0, 0x195a6, 0x095b0, # 1980
0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, # 1985
0X06D40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, # 1990
0x04970, 0x064b0, 0X074A3, 0x0ea50, 0x06b58, # 1995
0X055C0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, # 2000
0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, # 2005
0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, # 2010
0x0b4a0, 0X0BAA4, 0X0AD50, 0x055d9, 0x04ba0, # 2015
0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, # 2020
0X06AA0, 0X0AD50, 0x05b52, 0x04b60, 0x0a6e6, # 2025
0X0A4E0, 0x0d260, 0x0ea65, 0x0d530, 0X05AA0, # 2030
0X076A3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, # 2035
0x1d0b6, 0x0d250, 0x0d520, 0X0DD45, 0x0b5a0, # 2040
0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, # 2045
0x0aa50, 0x1b255, 0X06D20, 0x0ada0 # 2049
]
yearscoded = Len (yearcode)
From sys import argv, exit, stdout
From time import time, localtime
Ow=stdout.write
Class Lunaryearinfo:
def __init__ (self):
Self.yeardays = 0
Self.monthdays = [0]*13
Self.leapmonth = 1 #-1 means no lunar leap month
Yearinfo = [0]*yearscoded #global variable
For I in Range (yearscoded):
Yearinfo[i] = Lunaryearinfo ()
Class Date:
def __init__ (self, year, month, day, Weekday=-1, Gan=-1, zhi=-1):
Self.year =year
Self.month =month
Self.day =day
Self.weekday=weekday
Self.gan =gan
Self.zhi =zhi
solar1st = Date (0, 0, weekday=2) #Wednesday, January 31, 1900
lunar1st = Date (0, 0, 0, weekday=2, gan=6, zhi=0)
#Wednesday, first day, first month, 1900, Geng Zi year
def error (msg):
print ' Error: ', msg; Exit (0)
Def issolarleapyear (year):
year=year+1900
return (year%4 = = 0) and (year%100! = 0) or (year%400 = = 0)
baseyear=1201-1900
# In fact, Real baseyear=1201. In order to ease calculation of
# leap years. Real baseyear must conform to:
# Realbaseyear%4==1 and Realbaseyear%400==1.
# Assert Realbaseyear < Solar1st.year.
# Compute The number of days from the Solar first Date
# month=0 means January, ...
def solardaysfrombaseyear (d): #d is a Date class
Delta = d.year-baseyear
offset = delta*365 + DELTA/4-delta/100 + delta/400
For I in Range (D.month):
Offset + = Daysinsolarmonth[i];
If D.month>1 and Issolarleapyear (d.year):
Offset + = 1
Offset + = D.day
# print ' ___ ', year, month, day, ' offset= ', offset ########
Return offset
# Compute The number of days from the Solar first Date
# month=0 means January, ..., year=0 means 1900, ...
def solardaysfromfirstdate (d): #d is a Date class
Return Solardaysfrombaseyear (d)-Solardaysfrombaseyear (solar1st)
def calclunardayspermonth (iyear):
Code = Yearcode[iyear]
Leapmonth = code&0xf #leapMonth ==0 means no lunar leap month
Code >>= 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 ' Ten '
Elif num>10 and num<20:
Return ' ten ' + 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 ' [0;33;44m%s[0m '% lunarholiday[tmp] + \
"* (6-len (lunarholiday[tmp))
Elif dlunar.day==0:
TMP2 = ' leap ' *isleapmonth + NUM2GB (dlunar.month+1) + ' month '
Return ' [7m%s[0m '% tmp2 + ' * (8-len (TMP2))
Elif dlunar.day<10:
Return ' at the beginning ' + NUM2GB (dlunar.day+1)
Else
Return NUM2GB (dlunar.day+1)
Def Outputcalendar (year, month):
Dlunar = Date ( -1,-1,-1)
ow (' \ n Gregorian%d years%d months '% (year+1900, month+1))
For Iday in range (Getsolardaysinmonth (year, month)):
Dsolar = Date (year, month, Iday)
Dlunar, Isleapmonth = Solar2lunar (dsolar)
If iday==0:
Ow (' Starting from lunar calendar%s%s%s month (%s%s year, the zodiac is%s) \ n '%
(NUM2GB (dlunar.year+1900), ' Leap ' *isleapmonth,
NUM2GB (dlunar.month+1),
GANGB [Dlunar.gan], Zhigb[dlunar.zhi], Shengxiaogb[dlunar.zhi]
))
ow (' = ' *74 + ' \ n ')
For I in range (7):
Ow ("%3s%2s"% (Weekdayen[i][:3], weekdaygb[i]))
ow (' \ n ')
For I in Range (Dlunar.weekday): ow (' *11)
elif dlunar.weekday==0:ow (' \ n ')
Ow ("%2d%-8s"% (iday+1, LUNARDATE2GB (Dlunar, Isleapmonth)))
ow (' \ n ')
def checkargv (argv):
ARGC = Len (argv)
If Argc==1 or argv[1] in ('-H ', '--help '):
Print __doc__; Exit (0)
#in case people input arguments as "4-digit-year month"
If Argc==3 and Len (argv[1]) = = 4 and Len (Argv[2]) in (on):
ARGV[1], argv[2] = argv[2], argv[1]
#Get Month
Month=-1
For Imonth in range (12):
If argv[1].lower () = = Monthen[imonth].lower () or \
Argv[1].lower () = = Monthen[imonth][:3].lower ():
month = imonth+1; Break
If Month==-1:
month = eval (argv[1])
If Month<1 or Month>12:error ("Month not within 1--12.")
#Get year
If argc==2:year = LocalTime (Time ()) [0]
Else
If Len (argv[2])! = 4:error ("Year must is 4 digits.")
Year = eval (argv[2])
If year<1900 or year>= 1900+yearscoded or (year==1900 and month==1):
Error ("Year must is within%d--%d, excluding 1900/1.")
% (1900, 1900 + yearsCoded-1))
Return year-1900, Month-1
Year, month = CHECKARGV (argv)
Calcalllunaryearsinfo ()
Outputcalendar (year, month)
This also has a problem (August 1989 data conversion into the lunar calendar is problematic)
Read several procedures, found that the implementation of this does not need any NB algorithm (as if there is no such algorithm) can be directly implemented to the Gregorian calendar, are recorded a bunch of lunar calendar data, and then according to the basic time to calculate the difference of several days, the correctness of all lunar data determines the correctness of the program.
The classmate gave a LUA program, I tried, and did not find the wrong, first directly to the program (directly from Lua to Python, write a bit messy)
Copy the Code code as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Math
def getdayof (ST):
#– zodiac name
Ctiangan = ["A", "B", "C", "Ding", "E", "Self", "Geng", "Xin", "Ren", "decyl"]
#– name
Cdizhi = [" Son "," ugly "," Yin "," Mao "," Chen, "WU", "noon", "not", "Shen", "unitary", "Xu", "Hai"]
#– zodiac name
Cshuxiang = ["Rat", "ox", "Tiger", "Rabbit", "dragon", "Snake", "horse", "sheep", "monkey", "chicken "," dog "," pig "]
#– Lunar date name
Cdayname =[
" * "," The Day "," second day "," Third Day "," four "," Duanwu ",
" arrest "," When初七because "," Holidays "," Penggushan "," decade ",
" 11 "," 12 " , "13", "14", "XV",
"16", "17", "18", "19", "20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30"
]
#– lunar month name
Cmonname = ["*", "positive", "two", "three", "four", "five", "six", "seven", "eight", "Nine", "Ten", "11", "Wax"]
#– Gregorian calendar days before month
Wmonthadd = [0,31,59,90,120,151,181,212,243,273,304,334]
#– lunar data
Wnonglidata = [ 2635,333387,1701,1748,267701,694,2391,133423,1175,396438
, 3402,3749,331177,1453,694,201326,2350,465197,3221,3402
, 400202,2901,1386,267611,605,2349,137515,2709,464533,1738
, 2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
, 2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
, 330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
, 1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
, 1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
, 268949,3402,3493,133973,1386,464219,605,2349,334123,2709
, 2890,267946,2773,592565,1210,2651,395863,1323,2707,265877]
#-The current calendar year, month, day-
Wcuryear = st["Year"]
Wcurmonth = st["Mon"]
Wcurday = st["Day"]
#-calculation to the initial time February 8, 1921 days: 1921-2-8 (first day of the first month)-
#nTheDate = (wcuryear–1921) * 365 + (wcuryear–1921)/4 + Wcurday + wmonthadd[wcurmonth]–38
Nthedate = (wcuryear–1921) * 365 + (wcuryear–1921)/4 + Wcurday + wmonthadd[wcurmonth-1]–38
if (((wcuryear% 4) = = 0) and (Wcurmonth > 2)):
Nthedate = nthedate + 1
#– calculation of lunar zodiac, earthly branches, months and days-
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 <= (+ nBit):
Nisend = 1
Break
Nthedate = Nthedate–29–nbit
n = n–1
If nisend! = 0:
Break
m = m + 1
Wcuryear = 1921 + M
Wcurmonth = k–n + 1
wcurday = Int (Math.floor (nthedate))
if k = = 12:
if Wcurmonth = = Wnonglidata[m]/65536 + 1:
Wcurmonth = 1–wcurmonth
Elif wcurmonth > Wnonglidata[m]/65536 + 1:
Wcurmonth = wcurmonth–1
print ' Solar calendar ', st["year"], st["mon"], st["Day"]
print ' lunar calendar ', wcuryear, Wcurmonth, Wcurday
#– generates lunar zodiac, earthly branches, Zodiac ==> wnongli–
Szshuxiang = cshuxiang[(((wCurYear-4)% 60)% 12) + 1]
Szshuxiang = cshuxiang[(((wCurYear-4)% 60)% 12) + 1]
Znongli = Szshuxiang + ' (' + ctiangan[(((wCurYear-4)%)] + cdizhi[((((wCurYear-4)% 60)% 12)] + ') Year '
#–sznongli, "%s (%s%s) years", szshuxiang,ctiangan[((wCurYear-4)%)% 10],cdizhi[((wCurYear-4)% 60)% 12]);
#– generation lunar month, day ==> wnongliday–*/
If Wcurmonth < 1:
Sznongliday = "Leap" + cmonname[( -1 * wcurmonth)]
Else
Sznongliday = Cmonname[wcurmonth]
Sznongliday = Sznongliday + "month" + Cdayname[wcurday]
Print Sznongliday
#return Sznongli. Sznongliday
def main ():
St = {"Year": 1989, "Mon": 8, "Day": 1}
Getdayof (ST)
ST1 = {"Year": +, "mon": Ten, "Day": 7}
Getdayof (ST1)
ST1 = {"Year": +, "mon": Ten, "Day": 1}
Getdayof (ST1)
#print ("".. Getdayof (ST))
Main ()
Data is basically correct, according to their own needs to change the program can be. There is time to change a little later.