feat(saju-lab): lunar.py — 음력↔양력 변환 (sxtwl)
This commit is contained in:
23
saju-lab/app/calculator/lunar.py
Normal file
23
saju-lab/app/calculator/lunar.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
"""음력↔양력 변환 — sxtwl 기반."""
|
||||||
|
import sxtwl
|
||||||
|
|
||||||
|
|
||||||
|
def solar_to_lunar(year: int, month: int, day: int) -> dict:
|
||||||
|
"""양력 → 음력 변환. {'year', 'month', 'day', 'is_leap'} 반환."""
|
||||||
|
day_obj = sxtwl.fromSolar(year, month, day)
|
||||||
|
return {
|
||||||
|
"year": day_obj.getLunarYear(),
|
||||||
|
"month": day_obj.getLunarMonth(),
|
||||||
|
"day": day_obj.getLunarDay(),
|
||||||
|
"is_leap": bool(day_obj.isLunarLeap()),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def lunar_to_solar(year: int, month: int, day: int, is_leap: bool = False) -> tuple[int, int, int]:
|
||||||
|
"""음력 → 양력 변환. (year, month, day) tuple 반환."""
|
||||||
|
day_obj = sxtwl.fromLunar(year, month, day, is_leap)
|
||||||
|
return (
|
||||||
|
day_obj.getSolarYear(),
|
||||||
|
day_obj.getSolarMonth(),
|
||||||
|
day_obj.getSolarDay(),
|
||||||
|
)
|
||||||
34
saju-lab/tests/test_lunar.py
Normal file
34
saju-lab/tests/test_lunar.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""음력↔양력 변환 테스트 (sxtwl 기반)."""
|
||||||
|
from app.calculator import lunar
|
||||||
|
|
||||||
|
|
||||||
|
def test_solar_to_lunar_known_date():
|
||||||
|
# 2024년 추석 = 양력 2024-09-17 = 음력 2024-08-15
|
||||||
|
result = lunar.solar_to_lunar(2024, 9, 17)
|
||||||
|
assert result["year"] == 2024
|
||||||
|
assert result["month"] == 8
|
||||||
|
assert result["day"] == 15
|
||||||
|
assert result["is_leap"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_lunar_to_solar_known_date():
|
||||||
|
# 음력 2024-08-15 → 양력 2024-09-17
|
||||||
|
result = lunar.lunar_to_solar(2024, 8, 15, is_leap=False)
|
||||||
|
assert result == (2024, 9, 17)
|
||||||
|
|
||||||
|
|
||||||
|
def test_solar_to_lunar_new_years():
|
||||||
|
# 양력 2024-01-01 → 음력 2023-11-20 (음력 새해는 보통 1~2월)
|
||||||
|
result = lunar.solar_to_lunar(2024, 1, 1)
|
||||||
|
assert result["year"] in (2023, 2024)
|
||||||
|
assert result["month"] in range(1, 13)
|
||||||
|
|
||||||
|
|
||||||
|
def test_roundtrip():
|
||||||
|
# 양력 → 음력 → 양력 = 원본
|
||||||
|
solar_input = (1990, 5, 15)
|
||||||
|
lunar_mid = lunar.solar_to_lunar(*solar_input)
|
||||||
|
solar_back = lunar.lunar_to_solar(
|
||||||
|
lunar_mid["year"], lunar_mid["month"], lunar_mid["day"], lunar_mid["is_leap"]
|
||||||
|
)
|
||||||
|
assert solar_back == solar_input
|
||||||
Reference in New Issue
Block a user