### Install python-lunardate Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Installs the python-lunardate library using pip. This is the primary method for obtaining the library for use in Python projects. ```bash pip install lunardate ``` -------------------------------- ### Create LunarDate with Leap Month Indicator Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates creating LunarDate objects with leap month indicators using both boolean and integer values. The isLeapMonth parameter specifies if the month is a leap month. This example shows that True and 1 are equivalent. ```python lunar1 = LunarDate(1976, 8, 8, isLeapMonth=1) lunar2 = LunarDate(1976, 8, 8, isLeapMonth=True) print(lunar1 == lunar2) ``` -------------------------------- ### Perform Date Comparison Operations in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates how to compare two `LunarDate` objects using standard comparison operators (`<`, `<=`, `>`, `>=`, `==`, `!=`). Comparisons are based on the chronological order of dates. This example illustrates equality and inequality comparisons. ```python from lunardate import LunarDate # Create sample dates lunar1 = LunarDate(1976, 8, 8) lunar2 = LunarDate.today() lunar3 = LunarDate(1976, 8, 8) # Equality comparison print(f"lunar1 == lunar3: {lunar1 == lunar3}") print(f"lunar1 == lunar2: {lunar1 == lunar2}") print(f"Today == Today: {LunarDate.today() == LunarDate.today()}") # Inequality comparison print(f"lunar1 != lunar2: {lunar1 != lunar2}") ``` -------------------------------- ### Calculate Date Differences in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates how to calculate the difference between two dates, returning a `datetime.timedelta` object. It supports differences between two `LunarDate` objects, between a `LunarDate` and a `datetime.date`, and handles both directions of subtraction. The example calculates age in days and validates date ranges. ```python from lunardate import LunarDate import datetime # Difference between two lunar dates lunar1 = LunarDate(1976, 8, 8) lunar2 = LunarDate(1976, 8, 18) diff = lunar2 - lunar1 print(f"Difference: {diff.days} days") # Same date difference is zero lunar = LunarDate(1976, 8, 8) diff_zero = lunar - lunar print(f"Same date difference: {diff_zero}") # Difference between lunar and solar dates lunar = LunarDate(1976, 8, 8) solar = datetime.date(2008, 1, 1) diff_ls = lunar - solar print(f"Lunar - Solar: {diff_ls.days} days") # Reverse subtraction (solar - lunar) diff_sl = solar - lunar print(f"Solar - Lunar: {diff_sl.days} days") # Calculate age in days birth_lunar = LunarDate(1990, 5, 15) today_lunar = LunarDate.today() age_days = (today_lunar - birth_lunar).days print(f"Age in days: {age_days}") # Use differences for date validation start = LunarDate(2000, 1, 1) end = LunarDate(2000, 12, 29) duration = (end - start).days print(f"Duration of lunar year 2000: {duration} days") ``` -------------------------------- ### Get Current Lunar Date Source: https://context7.com/lidaobing/python-lunardate/llms.txt Returns a LunarDate object representing today's date in the lunar calendar. This class method retrieves the current system date and converts it to lunar format. ```APIDOC ## Get Current Lunar Date ### Description Returns a `LunarDate` object representing today's date in the lunar calendar. This class method retrieves the current system date and converts it to lunar format, providing a convenient way to work with the current lunar date. ### Method `LunarDate.today()` ### Endpoint N/A (Library method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from lunardate import LunarDate import datetime # Get today's lunar date today_lunar = LunarDate.today() print(f"Today's lunar date: {today_lunar}") print(f"Type: {type(today_lunar).__name__}") # Type: LunarDate # Compare with today's solar date today_solar = datetime.date.today() print(f"Today's solar date: {today_solar}") # Access components print(f"Lunar year: {today_lunar.year}") print(f"Lunar month: {today_lunar.month}") print(f"Lunar day: {today_lunar.day}") print(f"Is leap month: {today_lunar.isLeapMonth}") ``` ### Response #### Success Response (200) - **LunarDate object** (object) - A `LunarDate` object representing the current lunar date. ``` -------------------------------- ### Get Today's Lunar Date - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Retrieves the current date in the Chinese Lunar Calendar using the `LunarDate.today()` class method. This provides a convenient way to get the current lunar date without manual conversion. ```python from lunardate import LunarDate today_lunar = LunarDate.today() print(today_lunar) print(type(today_lunar).__name__) ``` -------------------------------- ### Get Leap Month for a Year - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Provides a way to determine the leap month for a specific Gregorian year using the `LunarDate.leapMonthForYear()` class method. It returns the month number if a leap month exists, or `None` if there is no leap month in that year. ```python from lunardate import LunarDate year_with_leap = 2023 year_without_leap = 2022 leap_month_2023 = LunarDate.leapMonthForYear(year_with_leap) leap_month_2022 = LunarDate.leapMonthForYear(year_without_leap) print(f"Leap month for {year_with_leap}: {leap_month_2023}") print(f"Leap month for {year_without_leap}: {leap_month_2022}") ``` -------------------------------- ### Get Current Lunar Date in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Returns a LunarDate object representing the current date in the lunar calendar. This class method retrieves the current system date and converts it to the lunar format, offering a convenient way to access today's lunar date components. It can be compared with the current solar date. ```python from lunardate import LunarDate import datetime # Get today's lunar date today_lunar = LunarDate.today() print(f"Today's lunar date: {today_lunar}") print(f"Type: {type(today_lunar).__name__}") # Type: LunarDate # Compare with today's solar date today_solar = datetime.date.today() print(f"Today's solar date: {today_solar}") # Access components print(f"Lunar year: {today_lunar.year}") print(f"Lunar month: {today_lunar.month}") print(f"Lunar day: {today_lunar.day}") print(f"Is leap month: {today_lunar.isLeapMonth}") ``` -------------------------------- ### Handling Leap Month Dates in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Illustrates how to work with leap months in the Chinese calendar using the LunarDate class. It shows how to identify leap months, convert between solar and lunar dates involving leap months, and check for the presence of leap months in specific years. ```python from lunardate import LunarDate # Working with leap months in year 2088 # Regular 4th month regular_month = LunarDate.fromSolarDate(2088, 5, 17) print(f"Regular month 4: day {regular_month.day}, isLeap: {regular_month.isLeapMonth}") # Regular month 4: day 27, isLeap: False # Leap 4th month (intercalary month) leap_month = LunarDate.fromSolarDate(2088, 6, 17) print(f"Leap month 4: day {leap_month.day}, isLeap: {leap_month.isLeapMonth}") # Leap month 4: day 28, isLeap: True # Regular 5th month (after leap month) after_leap = LunarDate.fromSolarDate(2088, 7, 17) print(f"Month 5: day {after_leap.day}, isLeap: {after_leap.isLeapMonth}") # Month 5: day 29, isLeap: False # Converting leap month dates back to solar leap_date = LunarDate(1976, 8, 8, isLeapMonth=True) solar = leap_date.toSolarDate() print(f"Leap month {leap_date.month} converts to solar: {solar}") # Leap month 8 converts to solar: 1976-10-01 # Regular vs leap month in same year regular = LunarDate(1976, 8, 8, isLeapMonth=False) leap = LunarDate(1976, 8, 8, isLeapMonth=True) diff = (leap - regular).days print(f"Days between regular and leap 8th month day 8: {diff}") # Checking which years have leap months print("\nLeap months 2020-2025:") for year in range(2020, 2026): leap_month = LunarDate.leapMonthForYear(year) if leap_month: print(f" {year}: leap month {leap_month}") else: print(f" {year}: no leap month") ``` -------------------------------- ### Create Lunar Date Object Source: https://context7.com/lidaobing/python-lunardate/llms.txt Initializes a LunarDate object with the specified year, month, day, and optional leap month indicator. The constructor validates parameters and creates an immutable representation of a Chinese lunar calendar date. ```APIDOC ## Create Lunar Date Object ### Description Initializes a `LunarDate` object with the specified year, month, day, and optional leap month indicator. The constructor validates parameters and creates an immutable representation of a Chinese lunar calendar date. ### Method `LunarDate(year, month, day, isLeapMonth=False)` ### Endpoint N/A (Library method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from lunardate import LunarDate # Create a regular lunar date lunar = LunarDate(2008, 9, 4) print(lunar) # LunarDate(2008, 9, 4, 0) # Create a leap month date leap_month_date = LunarDate(1976, 8, 8, isLeapMonth=True) print(leap_month_date) # LunarDate(1976, 8, 8, 1) # Access individual components print(f"Year: {lunar.year}") # Year: 2008 print(f"Month: {lunar.month}") # Month: 9 print(f"Day: {lunar.day}") # Day: 4 print(f"Is leap: {lunar.isLeapMonth}") # Is leap: False ``` ### Response #### Success Response (200) - **LunarDate object** (object) - A `LunarDate` object representing the specified lunar date. ``` -------------------------------- ### Comparing and Sorting Lunar Dates in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates how to compare LunarDate objects using standard comparison operators (<, >, >=, <=). It also shows how to sort a list of LunarDate objects. The library ensures type safety by raising a TypeError when comparing LunarDate objects with non-date types for ordering. ```python from lunardate import LunarDate lunar1 = LunarDate(1976, 8, 8) lunar2 = LunarDate.today() lunar3 = LunarDate(1976, 8, 8) # Less than / greater than print(f"lunar1 < lunar2: {lunar1 < lunar2}") # True (1976 is before today) print(f"lunar1 > lunar2: {lunar1 > lunar2}") # False print(f"lunar2 >= lunar1: {lunar2 >= lunar1}") # True print(f"lunar1 <= lunar3: {lunar1 <= lunar3}") # True (equal dates) # Type safety - comparing with non-LunarDate print(f"lunar1 == 5: {lunar1 == 5}") # False (no error for equality) try: result = lunar1 < 5 # TypeError for ordering comparisons except TypeError as e: print(f"Error: {e}") # Error: can't compare LunarDate to int # Sorting lunar dates dates = [ LunarDate(2000, 5, 15), LunarDate(1990, 3, 20), LunarDate(2010, 12, 1), ] sorted_dates = sorted(dates) print(f"Sorted: {sorted_dates}") ``` -------------------------------- ### Lunar Date Arithmetic and Comparison - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Demonstrates date arithmetic operations ('+', '-') between `LunarDate` objects and `datetime.date`/`datetime.timedelta`, as well as comparison operators ('<', '>', '==', '!=', '<=', '>='). This allows for calculations and comparisons of lunar dates. ```python import datetime from lunardate import LunarDate ld = LunarDate(1976, 8, 8) sd = datetime.date(2008, 1, 1) td = datetime.timedelta(days=10) # Arithmetic examples print(f"ld - ld: {ld - ld}") print(f"ld - sd: {ld - sd}") print(f"ld + td: {ld + td}") print(f"td + ld: {td + ld}") # Comparison examples ld2 = LunarDate.today() print(f"ld < ld2: {ld < ld2}") print(f"ld == ld2: {ld == ld2}") print(f"ld == ld: {ld == ld}") ``` -------------------------------- ### Access Lunar Date Components - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Illustrates how to access individual components (year, month, day, and leap month status) of a `LunarDate` object after it has been created or converted. These are direct attributes of the `LunarDate` instance. ```python from lunardate import LunarDate lunar_date = LunarDate(1976, 8, 8, 1) print(f"Year: {lunar_date.year}") print(f"Month: {lunar_date.month}") print(f"Day: {lunar_date.day}") print(f"Is Leap Month: {lunar_date.isLeapMonth}") ``` -------------------------------- ### Create Lunar Date Object in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Initializes a LunarDate object with specified year, month, day, and an optional leap month indicator. The constructor validates the provided parameters to ensure they represent a valid lunar date within the supported range. It creates an immutable representation of the date. ```python from lunardate import LunarDate # Create a regular lunar date lunar = LunarDate(2008, 9, 4) print(lunar) # LunarDate(2008, 9, 4, 0) # Create a leap month date leap_month_date = LunarDate(1976, 8, 8, isLeapMonth=True) print(leap_month_date) # LunarDate(1976, 8, 8, 1) # Access individual components print(f"Year: {lunar.year}") # Year: 2008 print(f"Month: {lunar.month}") # Month: 9 print(f"Day: {lunar.day}") # Day: 4 print(f"Is leap: {lunar.isLeapMonth}") # Is leap: False ``` -------------------------------- ### Convert Solar to Lunar Date - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Demonstrates converting a Gregorian (solar) date to its Chinese Lunar Calendar equivalent using the `LunarDate.fromSolarDate` class method. Requires importing the `LunarDate` class. ```python from lunardate import LunarDate # Convert a specific solar date solar_year, solar_month, solar_day = 1976, 10, 1 lunar_date = LunarDate.fromSolarDate(solar_year, solar_month, solar_day) print(lunar_date) ``` -------------------------------- ### Check Leap Month for Year in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates how to determine the leap month for a given year using the `LunarDate.leapMonthForYear()` method. It shows how to check for leap months, handle years with and without leap months, and catch ValueError exceptions for out-of-range years. Returns None if no leap month. ```python from lunardate import LunarDate # Year with leap month leap_1976 = LunarDate.leapMonthForYear(1976) print(f"1976 leap month: {leap_1976}") leap_2023 = LunarDate.leapMonthForYear(2023) print(f"2023 leap month: {leap_2023}") # Year without leap month leap_2022 = LunarDate.leapMonthForYear(2022) print(f"2022 leap month: {leap_2022}") # Check multiple years for year in [2020, 2021, 2022, 2023, 2024]: leap = LunarDate.leapMonthForYear(year) if leap: print(f"{year} has leap month {leap}") else: print(f"{year} has no leap month") # Error handling for out-of-range years try: LunarDate.leapMonthForYear(1899) # Before supported range except ValueError as e: print(f"Error: {e}") ``` -------------------------------- ### Perform Date Arithmetic with Timedelta in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Demonstrates how to perform addition and subtraction operations between `LunarDate` objects and `datetime.timedelta` objects. These operations automatically handle month and year boundaries, including leap months. The timedelta can be added on either side of the LunarDate object. ```python from lunardate import LunarDate import datetime # Add days to a lunar date lunar = LunarDate(1976, 8, 8) td = datetime.timedelta(days=10) result_add = lunar + td print(f"{lunar} + 10 days = {result_add}") # Timedelta can be on either side result_radd = td + lunar print(f"10 days + {lunar} = {result_radd}") # Subtract days from a lunar date result_sub = lunar - td print(f"{lunar} - 10 days = {result_sub}") # Complex arithmetic crossing year boundaries lunar_start = LunarDate(1976, 12, 25) one_month = datetime.timedelta(days=30) lunar_next = lunar_start + one_month print(f"Adding 30 days crosses year: {lunar_start} -> {lunar_next}") # Chain operations lunar_base = LunarDate(2000, 1, 1) result = lunar_base + datetime.timedelta(days=100) - datetime.timedelta(days=50) print(f"Chain operations result: {result}") ``` -------------------------------- ### Convert Lunar to Solar Date - Python Source: https://github.com/lidaobing/python-lunardate/blob/master/README.md Shows how to convert a Chinese Lunar Calendar date back to its Gregorian (solar) equivalent using the `toSolarDate` method of a `LunarDate` object. This method returns a `datetime.date` object. ```python from lunardate import LunarDate import datetime # Create a LunarDate object (e.g., 1976, August 8, Leap Month 1) lunar_date = LunarDate(1976, 8, 8, 1) # Convert to solar date solar_date = lunar_date.toSolarDate() print(solar_date) ``` -------------------------------- ### Convert Solar Date to Lunar Date Source: https://context7.com/lidaobing/python-lunardate/llms.txt Creates a LunarDate object from a Gregorian calendar date. This static method performs the conversion by calculating the offset from the library's reference date (January 31, 1900) and determining the corresponding lunar year, month, day, and leap month status. ```APIDOC ## Convert Solar Date to Lunar Date ### Description Creates a `LunarDate` object from a Gregorian calendar date. This static method performs the conversion by calculating the offset from the library's reference date (January 31, 1900) and determining the corresponding lunar year, month, day, and leap month status. ### Method `LunarDate.fromSolarDate(year, month, day)` ### Endpoint N/A (Library method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from lunardate import LunarDate # Basic conversion from solar to lunar lunar = LunarDate.fromSolarDate(1976, 10, 1) print(lunar) # LunarDate(1976, 8, 8, 1) print(f"Year: {lunar.year}, Month: {lunar.month}, Day: {lunar.day}") # Year: 1976, Month: 8, Day: 8 print(f"Is leap month: {lunar.isLeapMonth}") # Is leap month: True # Converting a regular lunar month (not leap) lunar_regular = LunarDate.fromSolarDate(2008, 10, 2) print(lunar_regular) # LunarDate(2008, 9, 4, 0) print(f"Is leap month: {lunar_regular.isLeapMonth}") # Is leap month: False # Year with leap month - before, during, and after before_leap = LunarDate.fromSolarDate(2088, 5, 17) print(f"Before leap: {before_leap.month}, isLeap: {before_leap.isLeapMonth}") # Before leap: 4, isLeap: False during_leap = LunarDate.fromSolarDate(2088, 6, 17) print(f"During leap: {during_leap.month}, isLeap: {during_leap.isLeapMonth}") # During leap: 4, isLeap: True after_leap = LunarDate.fromSolarDate(2088, 7, 17) print(f"After leap: {after_leap.month}, isLeap: {after_leap.isLeapMonth}") # After leap: 5, isLeap: False ``` ### Response #### Success Response (200) - **LunarDate object** (object) - A LunarDate object representing the converted date. ``` -------------------------------- ### Convert Solar Date to Lunar Date in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Creates a LunarDate object from a Gregorian calendar date. This method handles the conversion by calculating the offset from a reference date and determining the corresponding lunar date components, including leap month status. It supports dates within the library's range. ```python from lunardate import LunarDate # Basic conversion from solar to lunar lunar = LunarDate.fromSolarDate(1976, 10, 1) print(lunar) # LunarDate(1976, 8, 8, 1) print(f"Year: {lunar.year}, Month: {lunar.month}, Day: {lunar.day}") # Year: 1976, Month: 8, Day: 8 print(f"Is leap month: {lunar.isLeapMonth}") # Is leap month: True # Converting a regular lunar month (not leap) lunar_regular = LunarDate.fromSolarDate(2008, 10, 2) print(lunar_regular) # LunarDate(2008, 9, 4, 0) print(f"Is leap month: {lunar_regular.isLeapMonth}") # Is leap month: False # Year with leap month - before, during, and after before_leap = LunarDate.fromSolarDate(2088, 5, 17) print(f"Before leap: {before_leap.month}, isLeap: {before_leap.isLeapMonth}") # Before leap: 4, isLeap: False during_leap = LunarDate.fromSolarDate(2088, 6, 17) print(f"During leap: {during_leap.month}, isLeap: {during_leap.isLeapMonth}") # During leap: 4, isLeap: True after_leap = LunarDate.fromSolarDate(2088, 7, 17) print(f"After leap: {after_leap.month}, isLeap: {after_leap.isLeapMonth}") # After leap: 5, isLeap: False ``` -------------------------------- ### Convert Lunar Date to Solar Date Source: https://context7.com/lidaobing/python-lunardate/llms.txt Converts a LunarDate object back to a standard Python datetime.date object. This method validates the lunar date parameters and calculates the corresponding Gregorian date. Raises ValueError if the lunar date is invalid. ```APIDOC ## Convert Lunar Date to Solar Date ### Description Converts a `LunarDate` object back to a standard Python `datetime.date` object. This method validates the lunar date parameters and calculates the corresponding Gregorian date. Raises `ValueError` if the lunar date is invalid (year out of range, invalid month, or day exceeding the month's length). ### Method `LunarDate.toSolarDate()` ### Endpoint N/A (Library method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from lunardate import LunarDate import datetime # Basic conversion from lunar to solar lunar = LunarDate(1976, 8, 8, isLeapMonth=True) solar = lunar.toSolarDate() print(solar) # 1976-10-01 print(type(solar)) # # Regular month conversion lunar_regular = LunarDate(2008, 9, 4) solar_regular = lunar_regular.toSolarDate() print(solar_regular) # 2008-10-02 # Error handling for invalid dates try: invalid_lunar = LunarDate(2004, 1, 30) # Day 30 doesn't exist in this month invalid_lunar.toSolarDate() except ValueError as e: print(f"Error: {e}") # Error: day out of range try: invalid_year = LunarDate(2100, 1, 1) # Year out of supported range invalid_year.toSolarDate() except ValueError as e: print(f"Error: {e}") # Error: year out of range [1900, 2100) ``` ### Response #### Success Response (200) - **datetime.date object** (object) - A `datetime.date` object representing the converted Gregorian date. #### Error Response (400) - **ValueError**: Raised if the lunar date is invalid. ``` -------------------------------- ### Convert Lunar Date to Solar Date in Python Source: https://context7.com/lidaobing/python-lunardate/llms.txt Converts a LunarDate object back to a standard Python datetime.date object. This method validates the lunar date parameters and calculates the corresponding Gregorian date. It raises a ValueError for invalid lunar dates, such as years out of range or invalid day/month combinations. ```python from lunardate import LunarDate import datetime # Basic conversion from lunar to solar lunar = LunarDate(1976, 8, 8, isLeapMonth=True) solar = lunar.toSolarDate() print(solar) # 1976-10-01 print(type(solar)) # # Regular month conversion lunar_regular = LunarDate(2008, 9, 4) solar_regular = lunar_regular.toSolarDate() print(solar_regular) # 2008-10-02 # Error handling for invalid dates try: invalid_lunar = LunarDate(2004, 1, 30) # Day 30 doesn't exist in this month invalid_lunar.toSolarDate() except ValueError as e: print(f"Error: {e}") # Error: day out of range try: invalid_year = LunarDate(2100, 1, 1) # Year out of supported range invalid_year.toSolarDate() except ValueError as e: print(f"Error: {e}") # Error: year out of range [1900, 2100) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.