A Chinese lunar date library for Python 中国农历日期 Python 库
cnlunardate provides a date type for the Chinese lunar calendar, with functionality similar to Python's built-in datetime.date.
Features
- Construct and manipulate Chinese lunar dates.
- Convert between Chinese lunar dates and Gregorian dates.
- Support leap months.
- Date arithmetic, comparisons, and ordinal conversions.
- Immutable date objects.
- Pickling support.
Requirements
Python 3.11 or later.
Installation
$ python -m pip install cnlunardate
Usage
Basic operations:
>>> from cnlunardate import cnlunardate
>>> # Basic constructors
>>> cnlunardate(2017, 6, 1) # 4th attribute defaults to False (month is not leap)
cnlunardate.cnlunardate(2017, 6, 1, False)
>>> cnlunardate(2017, 6, 1, True) # (month is leap)
cnlunardate.cnlunardate(2017, 6, 1, True)
>>> # Conversions from/to solar date
>>> from datetime import date
>>> d = cnlunardate.fromsolardate(date(2017, 6, 24))
>>> d
cnlunardate.cnlunardate(2017, 6, 1, False)
>>> d.tosolardate()
datetime.date(2017, 6, 24)
Other supported operations as datetime.date (including pickling):
>>> d = cnlunardate.fromordinal(736504) # 736504th day after 0001, 1, 1
>>> d
cnlunardate.cnlunardate(2017, 6, 1, False)
>>> d.toordinal()
736504
>>> # A cnlunardate object is immutable; all operations produce a new object
>>> d.replace(year=2018)
cnlunardate.cnlunardate(2018, 6, 1, False)
>>> d.replace(isLeapMonth=True)
cnlunardate.cnlunardate(2017, 6, 1, True)
>>> d
cnlunardate.cnlunardate(2017, 6, 1, False)
>>> d.weekday() # 0 = Monday
5
>>> d.isoweekday() # 1 = Monday
6
>>> t = d.timetuple()
>>> for i in t:
... print(i)
...
2017 # year
6 # month
24 # day
0
0
0
5 # weekday (0 = Monday)
175 # 175th day in the year
-1
>>> ic = d.isocalendar()
>>> for i in ic:
... print(i)
...
2017 # ISO year
25 # ISO week number
6 # ISO day number ( 1 = Monday )
>>> today = cnlunardate.today()
>>> today
cnlunardate.cnlunardate(2019, 11, 22, False)
>>> import time
>>> today == cnlunardate.fromtimestamp(time.time())
True
>>> cnlunardate.min
cnlunardate.cnlunardate(1900, 1, 1, False)
>>> cnlunardate.max
cnlunardate.cnlunardate(2100, 12, 1, False)
Errors:
>>> cnlunardate(2101, 1, 1)
ValueError: year 2101 must be in 1900..2100
>>> cnlunardate(2017, 13, 1)
ValueError: month 13 must be in 1..12
>>> cnlunardate(2017, 1, 30)
ValueError: day 30 must be in 1..29
>>> cnlunardate(2017, 1, 1, True)
ValueError: month 1 is not leap in 2017
>>> from datetime import timedelta
>>> cnlunardate.min - timedelta(days=1)
OverflowError: result out of range
>>> cnlunardate.max + timedelta(days=1)
OverflowError: result out of range
>>> cnlunardate("Hello")
TypeError: an integer is required (got type str)
Development
Clone the repository:
git clone https://github.com/YuBPan/cnlunardate.git
cd cnlunardate
Use a virtual environment, install the package and development dependencies:
python -m venv path/to/venv
source path/to/venv/bin/activate
python -m pip install -e ".[dev]"
Run tests
python -m pytest
Lint and format checks
python -m ruff check .
python -m ruff format --check .
License
MIT
Reference
Metadata
Release files for cnlunardate 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cnlunardate-0.1.0.tar.gz | 15.1 kB | With provenance | Details for cnlunardate-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cnlunardate-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for cnlunardate-0.1.0-py3-none-any.whl |
Total release size: 23.3 kB
Release files / cnlunardate-0.1.0.tar.gz
| Download URL | cnlunardate-0.1.0.tar.gz |
|---|---|
| Size | 15.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea8731db5c742dd4c3020513f611a9a45857284df99f642e543919f71bafc382
|
|
BLAKE2b-256 checksum How to use checksums |
70180ab74ca86280d084acedcff2361b162b2e7727e6be9fdf6700df615fd085
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logRelease files / cnlunardate-0.1.0-py3-none-any.whl
| Download URL | cnlunardate-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
73c933fa85fb497eb9f024f4090e00dde41b085f32def34a28b3b6b0f1d549e4
|
|
BLAKE2b-256 checksum How to use checksums |
333fc20fc80ee4dfbcac74690f83c2a1848ebd7bc98bd6b12f9e197344c1712d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logSecurity for cnlunardate 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for cnlunardate-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.