lunar_python

repository·master·Indexed 20 days ago

https://github.com/6tail/lunar-python

A zero-dependency Python library for managing solar and lunar calendars. It provides extensive support for traditional Chinese astrological data, including the Nongli (Lunar), Buddhist, and Taoist calendars, BaZi (Eight Characters) calculations, solar terms (JieQi), and traditional almanac (Huangli) features such as zodiac signs and the sexagenary cycle.

Tokens
18.9K
Snippets
78
Records
92
Agent score
66%

What's inside lunar_python

  1. Overview of lunar_python features

    master

    The lunar_python library is a zero-dependency tool for handling various calendar systems and traditional Chinese astrological data.

    Supported features include:

    • Calendars: Solar (Gregorian), Lunar (Nongli), Buddhist, and Taoist calendars.
    • Astrology & Zodiac: Constellations (Zodiac signs), Sexagenary cycle (Gan-Zhi), Chinese Zodiac (Shengxiao), and Eight Characters (BaZi).
    • Traditional Almanac (Huangli): Solar terms (Jieqi), Festivals, Pengzu Baiji, Auspicious gods (Xi-shen, Fu-shen, Cai-shen, etc.), Fetal god directions, Clashes (Chong-sha), and Na Yin.
    • Other Metadata: Julian Day, Five Elements (Wu Xing), Ten Gods (Shi Shen), Twelve Jianchu stars, and official holiday/adjustment schedules.
  2. Initialize a Lunar object and convert to Solar

    master

    Use Lunar.fromYmd(year, month, day) to initialize a lunar calendar object from a specific year, month, and day. You can then retrieve the corresponding solar calendar information using the .getSolar() method.

    from lunar_python import Lunar
    
    # Initialize lunar calendar by specifying year, month, and day
    lunar = Lunar.fromYmd(1986, 4, 21)
    
    # Print the full lunar calendar string
    print(lunar.toFullString())
    
    # Convert lunar to solar and print the full solar calendar string
    print(lunar.getSolar().toFullString())
  3. Initialize and use Lunar objects

    master

    Use the Lunar.fromYmd(year, month, day) method to create a Lunar instance from a specific year, month, and day. You can then use toFullString() to get a detailed Chinese Lunar description or getSolar() to convert the instance to a Solar calendar object.

    from lunar_python import Lunar
    
    # init lunar by ymd
    lunar = Lunar.fromYmd(1986, 4, 21)
    
    # print lunar
    print(lunar.toFullString())
    
    # convert to solar and print
    print(lunar.getSolar().toFullString())
  4. Initialize a Solar date

    master

    The Solar class represents a Gregorian calendar date. You can initialize it using several factory methods depending on your available data format.

    from lunar_python import Solar
    from datetime import datetime
    
    # From year, month, day, hour, minute, second
    solar = Solar(2023, 10, 27, 10, 30, 0)
    
    # From a datetime object
    dt = datetime(2023, 10, 27, 10, 30, 0)
    solar_from_dt = Solar.fromDate(dt)
    
    # From year, month, day
    solar_ymd = Solar.fromYmd(2023, 10, 27)
    
    # From year, month, day, hour, minute, second
    solar_ymdhms = Solar.fromYmdHms(2023, 10, 27, 10, 30, 0)
  5. Get time and related calendar conversions

    master

    Methods for handling time and alternative calendar systems:

    • getTime(): Returns a LunarTime object representing the current time of the Lunar instance.
    • getTimes(): Returns a list of all 12 LunarTime objects (shichen/double-hours) for the current day.
    • getFoto(): Returns the Buddhist calendar (Foto) equivalent of the current lunar date.
    • getTao(): Returns the Taoist calendar (Tao) equivalent of the current lunar date.
  6. Get Chong and Sha information from LunarTime

    master

    The LunarTime class provides methods to identify clashes (Chong) and negative influences (Sha) for a specific time:

    • getChong(): Returns the Earthly Branch that clashes with the current time.
    • getChongGan(): Returns the Heavenly Stem of the clash.
    • getChongShengXiao(): Returns the Zodiac sign of the clashing branch.
    • getChongDesc(): Returns a formatted description of the clash (e.g., "(甲子)鼠").
    • getSha(): Returns the Sha (煞) associated with the current Earthly Branch.
    • getXun(): Returns the Xun (旬) the time belongs to.
    • getXunKong(): Returns the Xun Kong (旬空) or void period.
    from lunar_python import LunarTime
    
    lunar_time = LunarTime.fromYmdHms(2023, 1, 1, 12, 0, 0)
    
    print(f"Clash: {lunar_time.getChongDesc()}")
    print(f"Sha: {lunar_time.getSha()}")
    print(f"Xun: {lunar_time.getXun()}")
    print(f"Xun Kong: {lunar_time.getXunKong()}")
  7. Initialize SolarMonth using year and month

    master

    The SolarMonth class represents a month in the solar (Gregorian) calendar. You can initialize it directly by providing a year and a month, or use factory methods to create instances from existing date objects or year/month pairs.

    from lunar_python import SolarMonth
    import datetime
    
    # Initialize with year and month
    solar_month = SolarMonth(2023, 10)
    
    # Use fromYm factory method
    solar_month_alt = SolarMonth.fromYm(2023, 10)
    
    # Use fromDate factory method with a datetime object
    date_obj = datetime.date(2023, 10, 1)
    solar_month_date = SolarMonth.fromDate(date_obj)
  8. Use the SolarSeason class to manage solar quarters

    master

    The SolarSeason class represents a solar quarter (阳历季度) based on a specific year and month. It allows you to determine which quarter a month belongs to, retrieve the months within a quarter, and navigate between quarters.

    Initialization

    You can initialize a SolarSeason instance in two ways:

    1. fromYm(year, month): Create an instance by providing a specific year and month.
    2. fromDate(date): Create an instance using a standard Python date object.

    Key Methods

    • getIndex(): Returns the quarter index (1-indexed). For example, months 1-3 return 1, 4-6 return 2, etc.
    • getMonths(): Returns a list of SolarMonth objects representing all months in that specific quarter.
    • next(seasons): Shifts the current quarter by a specified number of quarters. Use a positive integer to move forward or a negative integer to move backward.
    • toString(): Returns a string in the format year.quarter (e.g., 2023.1).
    • toFullString(): Returns a formatted string in Chinese (e.g., 2023年1季度).
    from lunar_python import SolarSeason
    import datetime
    
    # Initialize from year and month
    season = SolarSeason.fromYm(2023, 5)
    print(f"Quarter index: {season.getIndex()}")  # Output: 2
    print(f"String format: {season.toString()}") # Output: 2023.2
    
    # Initialize from a date object
    date_obj = datetime.date(2023, 10, 1)
    season_from_date = SolarSeason.fromDate(date_obj)
    print(f"Full string: {season_from_date.toFullString()}") # Output: 2023年4季度
    
    # Get months in this quarter
    months = season.getMonths()
    for m in months:
        print(f"Month: {m.getYear()}.{m.getMonth()}")
    
    # Move to the next quarter
    next_season = season.next(1)
    print(f"Next season: {next_season.toString()}")
  9. Get Solar date information (Week, Festivals, Zodiac)

    master

    The Solar class provides several methods to extract metadata about a specific date:

    • getWeek(): Returns the day of the week as an integer (0 for Sunday, 1 for Monday, etc.).
    • getWeekInChinese(): Returns the day of the week in Chinese characters (e.g., '日', '一').
    • getFestivals(): Returns a list of official holidays/festivals occurring on that date.
    • getOtherFestivals(): Returns a list of non-official/traditional festivals (e.g., Zhongyuan Festival).
    • getXingZuo(): Returns the Western Zodiac sign.
    • isLeapYear(): Returns True if the year is a leap year.
    solar = Solar(2023, 10, 27)
    
    print(solar.getWeek())           # 5
    print(solar.getWeekInChinese())  # 五
    print(solar.getFestivals())      # ['...']
    print(solar.getXingZuo())       # 天蝎
    print(solar.isLeapYear())        # False
  10. Use the NineStar class to access astrological data

    master

    The NineStar class provides access to various astrological attributes associated with a specific index (representing one of the nine stars). You can instantiate it using NineStar.fromIndex(index) and retrieve information related to different systems such as Bei Dou, Xuan Kong, Qi Men, and Tai Yi.

    Key attributes available include:

    • Basic Info: Number, Color, Wu Xing (Five Elements), and Position.
    • Bei Dou: Star names in the Bei Dou system.
    • Xuan Kong: Star names, luck status, and associated elements.
    • Qi Men: Star names, luck status, Yin/Yang status, and Ba Men (Eight Gates) association.
    • Tai Yi: Star names, type (e.g., auspicious/inauspicious), and descriptive poems (Song).
    from lunar_python import NineStar
    
    # Initialize a NineStar instance using an index (0-8)
    star = NineStar.fromIndex(0)
    
    print(star.getNumber())           # e.g., "一"
    print(star.getColor())            # e.g., "白"
    print(star.getWuXing())           # e.g., "水"
    print(star.getNameInBeiDou())     # e.g., "天枢"
    print(star.getNameInXuanKong())    # e.g., "贪狼"
    print(star.toFullString())       # Returns a detailed formatted string of all attributes