### Install ZhDate using pip Source: https://github.com/cutepandash/zhdate/blob/master/README.md Install the zhdate library using pip for quick setup. ```bash pip install zhdate ``` -------------------------------- ### Install ZhDate from Git Source: https://github.com/cutepandash/zhdate/blob/master/README.md Clone the repository and install zhdate locally from the source code. ```bash git clone https://github.com/CutePandaSh/zhdate.git cd zhdate python setup.py install ``` -------------------------------- ### Install ZhDate using pip Source: https://context7.com/cutepandash/zhdate/llms.txt Install the ZhDate library using pip. This is the recommended method for most users. ```bash pip install zhdate ``` ```bash pip install zhdate --upgrade ``` -------------------------------- ### Upgrade ZhDate using pip Source: https://github.com/cutepandash/zhdate/blob/master/README.md Upgrade an existing installation of the zhdate library to the latest version. ```bash pip install zhdate --upgrade ``` -------------------------------- ### Get today's lunar date Source: https://context7.com/cutepandash/zhdate/llms.txt Use the static method `ZhDate.today()` to get the current system date as a ZhDate object. This is a convenient shortcut for `ZhDate.from_datetime(datetime.now())`. ```python from zhdate import ZhDate # Get today's lunar date today = ZhDate.today() print(today) # Example Output: 农历2024年11月15日 # Combine with Chinese formatting print(today.chinese()) # Example Output: 二〇二四年十一月十五 甲辰龙年 ``` -------------------------------- ### Get Month Days for a Lunar Year with ZhDate.month_days Source: https://context7.com/cutepandash/zhdate/llms.txt ZhDate.month_days is a convenient static method that directly returns a list of days for each month of a specified lunar year. It is a wrapper around the decode method. ```python from zhdate import ZhDate # Get month days for 2020 (Gengzi Rat Year, with leap April) months_2020 = ZhDate.month_days(2020) print(f"2020 has {len(months_2020)} months") print(f"Days per month: {months_2020}") # Output: # 2020 has 13 months # Days per month: [30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30] # Get month days for 2019 (Jihai Pig Year, no leap month) months_2019 = ZhDate.month_days(2019) print(f"2019 has {len(months_2019)} months") # Output: 2019 has 12 months ``` -------------------------------- ### Get Today's Lunar Date Source: https://github.com/cutepandash/zhdate/blob/master/README.md Retrieves the current day's date in the Chinese lunar calendar. ```python from zhdate import ZhDate # 当天的农历日期 ZhDate.today() ``` -------------------------------- ### Get Chinese formatted lunar date string Source: https://context7.com/cutepandash/zhdate/llms.txt Use the `chinese()` method to get a full Chinese string representation of the lunar date, including the year (with Heavenly Stems and Earthly Branches), zodiac animal, month (indicating leap month), and day. ```python from zhdate import ZhDate # Pig Year, January 1 date1 = ZhDate(2019, 1, 1) print(date1.chinese()) # Output: 二〇一九年正月初一 己亥猪年 # Rat Year, leap April 29 date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap.chinese()) # Output: 二〇二〇年闰四月廿九 庚子鼠年 # Historical date: September 1, 1900 (Rat Year) date_hist = ZhDate(1900, 9, 1, False) print(date_hist.chinese()) # Output: 一九〇〇年九月初一 庚子鼠年 ``` -------------------------------- ### Convert a lunar date to a Gregorian datetime object Source: https://context7.com/cutepandash/zhdate/llms.txt Use the `to_datetime()` method on a ZhDate object to get the corresponding `datetime` object in the Gregorian calendar. This is useful for integrating with other date/time functionalities. ```python from zhdate import ZhDate # Lunar January 1, 2010 -> Gregorian date1 = ZhDate(2010, 1, 1) gregorian = date1.to_datetime() print(gregorian) # Output: 2010-02-14 00:00:00 # Lunar leap April 29, 2020 -> Gregorian date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap.to_datetime()) # Output: 2020-06-20 00:00:00 # Lunar November 27, 2100 -> Gregorian date_far = ZhDate(2100, 11, 27) print(date_far.to_datetime()) # Output: 2100-12-27 00:00:00 ``` -------------------------------- ### Get Chinese Representation of Lunar Date Source: https://github.com/cutepandash/zhdate/blob/master/README.md Retrieves the Chinese string representation of a ZhDate object. ```python from zhdate import ZhDate # 中文输出 new_zhdate5 = ZhDate(2019, 1, 1) print(new_zhdate5.chinese()) ``` -------------------------------- ### Create and Print Lunar Date Object Source: https://github.com/cutepandash/zhdate/blob/master/README.md Demonstrates creating a ZhDate object for a specific lunar date and printing its string representation. ```python from zhdate import ZhDate date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 print(date1) # 直接返回农历日期字符串 ``` -------------------------------- ### ZhDate.today() Source: https://context7.com/cutepandash/zhdate/llms.txt A static method that returns the ZhDate object for the current system date. It is equivalent to ZhDate.from_datetime(datetime.now()). ```APIDOC ## ZhDate.today() ### Description A static method that returns the ZhDate object for the current system date. It is equivalent to ZhDate.from_datetime(datetime.now()). ### Method Static method ### Parameters None ### Request Example ```python from zhdate import ZhDate # Get today's lunar date today = ZhDate.today() print(today) # Combine with Chinese formatting print(today.chinese()) ``` ### Response Example ``` # Example output for today (will vary): 农历2024年11月15日 # Example output for today.chinese(): 二〇二四年十一月十五 甲辰龙年 ``` ``` -------------------------------- ### Date Arithmetic (__add__ and __sub__) Source: https://context7.com/cutepandash/zhdate/llms.txt ZhDate supports addition and subtraction with integers, other ZhDate objects, and datetime objects. Adding an integer returns a new ZhDate. Subtracting two ZhDate or a ZhDate and a datetime returns the difference in days as an integer. ```APIDOC ## Date Arithmetic (__add__ and __sub__) ### Description ZhDate supports addition and subtraction with integers, other ZhDate objects, and datetime objects. Adding an integer returns a new ZhDate. Subtracting two ZhDate or a ZhDate and a datetime returns the difference in days as an integer. ### Method Instance methods ### Parameters - For addition/subtraction with an integer: An integer representing the number of days to add or subtract. - For subtraction with another ZhDate or datetime: The other date object. ### Request Example ```python from zhdate import ZhDate from datetime import datetime base = ZhDate(2019, 1, 1) # Lunar January 1st, 2019 # Add 30 days to the base date result_add = base + 30 print(result_add) # Subtract 30 days from the base date result_sub_int = base - 30 print(result_sub_int) # Calculate the difference in days between two ZhDate objects diff_zhdate = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) print(diff_zhdate) # Calculate the difference in days between a ZhDate and a datetime object diff_dt = ZhDate(2019, 1, 1) - datetime(2018, 2, 16) print(diff_dt) ``` ### Response Example ``` # Output for result_add: 农历2019年2月1日 # Output for result_sub_int: 农历2018年12月2日 # Output for diff_zhdate: 354 # Output for diff_dt: 354 ``` ``` -------------------------------- ### Create Lunar Date with Leap Month Source: https://github.com/cutepandash/zhdate/blob/master/README.md Demonstrates creating a ZhDate object for a leap month in the lunar calendar and converting it to a datetime object. ```python from zhdate import ZhDate date3 = ZhDate(2020, 4, 30, leap_month=True) # 新建农历 2020年闰4月30日 print(date3.to_datetime()) ``` -------------------------------- ### Date Comparison (__eq__) Source: https://context7.com/cutepandash/zhdate/llms.txt ZhDate objects can be compared for equality using the == operator. This checks if the year, month, day, and leap month status are identical. ```APIDOC ## Date Comparison (__eq__) ### Description ZhDate objects can be compared for equality using the == operator. This checks if the year, month, day, and leap month status are identical. ### Method Instance method ### Parameters - The other object to compare with (should be a ZhDate instance for equality). ### Request Example ```python from zhdate import ZhDate from datetime import datetime # Verify: Lunar January 1st, 2019 == Gregorian February 5th, 2019 lunar = ZhDate(2019, 1, 1) from_gregorian = ZhDate.from_datetime(datetime(2019, 2, 5)) if lunar == from_gregorian: print("Dates match, conversion is correct!") # Leap month vs. non-leap month comparison date_normal = ZhDate(2020, 4, 29, leap_month=False) date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_normal == date_leap) # Comparison with a non-ZhDate type will raise a TypeError try: result = ZhDate(2019, 1, 1) == "2019-01-01" except TypeError as e: print(f"Type error: {e}") ``` ### Response Example ``` # Output for equality check: 日期一致,转换正确! # Output for leap vs. non-leap: False # Output for type error: 类型错误: 比较必须都是ZhDate类型 ``` ``` -------------------------------- ### Compare Lunar Dates Source: https://github.com/cutepandash/zhdate/blob/master/README.md Shows how to compare two ZhDate objects for equality, including conversion from datetime objects. ```python from zhdate import ZhDate from datetime import datetime # 支持比较 if ZhDate(2019, 1, 1) == ZhDate.from_datetime(datetime(2019, 2, 5)): pass ``` -------------------------------- ### Create a ZhDate object for a normal lunar date Source: https://context7.com/cutepandash/zhdate/llms.txt Create a ZhDate instance by providing the lunar year, month, and day. Ensure the date is within the supported range (1900-2100). ```python from zhdate import ZhDate # Create a normal lunar date: January 1, 2019 (Lunar New Year of the Pig) date1 = ZhDate(2019, 1, 1) print(date1) # Output: 农历2019年1月1日 ``` -------------------------------- ### Perform date arithmetic with ZhDate objects Source: https://context7.com/cutepandash/zhdate/llms.txt ZhDate supports addition and subtraction with integers, other ZhDate objects, and datetime objects. Adding an integer returns a new ZhDate, while subtracting two dates returns the difference in days. ```python from zhdate import ZhDate from datetime import datetime base = ZhDate(2019, 1, 1) # Lunar January 1, 2019 # Add 30 days to the base date result_add = base + 30 print(result_add) # Output: 农历2019年2月1日 # Subtract 30 days from the base date result_sub_int = base - 30 print(result_sub_int) # Output: 农历2018年12月2日 # Subtract two ZhDate objects to get the difference in days diff_zhdate = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) print(diff_zhdate) # Output: 354 # Subtract a datetime object from a ZhDate object to get the difference in days diff_dt = ZhDate(2019, 1, 1) - datetime(2018, 2, 16) print(diff_dt) # Output: 354 ``` -------------------------------- ### ZhDate.month_days(year) Source: https://context7.com/cutepandash/zhdate/llms.txt A static method that directly returns a list of days for each month of a given lunar calendar year. It is a convenient wrapper around the `decode` method. ```APIDOC ## ZhDate.month_days(year) ### Description A static method that directly returns a list of days for each month of a given lunar calendar year. It is a convenient wrapper around the `decode` method. ### Method `static method` ### Parameters #### Path Parameters - **year** (int) - Required - The lunar calendar year for which to retrieve the monthly days. ### Request Example ```python from zhdate import ZhDate # Get days for 2020 (Gengzi Rat Year, with leap April) months_2020 = ZhDate.month_days(2020) print(f"2020 has {len(months_2020)} months") print(f"Days per month: {months_2020}") # Output: # 2020 has 13 months # Days per month: [30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30] # Get days for 2019 (Jihai Pig Year, no leap month) months_2019 = ZhDate.month_days(2019) print(f"2019 has {len(months_2019)} months") # Output: 2019 has 12 months ``` ### Response #### Success Response - Returns a list of integers representing the number of days in each month of the specified lunar year. The list length is 13 for leap years and 12 for non-leap years. ``` -------------------------------- ### ZhDate(lunar_year, lunar_month, lunar_day, leap_month=False) Source: https://context7.com/cutepandash/zhdate/llms.txt Constructs a ZhDate instance representing a Chinese lunar date. It supports dates from 1900 to 2100 and handles leap months. Invalid dates will raise a TypeError. ```APIDOC ## ZhDate(lunar_year, lunar_month, lunar_day, leap_month=False) ### Description Constructs a ZhDate instance representing a Chinese lunar date. It supports dates from 1900 to 2100 and handles leap months. Invalid dates will raise a TypeError. ### Parameters - **lunar_year** (int) - Required - The lunar year. - **lunar_month** (int) - Required - The lunar month. - **lunar_day** (int) - Required - The lunar day. - **leap_month** (bool) - Optional - Defaults to False. Indicates if the month is a leap month. ### Request Example ```python from zhdate import ZhDate # Create a normal lunar date: January 1st, 2019 (Chinese New Year) date1 = ZhDate(2019, 1, 1) print(date1) # Create a lunar date with a leap month: Leap April 29th, 2020 date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap) # Invalid dates will raise an exception try: invalid = ZhDate(2020, 3, 30, leap_month=True) # 2020 does not have a leap March except TypeError as e: print(f"Error: {e}") ``` ### Response Example ``` # Output for date1: 农历2019年1月1日 # Output for date_leap: 农历2020年闰4月29日 # Output for invalid date: 错误: 农历日期不支持所谓"农历2020年闰3月30日",超出农历1900年1月1日至2100年12月29日,或日期不存在 ``` ``` -------------------------------- ### Decode Lunar Year Code with ZhDate.decode Source: https://context7.com/cutepandash/zhdate/llms.txt Use ZhDate.decode to parse a 20-bit binary year code into a list of days per month. The output list will have 13 elements for leap years and 12 for non-leap years. This method is typically for internal use but can reveal month-day distributions. ```python from zhdate import ZhDate from zhdate.constants import CHINESEYEARCODE # Parse 1900 (year code 19416, with leap August) months_1900 = ZhDate.decode(19416) print(months_1900) # Output: [29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30] # 13 months # Parse 1901 (year code 19168, no leap month) months_1901 = ZhDate.decode(19168) print(months_1901) # Output: [29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29] # 12 months # Get month days dynamically by year year = 2023 code = CHINESEYEARCODE[year - 1900] months = ZhDate.decode(code) print(f"{year} has {len(months)} months, days per month: {months}") ``` -------------------------------- ### Create a ZhDate object for a leap lunar date Source: https://context7.com/cutepandash/zhdate/llms.txt Create a ZhDate instance for a leap month by setting `leap_month=True`. The library will validate if the specified leap month is valid for the given year. ```python from zhdate import ZhDate # Create a lunar date with a leap month: April 29, 2020 (leap month) date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap) # Output: 农历2020年闰4月29日 ``` -------------------------------- ### ZhDate.to_datetime() Source: https://context7.com/cutepandash/zhdate/llms.txt Converts a ZhDate object to a Python datetime object, representing the corresponding Gregorian calendar date. ```APIDOC ## ZhDate.to_datetime() ### Description Converts a ZhDate object to a Python datetime object, representing the corresponding Gregorian calendar date. ### Method Instance method ### Parameters None ### Request Example ```python from zhdate import ZhDate # Lunar January 1st, 2010 to Gregorian date1 = ZhDate(2010, 1, 1) gregorian = date1.to_datetime() print(gregorian) # Lunar Leap April 29th, 2020 to Gregorian date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap.to_datetime()) # Lunar November 27th, 2100 to Gregorian date_far = ZhDate(2100, 11, 27) print(date_far.to_datetime()) ``` ### Response Example ``` # Output for date1: 2010-02-14 00:00:00 # Output for date_leap: 2020-06-20 00:00:00 # Output for date_far: 2100-12-27 00:00:00 ``` ``` -------------------------------- ### Compare ZhDate objects for equality Source: https://context7.com/cutepandash/zhdate/llms.txt Use the equality operator `==` to compare two ZhDate objects. The comparison checks year, month, day, and leap month status. Comparing with non-ZhDate types will raise a TypeError. ```python from zhdate import ZhDate from datetime import datetime # Verify: Lunar January 1, 2019 == Gregorian February 5, 2019 lunar = ZhDate(2019, 1, 1) from_gregorian = ZhDate.from_datetime(datetime(2019, 2, 5)) if lunar == from_gregorian: print("Dates match, conversion is correct!") # Output: Dates match, conversion is correct! # Leap month and non-leap month are not equal date_normal = ZhDate(2020, 4, 29, leap_month=False) date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_normal == date_leap) # Output: False # Comparison with non-ZhDate type will raise an exception try: result = ZhDate(2019, 1, 1) == "2019-01-01" except TypeError as e: print(f"Type error: {e}") # Output: Type error: 比较必须都是ZhDate类型 ``` -------------------------------- ### ZhDate.from_datetime(dt) Source: https://context7.com/cutepandash/zhdate/llms.txt A static method that takes a datetime object and returns the corresponding ZhDate lunar date instance. ```APIDOC ## ZhDate.from_datetime(dt) ### Description A static method that takes a datetime object and returns the corresponding ZhDate lunar date instance. ### Method Static method ### Parameters - **dt** (datetime) - Required - The datetime object to convert. ### Request Example ```python from zhdate import ZhDate from datetime import datetime # Gregorian February 5th, 2019 to Lunar (Chinese New Year, 1st day of 1st month, Year of the Pig) dt = datetime(2019, 2, 5) lunar = ZhDate.from_datetime(dt) print(lunar) # Gregorian July 11th, 1903 to Lunar (with leap month) dt2 = datetime(1903, 7, 11) lunar2 = ZhDate.from_datetime(dt2) print(lunar2) # Gregorian February 19th, 2050 to Lunar dt3 = datetime(2050, 2, 19) print(ZhDate.from_datetime(dt3)) ``` ### Response Example ``` # Output for dt: 农历2019年1月1日 # Output for dt2: 农历1903年5月17日(闰月) # Output for dt3: 农历2050年1月28日 ``` ``` -------------------------------- ### ZhDate.decode(year_code) Source: https://context7.com/cutepandash/zhdate/llms.txt Decodes a 20-bit binary year code into a list of days for each month. Returns a list of length 13 for leap years and 12 for non-leap years. This method is typically for internal use but can be used to understand the distribution of days in a lunar year. ```APIDOC ## ZhDate.decode(year_code) ### Description Decodes a 20-bit binary year code into a list of days for each month. Returns a list of length 13 for leap years and 12 for non-leap years. This method is typically for internal use but can be used to understand the distribution of days in a lunar year. ### Method `static method` ### Parameters #### Path Parameters - **year_code** (int) - Required - The 20-bit binary year code to decode. ### Request Example ```python from zhdate import ZhDate from zhdate.constants import CHINESEYEARCODE # Decode year code for 1900 (leap August) months_1900 = ZhDate.decode(19416) print(months_1900) # Output: [29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30] # Decode year code for 1901 (no leap month) months_1901 = ZhDate.decode(19168) print(months_1901) # Output: [29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29] # Dynamically get month days by year year = 2023 code = CHINESEYEARCODE[year - 1900] months = ZhDate.decode(code) print(f"{year} has {len(months)} months, days per month: {months}") ``` ### Response #### Success Response - Returns a list of integers representing the number of days in each month of the lunar year. The list length is 13 for leap years and 12 for non-leap years. ``` -------------------------------- ### ZhDate.chinese() Source: https://context7.com/cutepandash/zhdate/llms.txt Returns a formatted Chinese string representation of the lunar date, including the year (with Heavenly Stems and Earthly Branches), zodiac sign, month (indicating leap months), and day. ```APIDOC ## ZhDate.chinese() ### Description Returns a formatted Chinese string representation of the lunar date, including the year (with Heavenly Stems and Earthly Branches), zodiac sign, month (indicating leap months), and day. ### Method Instance method ### Parameters None ### Request Example ```python from zhdate import ZhDate # Year of the Pig, 1st day of 1st month date1 = ZhDate(2019, 1, 1) print(date1.chinese()) # Year of the Rat, Leap 4th month, 29th day date_leap = ZhDate(2020, 4, 29, leap_month=True) print(date_leap.chinese()) # Historical date: Year of the Rat (1900), 1st day of 9th month date_hist = ZhDate(1900, 9, 1, False) print(date_hist.chinese()) ``` ### Response Example ``` # Output for date1: 二〇一九年正月初一 己亥猪年 # Output for date_leap: 二〇二〇年闰四月廿九 庚子鼠年 # Output for date_hist: 一九〇〇年九月初一 庚子鼠年 ``` ``` -------------------------------- ### Create Lunar Date from Datetime Object Source: https://github.com/cutepandash/zhdate/blob/master/README.md Creates a ZhDate object from a given Python datetime object. ```python from zhdate import ZhDate from datetime import datetime dt_date2 = datetime(2010, 2, 6) date2 = ZhDate.from_datetime(dt_date2) # 从阳历日期转换成农历日期对象 ``` -------------------------------- ### Calculate Difference Between Lunar and Gregorian Dates Source: https://github.com/cutepandash/zhdate/blob/master/README.md Calculates the difference in days between a ZhDate object and a datetime object, returning the result as an integer. ```python from zhdate import ZhDate from datetime import datetime # 减法支持 new_zhdate3 = ZhDate(2019, 1, 1) - datetime(2019, 1, 1) # 减去阳历日期,得到农历日期和阳历日期之间的天数差额 ``` -------------------------------- ### Handle invalid lunar date creation Source: https://context7.com/cutepandash/zhdate/llms.txt Attempting to create a ZhDate with an invalid or non-existent lunar date (e.g., a leap month that doesn't exist) will raise a TypeError. Catch this exception to handle invalid inputs gracefully. ```python from zhdate import ZhDate # Invalid date will raise an exception try: invalid = ZhDate(2020, 3, 30, leap_month=True) # 2020 does not have a leap March except TypeError as e: print(f"Error: {e}") # Output: Error: 农历日期不支持所谓"农历2020年闰3月30日",超出农历1900年1月1日至2100年12月29日,或日期不存在 ``` -------------------------------- ### Convert Lunar Date to Datetime Object Source: https://github.com/cutepandash/zhdate/blob/master/README.md Converts a ZhDate object to a standard Python datetime object. ```python from zhdate import ZhDate date1 = ZhDate(2010, 1, 1) # 新建农历 2010年正月初一 的日期对象 dt_date1 = date1.to_datetime() # 农历转换成阳历日期 datetime 类型 ``` -------------------------------- ### Convert a Gregorian datetime object to a ZhDate object Source: https://context7.com/cutepandash/zhdate/llms.txt Use the static method `ZhDate.from_datetime(dt)` to convert a Python `datetime` object into a ZhDate object. This is useful for processing external date inputs. ```python from zhdate import ZhDate from datetime import datetime # Gregorian February 5, 2019 -> Lunar (Lunar New Year, Pig Year) dt = datetime(2019, 2, 5) lunar = ZhDate.from_datetime(dt) print(lunar) # Output: 农历2019年1月1日 # Gregorian July 11, 1903 -> Lunar (with leap month) dt2 = datetime(1903, 7, 11) lunar2 = ZhDate.from_datetime(dt2) print(lunar2) # Output: 农历1903年5月17日(闰月) # Gregorian February 19, 2050 -> Lunar dt3 = datetime(2050, 2, 19) print(ZhDate.from_datetime(dt3)) # Output: 农历2050年1月28日 ``` -------------------------------- ### Calculate Difference Between Two Lunar Dates Source: https://github.com/cutepandash/zhdate/blob/master/README.md Calculates the difference in days between two ZhDate objects, returning the result as an integer. ```python from zhdate import ZhDate # 减法支持 new_zhdate2 = ZhDate(2019, 1, 1) - ZhDate(2018, 1, 1) #两个zhdate对象相减得到两个农历日期的差额 ``` -------------------------------- ### ZhDate.validate(year, month, day, leap) Source: https://context7.com/cutepandash/zhdate/llms.txt A static method to validate if a given lunar year, month, day, and leap month flag represent a valid date within the supported range (1900-2100). Returns True if valid, False otherwise. ```APIDOC ## ZhDate.validate(year, month, day, leap) ### Description A static method to validate if a given lunar year, month, day, and leap month flag represent a valid date within the supported range (1900-2100). Returns True if valid, False otherwise. This can be used for pre-validation before constructing a ZhDate object. ### Method Static method ### Parameters - **year** (int) - Required - The lunar year to validate. - **month** (int) - Required - The lunar month to validate. - **day** (int) - Required - The lunar day to validate. - **leap** (bool) - Required - Indicates if the month is a leap month. ### Request Example ```python from zhdate import ZhDate # Valid dates print(ZhDate.validate(1900, 11, 1, False)) # True print(ZhDate.validate(2020, 4, 29, True)) # True (2020 has a leap 4th month) print(ZhDate.validate(2100, 12, 29, False)) # True # Invalid dates print(ZhDate.validate(2020, 3, 30, True)) # False (2020 does not have a leap 3rd month) print(ZhDate.validate(2020, 4, 30, True)) # False (Leap 4th month in 2020 only has 29 days) print(ZhDate.validate(1909, 1, 29, True)) # False (1909's leap month is the 2nd month, not the 1st) # Batch validation example dates_to_check = [ (2024, 1, 1, False), (2024, 12, 30, False), (2023, 2, 30, True), # Invalid day for a leap 2nd month ] for d in dates_to_check: print(f"ZhDate.validate{d} => {ZhDate.validate(*d)}") ``` ### Response Example ``` # Output for valid dates: True True True # Output for invalid dates: False False False # Output for batch validation: ZhDate.validate(2024, 1, 1, False) => True ZhDate.validate(2024, 12, 30, False) => True ZhDate.validate(2023, 2, 30, True) => False ``` ``` -------------------------------- ### Validate a lunar date using ZhDate.validate Source: https://context7.com/cutepandash/zhdate/llms.txt Use the static method `ZhDate.validate(year, month, day, leap)` to check if a given lunar year, month, day, and leap status combination is valid within the library's supported range and rules. This can prevent errors during object creation. ```python from zhdate import ZhDate # Valid dates print(ZhDate.validate(1900, 11, 1, False)) # True print(ZhDate.validate(2020, 4, 29, True)) # True (2020 has a leap April) print(ZhDate.validate(2100, 12, 29, False)) # True # Invalid dates print(ZhDate.validate(2020, 3, 30, True)) # False (2020 has no leap March) print(ZhDate.validate(2020, 4, 30, True)) # False (leap April in 2020 only has 29 days) print(ZhDate.validate(1909, 1, 29, True)) # False (1909 leap month is February, not January) # Batch validation example dates_to_check = [ (2024, 1, 1, False), (2024, 12, 30, False), (2023, 2, 30, True), ] for d in dates_to_check: print(f"ZhDate.validate{d} => {ZhDate.validate(*d)}") # Output: # ZhDate.validate(2024, 1, 1, False) => True # ZhDate.validate(2024, 12, 30, False) => True # ZhDate.validate(2023, 2, 30, True) => False ``` -------------------------------- ### Subtract Days from Lunar Date Source: https://github.com/cutepandash/zhdate/blob/master/README.md Subtracts a specified number of days from a ZhDate object, returning a new ZhDate object. ```python from zhdate import ZhDate # 减法支持 new_zhdate = ZhDate(2019, 1, 1) - 30 #减整数,得到差额天数的新农历对象 ``` -------------------------------- ### Add Days to Lunar Date Source: https://github.com/cutepandash/zhdate/blob/master/README.md Adds a specified number of days to a ZhDate object, returning a new ZhDate object representing the future date. ```python from zhdate import ZhDate # 加法支持 new_zhdate4 = ZhDate(2019, 1, 1) + 30 # 加整数返回相隔天数以后的新农历对象 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.