Calculating lunar/lunisolar holidays in Python

前端 未结 2 459
灰色年华
灰色年华 2021-02-08 22:19

Any calendar nuts here? I\'ve been looking for information on how to calculate the holidays of the current year that occur irregularly in the Gregorian Calendar. Typically thi

2条回答
  •  别那么骄傲
    2021-02-08 22:48

    I have coded both of these, using existing libraries.

    Here is the code for Ramadan:

    from convertdate import islamic
    
    def ramadan(year):
        islamic_year = islamic.from_gregorian(year, 1, 1)[0]
        result = islamic.to_gregorian(islamic_year, 9, 1)
        if result[0] < year:
            result = islamic.to_gregorian(islamic_year+1, 9, 1)
        elif result[0] > year:
            result = islamic.to_gregorian(islamic_year-1, 9, 1)
        return date(*result)
    

    Here is the code for Chinese New Year:

    from lunardate import LunarDate
    
    _yot = ('Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig')
    
    def year_of_the(year):
        yr = (year - 2020) % len(_yot)
        return _yot[yr]
    
    def chinese_new_year(year):
        """Returns a tuple of the (date, year_of_the: str)"""
        return (LunarDate(year, 1, 1).toSolarDate(), year_of_the(year))
    

    I opened an enhancement request for convertdate to add them: https://github.com/fitnr/convertdate/issues/24

提交回复
热议问题