korean-lunar-calendar: Korean Lunar-Solar Calendar Converter
A Rust library for converting between Korean Lunar dates and Gregorian Solar dates. It also calculates the traditional Korean and Chinese Gapja (sexagenary cycle) names for the year, month, and day.
This library is based on the data and algorithms originally implemented in JavaScript by usingsky and adapted for Rust.
Features
- Lunar to Solar Conversion: Convert a Korean Lunar date (including intercalary months) to the corresponding Gregorian Solar date.
- Solar to Lunar Conversion: Convert a Gregorian Solar date to the corresponding Korean Lunar date.
- Gapja Calculation: Calculate the Korean (
갑자) and Chinese (干支) sexagenary cycle names for the year, month, and day of a given date. - Julian Day Number (JDN): Calculate the JDN for a solar date.
- Day of the Week: Determine the day of the week (Monday-Sunday) for a solar date.
- Solar Leap Year Check: Check if a given solar year is a leap year (handling Julian/Gregorian rules).
- Lunar Intercalary Month Check: Check if a given lunar year contains an intercalary month (윤달) and which month it is.
- Date Validation: Checks if the provided dates are within the supported range and handles historical anomalies like the Gregorian calendar reform gap in October 1582.
- Intercalary Month Handling: Correctly identifies and processes intercalary (leap) months in the Lunar calendar during conversions.
- ISO Formatting: Provides simple ISO 8601 format output (
YYYY-MM-DD) for both Lunar and Solar dates.
See the Examples section for code demonstrating these features.
Supported Date Range
- Lunar: 1391-01-01 to 2050-11-18
- Solar: 1391-02-05 to 2050-12-31
(Note: Dates are based on the Korean time zone (KST) implicitly via the underlying data/algorithm design.)
Usage
Add this to your Cargo.toml:
[]
= "0.1" # Check crates.io for the latest version
Then use the library like this:
use ;
let mut converter = new;
// Set a Solar date and get Lunar info
if converter.set_solar_date else
println!;
// Check solar leap year
let solar_year = 2024;
let is_solar_leap = is_solar_leap_year;
println!; // Output: true
// Check lunar intercalary month
let lunar_year = 2023;
if let Some = get_lunar_intercalary_month else
// Calculate JDN
if let Some = get_julian_day_number
Examples
The examples/ directory contains several examples demonstrating different features of the library. You can run them using cargo or task:
01_basic_conversion: Basic Solar <-> Lunar conversion02_lunar_to_solar: Lunar to Solar conversion03_intercalary_month: Intercalary month handling04_gapja_sexagenary: Korean/Chinese Gapja calculation05_julian_day_number: Julian Day Number calculation06_leap_year: Solar leap year check07_day_of_week: Day of week calculation08_comprehensive: Comprehensive feature demo
Run all examples:
Building and Testing
This project uses cargo for building and testing, and task (Taskfile) for convenience.
- Build:
# or - Test:
# or - Run Examples:
# or - Lint:
# or
License
This project is licensed under the MIT License. See the LICENSE file for details.