Python: an example of how to convert a Gregorian calendar to a lunar calendar.

Source: Internet
Author: User

Python: an example of how to convert a Gregorian calendar to a lunar calendar.

This article describes how to convert a calendar from a Gregorian calendar to a lunar calendar in Python. We will share this with you for your reference. The details are as follows:

Two key points:

1. the query table (126th rows) is used for the conversion from the Gregorian calendar to the lunar calendar)
2. What is the Astronomical Method Used for solar term? (176th rows)

Running Diagram(The background is hao123 Perpetual calendar)

Source code:

# Lunar. py #2015/02/27 Luo Bing import datetimeclass Lunar (object ): #*************************************** ***************************************# the following figure shows the data required for the calculation of the lunar calendar, to save storage space, we use the following abnormal storage methods. #*************************************** ***************************************# array g_lunar_month_day is saved to the number of days of the month in the year from January 1, 1901 to January 1, 2050, # The lunar calendar can only be 29 or 30 days a month. 12 (or 13) binary bits are used for a year. The corresponding bits are 1 Table and 30 days; otherwise, the value is 29 days g_lunar_month_day = [0x4ae0, 0xa570, 0x5268, 0xd260, 0xd950, 0x6aa8, 0x56a0, 0x9ad0, 0x4ae8, 0x4ae0, #1910, primary, 0xd250, primary, 0xb550, 0x56a0, 0x96d0, primary, 0x49b8, 0x49b0, #1920 0xa4b0, 0xb258, 0x6a50, primary, 0xada8, 0x2b60, 0x9570, 0x4978, 0x4970, 0x64b0, #1930 0xd4a0, 0xea50, 0x6d48, 0x5ad0, primary, 0x9370, 0x92e0, 0xc968, 0xc950, 0xd4a0, #1940 0xda50, 0xb550, 0x56a0, 0xaad8, 0x25d0, 0x92d0, 0xc958, 0xa950, 0xb4a8, 0x6ca0, #1950 0xb550, 0x55a8, 0x4da0, primary, 0x52b8, 0x52b0, 0xa950, 0xe950, primary, 0xad50, #1960 0xab50, primary, 0xa570, 0xa570, 0x5260, primary, 0xd950, 0x5aa8, 0x56a0, 0x96d0, #1970 0x4ae8, primary, 0xa4d0, primary, 0xd250, primary, #1980 0x49b0, 0xa4b8, 0xa4b0, 0xb258, 0x6a50, 0x6d40, 0xada0, 0xab60, 0x9370, 0x4978, #1990 0x4970, 0x64b0, 0x6a50, 0xea50, 0x6b28, 0x5ac0, 0xab60, 0x9368, 0x92e0, 0xc960, #2000 hour, 0xd4a0, 0xda50, 0x5aa8, 0x56a0, hour, 0xc958, 0xa950, #2010 0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, primary, 0x52b8, primary, 0xa930, 0x74a8, #2020 primary, 0xad50, primary, 0x4b60, 0x9570, 0xa4e0, 0xd260, 0xe930, 0xd530, 0x5aa0, #2030 0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520, 0xdaa0, 0xb5a0, #2040 0x56d0, 0x4ad8, 0x 49b0, 0xa4b8, 0xa4b0, 0xaa50, 0xb528, 0x6d20, 0xada0, 0x55b0, #2050] # array gLanarMonth stores the month of the lunar calendar from January 1, to the month of January 1, g_lunar_month = [0x00, 0x50, 0x04, 0x00, 0x20, #1910 0x60, 0x05, 0x00, 0x20, 0x70, #1920 0x05, 0x00, 0x40, 0x02, 0x06, #1930 0x00, 0x50, 0x03, 0x07, 0x00, #1940 0x60, 0x04, 0x00, 0x20, 0x70, #1950 0x05, 0x00, 0x30, 0x80, 0x06, #1960 0x00, 0x40, 0x03, 0x07, 0x00, #1970 0x50, 0x04, 0x08, 0x00, 0x60, #1980 0x04, 0x0a, 0x00, 0x60, 0x05, #1990 0x00, 0x30, 0x80, 0x05, 0x00, #2000 0x40, 0x02, 0x07, 0x00, 0x50, #2010 0x04, 0x09, 0x00, 0x60, 0x04, #2020 0x00, 0x20, 0x60, 0x05, 0x00, #2030 0x30, 0xb0, 0x06, 0x00, 0x50, #2040 0x02, 0x07, 0x00, 0x50, 0x03 #2050] START_YEAR = 1901 # tiangan = 'a, B, C, E, and G, Xin, Xi '# dizhi zhi = 'ziyin, Mao Chen, Wu, Wu, XI, Hai' # Zodiac xiao =' rat, ox, Tiger, Rabbit, dragon, Snake, horse, monkey, chicken, dog, and pig # month lm = 'October 80 or 90, dongla '# Day ld = 'day 1, Day 2, day 3, day 4, day 5, day 6, day 7, day 8, Day 9, day 11, day 13, day 14, Day 18, day 19, Day 1, Day 2, day 3, day 4, day 6, day 7, day 8 930' # solar term jie = 'small cold, big cold, spring, rain, spring, summer, autumn, White Dew, Autumn, frost, Winter, snow, snow, winter solstice 'def __( self, dt = None): ''' initialization: the parameter is datetime. datetime class instance, default current time '''self. localtime = dt if dt else datetime. datetime. today () def sx_year (self): # returns the year of the Zodiac ct = self. localtime # obtain the current time year = self. ln_year ()-3-1 # minus 3 (Note: minus 1) year = year % 12 # Model 12, return Self. xiao [year] def gz_year (self): # The ct = self is returned. localtime # obtain the current time year = self. ln_year ()-3-1 # subtract 3 from the lunar year (Note: minus 1) G = year % 10 # model 10, get the number of days dry Z = year % 12 # Model 12, return self. gan [G] + self. zhi [Z] def gz_month (self): # returns the dry branch calendar month (not implemented) pass def gz_day (self): # returns the dry branch calendar ct = self. localtime # obtain the current time C = ct. year // 100 # century, minus one y = ct. year % 100 # Take the last two digits of the year (if it is January, February, the current year minus one) y = y-1 if ct. month = 1 or ct. Month = 2 else y M = ct. month # Take the month (if it is January, February, it is calculated by 13, 14 respectively) M = M + 12 if ct. month = 1 or ct. month = 2 else M d = ct. day # Number of days I = 0 if ct. month % 2 = 1 else 6 # Take I (odd month I = 0, even month I = 6) # The following two are online formulas # http://baike.baidu.com/link? Url = Done # computation (Note: minus 1) G = 4 * C + C // 4 + 5 * y + y // 4 + 3 * (M + 1) // 5 + d-3-1G = G % 10 # computation count (Note: minus 1) Z = 8 * C + C // 4 + 5 * y + y // 4 + 3 * (M + 1) // 5 + d + 7 + I-1 Z = Z % 12 # return the return self. gan [G] + self. zhi [Z] def gz_hour (self): # Return computation time (hour) ct = self. localtime # obtain the current time # Count Z = round (ct. ho Ur/2) + 0.1) % 12 # The reason for adding 0.1 is the bug of round !! # Return the return self. zhi [Z] def ln_year (self): # return year, _, _ = self. ln_date () return year def ln_month (self): # returns the lunar month _, month, _ = self. ln_date () return month def ln_day (self): # returns the _, _, day = self. ln_date () return day def ln_date (self): # returns the integer tuples of the lunar date (year, month, and day) (Table query) delta_days = self. _ date_diff () # The Gregorian calendar in April February 19, 1901 is the lunar calendar in April lunar January 1, 1901 # the Gregorian calendar in April January 1 to February 19, 1901 has a total of 49 days. if (delta_days <49): year = self. START_YEAR-1 if (delta_days <19): month = 11; day = 11 + delta_days else: month = 12; day = delta_days-18 return (year, month, day) # delta_days-= 49 year, month, day = self. START_YEAR, 1, 1 # Calculation year tmp = self. _ lunar_year_days (year) while delta_days> = tmp: delta_days-= tmp year + = 1 tmp = self. _ lunar_year_days (year) # Calculate the month (foo, tmp) = self. _ lunar_month_days (year, month) while delta_days> = tmp: delta_days-= tmp if (month = self. _ get_leap_month (year): (tmp, foo) = self. _ lunar_month_days (year, month) if (delta_days <tmp): return (0, 0, 0) return (year, month, delta_days + 1) delta_days-= tmp month + = 1 (foo, tmp) = self. _ lunar_month_days (year, month) # Calculate the day + = delta_days return (year, month, day) def ln_date_str (self): # return the string of the lunar date, for example, February 1, lunar January 9 _, month, day = self. ln_date () return 'lunar calendar {} month {}'. format (self. lm [month-1], self. ld [(day-1) * 2: day * 2]) def ln_jie (self): # returns the lunar calendar solar ct = self. localtime # obtain the current time year = ct. year for I in range (24): # because both are floating point numbers, delta = self cannot be expressed as equal. _ julian_day ()-self. _ julian_day_of_ln_jie (year, I) if -. 5 <= delta <=. 5: return self. jie [I * 2 :( I + 1) * 2] return ''# display calendar def calendar (self ): pass ####################################### ################# private functions ################## ###################################### def _ date_diff (self): ''' the return value is based on the daily difference ''' return (self. localtime-datetime. datetime (1901, 1, 1 )). days def _ get_leap_month (self, lunar_year): flag = self. g_lunar_month [(lunar_year-self. START_YEAR) // 2] if (lunar_year-self. START_YEAR) % 2: return flag & 0x0f else: return flag> 4 def _ lunar_month_days (self, lunar_year, lunar_month): if (lunar_year <self. START_YEAR): return 30 high, low = 0, 29 iBit = 16-lunar_month; if (lunar_month> self. _ get_leap_month (lunar_year) and self. _ get_leap_month (lunar_year): iBit-= 1 if (self. g_lunar_month_day [lunar_year-self. START_YEAR] & (1 <iBit): low + = 1 if (lunar_month = self. _ get_leap_month (lunar_year): if (self. g_lunar_month_day [lunar_year-self. START_YEAR] & (1 <(iBit-1): high = 30 else: high = 29 return (high, low) def _ lunar_year_days (self, year ): days = 0 for I in range (1, 13): (high, low) = self. _ lunar_month_days (year, I) days + = high days + = low return days # returns the Confucian def _ julian_day (self): ct = self. localtime # obtain the current time year = ct. year month = ct. month day = ct. day if month <= 2: month + = 12 year-= 1 B = year/100 B = 2-B + year/400 dd = day + 0.5000115740 # after of the current day, it is the beginning of the day (after one second ).) */return int (365.25 * (year + 4716) + 0.01) + int (30.60001 * (month + 1 )) + dd + B-1524.5 # returns the number of vertices in the specified year def _ julian_day_of_ln_jie (self, year, st): s_stAccInfo = [0.00, 1272494.40, 2548020.60, 3830143.80, 5120226.60, 6420865.80, 7732018.80, 9055272.60, 10388958.00, 11733065.40, 13084292.40, 14441592.00, 15800560.80, 17159347.20, 18513766.20, 19862002.20, 21201005.40, 22529659.80, 23846845.20, 25152606.00, 26447687.40, 27733451.40, 29011921.20, 30285477.60] # It is known that the cold time in March is 1900 02:05:00 base1900_SlightColdJD = 2415025.5868055555 if (st <0) or (st> 24): return 0.0 stJd = 365.24219878 * (year-1900) + s_stAccInfo [st]/86400.0 return base1900_SlightColdJD + stJd # test def test (ct = None): ln = Lunar (ct) print ('gregorian calendar {} Beijing time {}'. format (ln. localtime. date (), ln. localtime. time () print '. format (ln. ln_date_str (), ln.gz _ year (), ln. sx_year (), ln.gz _ day (), ln.gz _ hour () print ('solar :{}'. format (ln. ln_jie () if _ name _ = '_ main _': ct = datetime. datetime (,) test (ct)

PS: Here are some calendar-related online tools for your reference:

Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli

Web calendar:
Http://tools.jb51.net/bianmin/webwannianli

Online calendar:
Http://tools.jb51.net/bianmin/wannianli

Online calendar flash version:
Http://tools.jb51.net/bianmin/flashwnl

In addition, this siteToday in historyThere are also similar lunar date display functions:

Http://tools.jb51.net/bianmin/lishi

Related Article

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.