Python method to complete the lunar calendar function code

Source: Internet
Author: User

From

#Coding=utf-8 #******************************************************************************#The following is the lunar calendar to calculate the required data, in order to save storage space, so use the following more abnormal storage methods.#******************************************************************************#the array g_lunar_month_day is deposited into the lunar calendar from 1901 to 2050, the number of months of the year information#The lunar calendar can only be 29 or 30 days per month, one year with 12 (or 13) bits, corresponding bit is 1 table 30 days, otherwise 29 daysG_lunar_month_day = [    0X4AE0, 0xa570, 0x5268, 0xd260, 0xd950, 0x6aa8, 0x56a0, 0x9ad0, 0x4ae8, 0X4AE0,#19100xa4d8, 0xa4d0, 0xd250, 0xd548, 0xb550, 0x56a0, 0x96d0, 0x95b0, 0x49b8, 0x49b0,#1920x10800xa4b0, 0xb258, 0x6a50, 0X6D40, 0xada8, 0x2b60, 0x9570, 0x4978, 0x4970, 0x64b0,#19300xd4a0, 0xea50, 0x6d48, 0x5ad0, 0x2b60, 0x9370, 0x92e0, 0xc968, 0xc950, 0xd4a0,#19400xda50, 0xb550, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, 0xb4a8, 0X6CA0,#19500xb550, 0x55a8, 0x4da0, 0xa5b0, 0x52b8, 0x52b0, 0xa950, 0xe950, 0x6aa0, 0XAD50,#19600XAB50, 0x4b60, 0xa570, 0xa570, 0x5260, 0xe930, 0xd950, 0x5aa8, 0x56a0, 0x96d0,#19700x4ae8, 0x4ad0, 0xa4d0, 0xd268, 0xd250, 0xd528, 0xb540, 0xb6a0, 0x96d0, 0x95b0,#19800x49b0, 0xa4b8, 0xa4b0, 0xb258, 0x6a50, 0X6D40, 0xada0, 0xab60, 0x9370, 0x4978,#19900x4970, 0x64b0, 0x6a50, 0xea50, 0x6b28, 0X5AC0, 0xab60, 0x9368, 0x92e0, 0xc960,# -0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950,# .0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, 0xa5b0, 0x52b8, 0x52b0, 0xa930, 0x74a8,#20200X6AA0, 0XAD50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260, 0xe930, 0xd530, 0X5AA0,#20300X6B50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520, 0xdaa0, 0xb5a0,#20400x56d0, 0x4ad8, 0x49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0X6D20, 0xada0, 0x55b0,#2050] #The array Glanarmonth holds the month of the lunar calendar from 1901 to 2050, if none is 0, each byte is stored for two yearsG_lunar_month = [    0x00, 0x50, 0x04, 0x00, 0x20,#19100x60, 0x05, 0x00, 0x20, 0x70,#1920x10800x05, 0x00, 0x40, 0x02, 0x06,#19300x00, 0x50, 0x03, 0x07, 0x00,#19400x60, 0x04, 0x00, 0x20, 0x70,#19500x05, 0x00, 0x30, 0x80, 0x06,#19600x00, 0x40, 0x03, 0x07, 0x00,#19700x50, 0x04, 0x08, 0x00, 0x60,#19800x04, 0x0a, 0x00, 0x60, 0x05,#19900x00, 0x30, 0x80, 0x05, 0x00,# -0x40, 0x02, 0x07, 0x00, 0x50,# .0x04, 0x09, 0x00, 0x60, 0x04,#20200x00, 0x20, 0x60, 0x05, 0x00,#20300x30, 0xb0, 0x06, 0x00, 0x50,#20400x02, 0x07, 0x00, 0x50, 0x03#2050] #==================================================================================  fromDatetimeImportDate, DateTime fromCalendarImportCalendar as Cal start_year= 1901defIs_leap_year (tm): Y=Tm.yearreturn( not(y% 4)) and(Y% 100)or( not(Y% 400)) defShow_month (tm): (Ly, LM, LD)=Get_ludar_date (tm)Print    PrintU"%d years%d months%d days"%(Tm.year, Tm.month, Tm.day), Week_str (tm),PrintU"\ t Lunar:", Y_lunar (ly), M_lunar (LM), D_lunar (LD)Print    PrintU"day \ t two \ t three \ four \ t five \ t six"C=Cal () DS= [D forDinchc.itermonthdays (Tm.year, Tm.month)] Count=0 forDinchDs:count+ = 1ifD = =0:Print "\ t",            Continue(ly, LM, LD)=get_ludar_date (DateTime (Tm.year, Tm.month, D))ifCount% 7 = =0:PrintD_str=Str (d)ifD = =Tm.day:d_str= u"*"+D_strPrintD_str + d_lunar (LD) + U"\ t",    Print defthis_month (): Show_month (DateTime.Now ())#www.iplaypython.com defWeek_str (tm): a= u'Monday Tuesday Wednesday Thursday Friday Saturday Sunday'. Split ()returnA[tm.weekday ()]defD_lunar (LD): a= u'First second day third day four Duanwu arrest When初七because holidays Penggushan 1.,234,567,892,212,22e,+28'. Split ()returnA[ld-1] defM_lunar (LM): a= u'April month May June seven month August Borlnd November December'. Split ()returnA[lm-1] defY_lunar (ly): Y=ly TG= u'methyl-N-Xing'. Split () DZ= u'obscure Chen Shin hai'. Split () SX= u'Mouse cow tiger dragon snake horse sheep monkey chicken Dog pig'. Split ()returntg[(y-4)%] + dz[(y-4)%] + U' '+ sx[(y-4)%] + U'years' defDate_diff (tm):return(TM-datetime (1901, 1, 1). daysdefGet_leap_month (lunar_year): Flag= g_lunar_month[(lunar_year-start_year)/2]    if(lunar_year-start_year)% 2:        returnFlag & 0x0fElse:        returnFlag >> 4deflunar_month_days (Lunar_year, lunar_month):if(Lunar_year <start_year):return30High , low= 0, 29ibit= 16-Lunar_month; if(Lunar_month > Get_leap_month (lunar_year) andGet_leap_month (lunar_year)): Ibit-= 1if(G_lunar_month_day[lunar_year-start_year] & (1 <<ibit)): Low+ = 1if(Lunar_month = =Get_leap_month (lunar_year)):if(G_lunar_month_day[lunar_year-start_year] & (1 << (iBit-1)) : High= 30Else: High= 29return(High, low)deflunar_year_days (year): days=0 forIinchRange (1, 13): (High, low)=Lunar_month_days (year, i) days+= High Days+= Lowreturn DaysdefGet_ludar_date (tm): Span_days=Date_diff (tm)#February 19, 1901 for lunar calendar 1901 month    #Gregorian calendar January 1, 1901 to February 19 total 49 days    if(Span_days <49): year= Start_year-1if(Span_days <19): Month= 11; Day= 11 +span_daysElse: Month= 12; Day= span_days-18return(year, month, day)#from the beginning of the lunar month of 1901Span_days-= 49Year , month, Day= Start_year, 1, 1#Calculation YearTMP =lunar_year_days (year) whileSpan_days >=tmp:span_days-=tmp Year+ = 1tmp=lunar_year_days (year)#Calculate Month(foo, tmp) =Lunar_month_days (year, month) whileSpan_days >=tmp:span_days-=tmpif(Month = =Get_leap_month (year)): (TMP, foo)=Lunar_month_days (year, month)if(Span_days <tmp):return(0, 0, 0) span_days-=tmp Month+ = 1(foo, tmp)=Lunar_month_days (year, month)#Calculation DayDay + =span_daysreturn(year, month, day)#simple function, only print the current monthThis_month ()

Python method to complete the lunar calendar function code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.