음력을 양력으로 변환할 수 있는 범위는 음력 1800-01-01 ~ 2049-12-08 까지이다.
다음 링크에서 c로 구현된 소스를 구해 일부 버그를 수정하고 변환 범위 확장 및 양력->음력 변환 기능 추가, 음력 간지 계산 기능을 추가해서 c#으로 다시 작성했다.
천문연구원의 음력/양력 변환 데이터와 대조 확인 완료했으며 위 변환 범위 내에서 문제 없음을 확인했다.
변환 클래스(Lunar)의 코드는 다음과 같다.
using System;
using System.Collections.Generic;
using System.Text;
namespace Solar2Lunar
{
/// <summary>
/// 양력<->음력 변환 클래스<br/>
/// 변환이 가능한 양력 범위(1800-01-25 ~ 2050-01-22)<br/>
/// 변환이 가능한 음력 범위(1800-01-01 ~ 2049-12-08)<br/>
/// 다음 링크에 있는 알고리즘을 C#코드로 수정해서 작성했음을 밝힌다.<br/>
/// http://openwrld.egloos.com/1417170<br/>
/// </summary>
public class Lunar
{
#region 음력데이터
/// <summary>
/// 음력 달의 특징<br/>
/// 1: 평달 (작은달)<br/>
/// 2: 평달 (큰달)<br/>
/// 3: 윤달 (작은달 + 작은달)<br/>
/// 4: 윤달 (작은달 + 큰달)<br/>
/// 5: 윤달 (큰달 + 작은달)<br/>
/// 6: 윤달 (큰달 + 큰달)<br/>
/// 작은달=29, 큰달=30<br/>
readonly int[,] LUNAR_FEATURES = {{2,1,2,5,2,1,2,1,2,1,2,1}, // 1800
{1,2,2,1,2,1,2,2,1,2,1,2}, // 1801
{1,1,2,1,2,1,2,2,2,1,2,1}, // 1802
{2,3,2,1,2,1,2,2,1,2,2,1}, // 1803
{2,1,1,2,1,1,2,2,1,2,2,2}, // 1804
{1,2,1,2,1,3,2,1,2,2,2,1}, // 1805
{2,2,1,2,1,1,1,2,1,2,2,1}, // 1806
{2,2,2,1,1,2,1,1,2,1,2,2}, // 1807
{1,2,2,1,5,2,1,2,1,1,2,1}, // 1808
{2,2,1,2,2,1,2,1,2,1,1,2}, // 1809
{1,2,1,2,2,1,2,2,1,2,1,2}, // 1810
{1,1,5,2,1,2,2,1,2,2,1,2}, // 1811
{1,1,2,1,2,1,2,1,2,2,2,1}, // 1812
{2,1,2,1,1,1,2,1,2,2,2,1}, // 1813
{2,5,2,1,1,1,2,1,2,2,1,2}, // 1814
{2,2,1,1,2,1,1,2,1,2,1,2}, // 1815
{2,2,1,2,1,5,1,2,1,2,1,2}, // 1816
{2,1,2,2,1,2,1,2,1,1,2,1}, // 1817
{2,1,2,2,1,2,2,1,2,1,1,2}, // 1818
{1,2,1,5,2,2,1,2,2,1,2,1}, // 1819
{1,2,1,2,1,2,1,2,2,2,1,2}, // 1820
{1,1,2,1,1,2,1,2,2,2,1,2}, // 1821
{2,1,5,1,1,2,1,2,2,1,2,2}, // 1822
{2,1,2,1,1,1,2,1,2,1,2,2}, // 1823
{2,1,2,1,2,1,4,1,2,1,2,2}, // 1824
{2,1,2,1,2,1,1,2,1,2,1,2}, // 1825
{2,1,2,2,1,2,1,1,2,1,2,1}, // 1826
{2,1,2,2,4,1,2,1,2,1,2,1}, // 1827
{2,1,2,1,2,2,1,2,1,2,1,2}, // 1828
{1,2,1,2,1,2,1,2,2,1,2,2}, // 1829
{1,1,2,3,2,1,2,2,1,2,2,2}, // 1830
{1,1,2,1,1,2,1,2,1,2,2,2}, // 1831
{1,2,1,2,1,1,2,1,5,2,2,2}, // 1832
{1,2,1,2,1,1,2,1,2,1,2,2}, // 1833
{1,2,2,1,2,1,1,2,1,2,1,2}, // 1834
{1,2,2,1,2,5,1,2,1,2,1,2}, // 1835
{1,2,1,2,2,1,2,1,2,1,2,1}, // 1836
{2,1,2,1,2,1,2,2,1,2,1,2}, // 1837
{1,2,1,5,1,2,2,1,2,2,1,2}, // 1838
{1,2,1,1,2,1,2,1,2,2,2,1}, // 1839
{2,1,2,1,1,2,1,2,1,2,2,2}, // 1840
{1,2,4,1,1,2,1,2,1,2,2,1}, // 1841
{2,2,1,2,1,1,2,1,2,1,2,1}, // 1842
{2,2,2,1,2,1,4,1,2,1,2,1}, // 1843
{2,2,1,2,1,2,1,2,1,2,1,2}, // 1844
{1,2,1,2,2,1,2,1,2,1,2,1}, // 1845
{2,1,2,1,5,2,1,2,2,1,2,1}, // 1846
{2,1,1,2,1,2,1,2,2,2,1,2}, // 1847
{1,2,1,1,2,1,2,1,2,2,2,1}, // 1848
{2,1,2,3,2,1,2,1,2,1,2,2}, // 1849
{2,1,2,1,1,2,1,1,2,2,1,2}, // 1850
{2,2,1,2,1,1,2,3,2,1,2,2}, // 1851
{2,1,2,2,1,1,2,1,2,1,1,2}, // 1852
{2,1,2,2,1,2,1,2,1,2,1,2}, // 1853
{1,2,1,2,1,2,5,2,1,2,1,2}, // 1854
{1,1,2,1,2,2,1,2,2,1,2,1}, // 1855
{2,1,1,2,1,2,1,2,2,2,1,2}, // 1856
{1,2,1,1,5,2,1,2,1,2,2,2}, // 1857
{1,2,1,1,2,1,1,2,2,1,2,2}, // 1858
{2,1,2,1,1,2,1,1,2,1,2,2}, // 1859
{2,1,6,1,1,2,1,1,2,1,2,2}, // 1860
{1,2,2,1,2,1,2,1,2,1,1,2}, // 1861
{2,1,2,1,2,2,1,5,2,1,1,2}, // 1862
{1,2,2,1,2,1,2,2,1,2,1,2}, // 1863
{1,1,2,1,2,1,2,2,1,2,2,1}, // 1864
{2,1,1,2,4,1,2,2,1,2,2,1}, // 1865
{2,1,1,2,1,1,2,2,1,2,2,2}, // 1866
{1,2,1,1,2,1,1,2,1,2,2,2}, // 1867
{1,2,2,3,2,1,1,2,1,2,2,1}, // 1868
{2,2,2,1,1,2,1,1,2,1,2,1}, // 1869
{2,2,2,1,2,1,2,1,1,5,2,1}, // 1870
{2,2,1,2,2,1,2,1,2,1,1,2}, // 1871
{1,2,1,2,2,1,2,1,2,2,1,2}, // 1872
{1,1,2,1,2,4,2,1,2,2,1,2}, // 1873
{1,1,2,1,2,1,2,1,2,2,2,1}, // 1874
{2,1,1,2,1,1,2,1,2,2,2,1}, // 1875
{2,2,1,1,5,1,2,1,2,2,1,2}, // 1876
{2,2,1,1,2,1,1,2,1,2,1,2}, // 1877
{2,2,1,2,1,2,1,1,2,1,2,1}, // 1878
{2,2,4,2,1,2,1,1,2,1,2,1}, // 1879
{2,1,2,2,1,2,2,1,2,1,1,2}, // 1880
{1,2,1,2,1,2,5,2,2,1,2,1}, // 1881
{1,2,1,2,1,2,1,2,2,1,2,2}, // 1882
{1,1,2,1,1,2,1,2,2,2,1,2}, // 1883
{2,1,1,2,3,2,1,2,2,1,2,2}, // 1884
{2,1,1,2,1,1,2,1,2,1,2,2}, // 1885
{2,1,2,1,2,1,1,2,1,2,1,2}, // 1886
{2,2,1,5,2,1,1,2,1,2,1,2}, // 1887
{2,1,2,2,1,2,1,1,2,1,2,1}, // 1888
{2,1,2,2,1,2,1,2,1,2,1,2}, // 1889
{1,5,2,1,2,2,1,2,1,2,1,2}, // 1890
{1,2,1,2,1,2,1,2,2,1,2,2}, // 1891
{1,1,2,1,1,5,2,2,1,2,2,2}, // 1892
{1,1,2,1,1,2,1,2,1,2,2,2}, // 1893
{1,2,1,2,1,1,2,1,2,1,2,2}, // 1894
{2,1,2,1,5,1,2,1,2,1,2,1}, // 1895
{2,2,2,1,2,1,1,2,1,2,1,2}, // 1896
{1,2,2,1,2,1,2,1,2,1,2,1}, // 1897
{2,1,5,2,2,1,2,1,2,1,2,1}, // 1898
{2,1,2,1,2,1,2,2,1,2,1,2}, // 1899
{1,2,1,1,2,1,2,5,2,2,1,2}, // 1900
{1,2,1,1,2,1,2,1,2,2,2,1}, // 1901
{2,1,2,1,1,2,1,2,1,2,2,2}, // 1902
{1,2,1,2,3,2,1,1,2,2,1,2}, // 1903
{2,2,1,2,1,1,2,1,1,2,2,1}, // 1904
{2,2,1,2,2,1,1,2,1,2,1,2}, // 1905
{1,2,2,4,1,2,1,2,1,2,1,2}, // 1906
{1,2,1,2,1,2,2,1,2,1,2,1}, // 1907
{2,1,1,2,2,1,2,1,2,2,1,2}, // 1908
{1,5,1,2,1,2,1,2,2,2,1,2}, // 1909
{1,2,1,1,2,1,2,1,2,2,2,1}, // 1910
{2,1,2,1,1,5,1,2,2,1,2,2}, // 1911
{2,1,2,1,1,2,1,1,2,2,1,2}, // 1912
{2,2,1,2,1,1,2,1,1,2,1,2}, // 1913
{2,2,1,2,5,1,2,1,2,1,1,2}, // 1914
{2,1,2,2,1,2,1,2,1,2,1,2}, // 1915
{1,2,1,2,1,2,2,1,2,1,2,1}, // 1916
{2,3,2,1,2,2,1,2,2,1,2,1}, // 1917
{2,1,1,2,1,2,1,2,2,2,1,2}, // 1918
{1,2,1,1,2,1,5,2,2,1,2,2}, // 1919
{1,2,1,1,2,1,1,2,2,1,2,2}, // 1920
{2,1,2,1,1,2,1,1,2,1,2,2}, // 1921
{2,1,2,2,3,2,1,1,2,1,2,2}, // 1922
{1,2,2,1,2,1,2,1,2,1,1,2}, // 1923
{2,1,2,1,2,2,1,2,1,2,1,1}, // 1924
{2,1,2,5,2,1,2,2,1,2,1,2}, // 1925
{1,1,2,1,2,1,2,2,1,2,2,1}, // 1926
{2,1,1,2,1,2,1,2,2,1,2,2}, // 1927
{1,5,1,2,1,1,2,2,1,2,2,2}, // 1928
{1,2,1,1,2,1,1,2,1,2,2,2}, // 1929
{1,2,2,1,1,5,1,2,1,2,2,1}, // 1930
{2,2,2,1,1,2,1,1,2,1,2,1}, // 1931
{2,2,2,1,2,1,2,1,1,2,1,2}, // 1932
{1,2,2,1,6,1,2,1,2,1,1,2}, // 1933
{1,2,1,2,2,1,2,2,1,2,1,2}, // 1934
{1,1,2,1,2,1,2,2,1,2,2,1}, // 1935
{2,1,4,1,2,1,2,1,2,2,2,1}, // 1936
{2,1,1,2,1,1,2,1,2,2,2,1}, // 1937
{2,2,1,1,2,1,4,1,2,2,1,2}, // 1938
{2,2,1,1,2,1,1,2,1,2,1,2}, // 1939
{2,2,1,2,1,2,1,1,2,1,2,1}, // 1940
{2,2,1,2,2,4,1,1,2,1,2,1}, // 1941
{2,1,2,2,1,2,2,1,2,1,1,2}, // 1942
{1,2,1,2,1,2,2,1,2,2,1,2}, // 1943
{1,1,2,4,1,2,1,2,2,1,2,2}, // 1944
{1,1,2,1,1,2,1,2,2,2,1,2}, // 1945
{2,1,1,2,1,1,2,1,2,2,1,2}, // 1946
{2,5,1,2,1,1,2,1,2,1,2,2}, // 1947
{2,1,2,1,2,1,1,2,1,2,1,2}, // 1948
{2,2,1,2,1,2,3,2,1,2,1,2}, // 1949
{2,1,2,2,1,2,1,1,2,1,2,1}, // 1950
{2,1,2,2,1,2,1,2,1,2,1,2}, // 1951
{1,2,1,2,4,2,1,2,1,2,1,2}, // 1952
{1,2,1,1,2,2,1,2,2,1,2,2}, // 1953
{1,1,2,1,1,2,1,2,2,1,2,2}, // 1954
{2,1,4,1,1,2,1,2,1,2,2,2}, // 1955
{1,2,1,2,1,1,2,1,2,1,2,2}, // 1956
{2,1,2,1,2,1,1,5,2,1,2,2}, // 1957
{1,2,2,1,2,1,1,2,1,2,1,2}, // 1958
{1,2,2,1,2,1,2,1,2,1,2,1}, // 1959
{2,1,2,1,2,5,2,1,2,1,2,1}, // 1960
{2,1,2,1,2,1,2,2,1,2,1,2}, // 1961
{1,2,1,1,2,1,2,2,1,2,2,1}, // 1962
{2,1,2,3,2,1,2,1,2,2,2,1}, // 1963
{2,1,2,1,1,2,1,2,1,2,2,2}, // 1964
{1,2,1,2,1,1,2,1,1,2,2,2}, // 1965
{1,2,5,2,1,1,2,1,1,2,2,1}, // 1966
{2,2,1,2,2,1,1,2,1,2,1,2}, // 1967
{1,2,2,1,2,1,5,2,1,2,1,2}, // 1968
{1,2,1,2,1,2,2,1,2,1,2,1}, // 1969
{2,1,1,2,2,1,2,1,2,2,1,2}, // 1970
{1,2,1,1,5,2,1,2,2,2,1,2}, // 1971
{1,2,1,1,2,1,2,1,2,2,2,1}, // 1972
{2,1,2,1,1,2,1,1,2,2,2,1}, // 1973
{2,2,1,5,1,2,1,1,2,2,1,2}, // 1974
{2,2,1,2,1,1,2,1,1,2,1,2}, // 1975
{2,2,1,2,1,2,1,5,2,1,1,2}, // 1976
{2,1,2,2,1,2,1,2,1,2,1,1}, // 1977
{2,2,1,2,1,2,2,1,2,1,2,1}, // 1978
{2,1,1,2,1,6,1,2,2,1,2,1}, // 1979
{2,1,1,2,1,2,1,2,2,1,2,2}, // 1980
{1,2,1,1,2,1,1,2,2,1,2,2}, // 1981
{2,1,2,3,2,1,1,2,2,1,2,2}, // 1982
{2,1,2,1,1,2,1,1,2,1,2,2}, // 1983
{2,1,2,2,1,1,2,1,1,5,2,2}, // 1984
{1,2,2,1,2,1,2,1,1,2,1,2}, // 1985
{1,2,2,1,2,2,1,2,1,2,1,1}, // 1986
{2,1,2,2,1,5,2,2,1,2,1,2}, // 1987
{1,1,2,1,2,1,2,2,1,2,2,1}, // 1988
{2,1,1,2,1,2,1,2,2,1,2,2}, // 1989
{1,2,1,1,5,1,2,2,1,2,2,2}, // 1990
{1,2,1,1,2,1,1,2,1,2,2,2}, // 1991
{1,2,2,1,1,2,1,1,2,1,2,2}, // 1992
{1,2,5,2,1,2,1,1,2,1,2,1}, // 1993
{2,2,2,1,2,1,2,1,1,2,1,2}, // 1994
{1,2,2,1,2,2,1,5,2,1,1,2}, // 1995
{1,2,1,2,2,1,2,1,2,2,1,2}, // 1996
{1,1,2,1,2,1,2,2,1,2,2,1}, // 1997
{2,1,1,2,3,2,2,1,2,2,2,1}, // 1998
{2,1,1,2,1,1,2,1,2,2,2,1}, // 1999
{2,2,1,1,2,1,1,2,1,2,2,1}, // 2000
{2,2,2,3,2,1,1,2,1,2,1,2}, // 2001
{2,2,1,2,1,2,1,1,2,1,2,1}, // 2002
{2,2,1,2,2,1,2,1,1,2,1,2}, // 2003
{1,5,2,2,1,2,1,2,1,2,1,2}, // 2004
{1,2,1,2,1,2,2,1,2,2,1,1}, // 2005
{2,1,2,1,2,1,5,2,2,1,2,2}, // 2006
{1,1,2,1,1,2,1,2,2,2,1,2}, // 2007
{2,1,1,2,1,1,2,1,2,2,1,2}, // 2008
{2,2,1,1,5,1,2,1,2,1,2,2}, // 2009
{2,1,2,1,2,1,1,2,1,2,1,2}, // 2010
{2,1,2,2,1,2,1,1,2,1,2,1}, // 2011
{2,1,6,2,1,2,1,1,2,1,2,1}, // 2012
{2,1,2,2,1,2,1,2,1,2,1,2}, // 2013
{1,2,1,2,1,2,1,2,5,2,1,2}, // 2014
{1,2,1,1,2,1,2,2,2,1,2,1}, // 2015
{2,1,2,1,1,2,1,2,2,1,2,2}, // 2016
{1,2,1,2,3,2,1,2,1,2,2,2}, // 2017
{1,2,1,2,1,1,2,1,2,1,2,2}, // 2018
{2,1,2,1,2,1,1,2,1,2,1,2}, // 2019
{2,1,2,5,2,1,1,2,1,2,1,2}, // 2020
{1,2,2,1,2,1,2,1,2,1,2,1}, // 2021
{2,1,2,1,2,2,1,2,1,2,1,2}, // 2022
{1,5,2,1,2,1,2,2,1,2,1,2}, // 2023
{1,2,1,1,2,1,2,2,1,2,2,1}, // 2024
{2,1,2,1,1,5,2,1,2,2,2,1}, // 2025
{2,1,2,1,1,2,1,2,1,2,2,2}, // 2026
{1,2,1,2,1,1,2,1,1,2,2,2}, // 2027
{1,2,2,1,5,1,2,1,1,2,2,1}, // 2028
{2,2,1,2,2,1,1,2,1,1,2,2}, // 2029
{1,2,1,2,2,1,2,1,2,1,2,1}, // 2030
{2,1,5,2,1,2,2,1,2,1,2,1}, // 2031
{2,1,1,2,1,2,2,1,2,2,1,2}, // 2032
{1,2,1,1,2,1,2,1,2,2,5,2}, // 2033
{1,2,1,1,2,1,2,1,2,2,2,1}, // 2034
{2,1,2,1,1,2,1,1,2,2,1,2}, // 2035
{2,2,1,2,1,4,1,1,2,2,1,2}, // 2036
{2,2,1,2,1,1,2,1,1,2,1,2}, // 2037
{2,2,1,2,1,2,1,2,1,1,2,1}, // 2038
{2,2,1,2,5,2,1,2,1,2,1,1}, // 2039
{2,1,2,2,1,2,2,1,2,1,2,1}, // 2040
{2,1,1,2,1,2,2,1,2,2,1,2}, // 2041
{1,5,1,2,1,2,1,2,2,1,2,2}, // 2042
{1,2,1,1,2,1,1,2,2,1,2,2}, // 2043
{2,1,2,1,1,2,3,2,1,2,2,2}, // 2044
{2,1,2,1,1,2,1,1,2,1,2,2}, // 2045
{2,1,2,2,1,1,2,1,1,2,1,2}, // 2046
{2,1,2,2,4,1,2,1,1,2,1,2}, // 2047
{1,2,2,1,2,2,1,2,1,2,1,1}, // 2048
{2,1,2,1,2,2,1,2,2,1,2,1}, // 2049
};
#endregion
/// <summary>
/// 음력 달의 특징<br/>
/// 0: 평달 (작은달)<br/>
/// 1: 평달 (큰달)<br/>
/// 2: 윤달 (작은달 + 작은달)<br/>
/// 3: 윤달 (작은달 + 큰달)<br/>
/// 4: 윤달 (큰달 + 작은달)<br/>
/// 5: 윤달 (큰달 + 큰달)<br/>
/// 작은달=29, 큰달=30<br/>
/// </summary>
private readonly int[] DAYS_OF_LUNAR_FEATURES = { 29, 30, 58, 59, 59, 60 };
private readonly int[] NORMAL_DAYS_OF_LEAP_MONTH = { 0, 0, 29, 29, 30, 30 };
/// <summary>
/// 양력 달의 날 수
/// </summary>
private readonly int[] SOLAR_DAYS_OF_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/// <summary>
/// 10간
/// </summary>
private readonly string[] GAN = { "갑", "을", "병", "정", "무", "기", "경", "신", "임", "계" };
/// <summary>
/// 12지
/// </summary>
private readonly string[] JI = { "자", "축", "인", "묘", "진", "사", "오", "미", "신", "유", "술", "해" };
// 1800-01-25 (토) 1800-01-01 경신(庚申)년 무인(戊寅)월 갑인(甲寅)일
private readonly int[] SOLAR_BASIC_DATE = { 1800, 1, 25 }; // 년, 월, 일
private readonly int[] BASIC_GANJI = { 6, 8, 4, 2, 0, 2 }; // 경신년, 무인월, 갑인일
/// <summary>
/// 양력 달의 날 수 조회<br/>
/// 윤년일 경우 2월은 29일
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <returns></returns>
public int GetSolarDaysOfMonth(int year, int month)
{ // 월별 일수 계산
if (month != 2)
return SOLAR_DAYS_OF_MONTH[month - 1];
if (IsLeapYear(year))
return 29;
else
return 28;
}
/// <summary>
/// 윤년체크
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public bool IsLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)))
return true;
else
return false;
}
/// <summary>
/// 양력 해의 날 수 조회<br/>
/// 윤년일 경우 366일, 평년일 경우 365일
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public int GetSolarDaysOfYear(int year)
{
if (IsLeapYear(year))
return 366;
else
return 365;
}
/// <summary>
/// 특정 일자의 양력 날 수 조회
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public int GetSolarDays(int year, int month, int day)
{
int ret = 0;
for (int i = SOLAR_BASIC_DATE[0]; i < year; i++)
ret += GetSolarDaysOfYear(i);
for (int i = 1; i < month; i++)
ret += GetSolarDaysOfMonth(year, i);
ret += day;
for (int i = 1; i < SOLAR_BASIC_DATE[1]; i++)
ret -= GetSolarDaysOfMonth(SOLAR_BASIC_DATE[0], i);
ret -= SOLAR_BASIC_DATE[2];
return ret;
}
/// <summary>
/// 특정 일자의 음력 날 수 조회
/// </summary>
/// <param name="ly"></param>
/// <param name="lm"></param>
/// <param name="ld"></param>
/// <param name="leapMonth"></param>
/// <returns></returns>
public int GetLunarDays(int ly, int lm, int ld, bool leapMonth)
{
int ret = 0;
for (int i = SOLAR_BASIC_DATE[0]; i < ly; i++)
ret += GetLunarDaysOfYear(i);
for (int i = 1; i < lm; i++)
ret += GetLunarDaysOfMonth(ly, i);
// 윤달일 경우 동월 평달 날짜 더해 줌
if (leapMonth)
{
ret += NORMAL_DAYS_OF_LEAP_MONTH[LUNAR_FEATURES[ly - SOLAR_BASIC_DATE[0], lm - 1] - 1];
}
ret += ld;
return ret;
}
/// <summary>
/// 양력을 음력으로 변환
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public string ToLunarDate(int year, int month, int day)
{
int ly = 0, lm = 0, ld = 0;
return ToLunarDate(GetSolarDays(year, month, day), out ly, out lm, out ld);
}
/// <summary>
/// 양력을 음력으로 변환
/// </summary>
/// <param name="solarDays"></param>
/// <param name="ly"></param>
/// <param name="lm"></param>
/// <param name="ld"></param>
/// <returns></returns>
public string ToLunarDate(int solarDays, out int ly, out int lm, out int ld)
{
int lunarDays = 0;
ly = 0;
lm = 0;
ld = 0;
for (int y = 0; y < LUNAR_FEATURES.GetLength(0); y++)
{
for (int m = 0; m < 12; m++)
{
int lday = DAYS_OF_LUNAR_FEATURES[LUNAR_FEATURES[y, m] - 1];
if (lunarDays + lday > solarDays)
{
ly = y + SOLAR_BASIC_DATE[0];
lm = m + 1;
ld = solarDays - lunarDays + 1;
if (ld > NORMAL_DAYS_OF_LEAP_MONTH[LUNAR_FEATURES[y, m] - 1])
ld -= NORMAL_DAYS_OF_LEAP_MONTH[LUNAR_FEATURES[y, m] - 1];
return string.Format("{0:0000}-{1:00}-{2:00}", ly, lm, ld);
}
lunarDays += lday;
}
}
return string.Format("{0:0000}-{1:00}-{2:00}", ly, lm, ld);
}
/// <summary>
/// 음력을 양력으로 변환
/// </summary>
/// <param name="ly"></param>
/// <param name="lm"></param>
/// <param name="ld"></param>
/// <param name="leapMonth"></param>
/// <returns></returns>
public string ToSolarDate(int ly, int lm, int ld, bool leapMonth)
{
int year = 0, month = 0, day = 0;
return ToLunarDate(GetLunarDays(ly, lm, ld, leapMonth), out year, out month, out day);
}
/// <summary>
/// 음력을 양력으로 변환
/// </summary>
/// <param name="lunarDays"></param>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public string ToSolarDate(int lunarDays, out int year, out int month, out int day)
{
int solarDays = 0;
year = 0;
month = 0;
day = 0;
lunarDays += SOLAR_BASIC_DATE[2] - 1;
for (int y = 0; y < LUNAR_FEATURES.GetLength(0); y++)
{
for (int m = 0; m < 12; m++)
{
int lday = GetSolarDaysOfMonth(y + SOLAR_BASIC_DATE[0], m + 1);
if (solarDays + lday >= lunarDays)
{
year = y + SOLAR_BASIC_DATE[0];
month = m + 1;
day = lunarDays - solarDays;
return string.Format("{0:0000}-{1:00}-{2:00}", year, month, day);
}
solarDays += lday;
}
}
return string.Format("{0:0000}-{1:00}-{2:00}", year, month, day);
}
/// <summary>
/// 음력 연도의 간지 문자열 조회
/// </summary>
/// <param name="ly"></param>
/// <returns></returns>
public string GetYearGanji(int ly)
{
string gan = GAN[(ly - SOLAR_BASIC_DATE[0] + BASIC_GANJI[0]) % 10];
string ji = JI[(ly - SOLAR_BASIC_DATE[0] + BASIC_GANJI[1]) % 12];
return gan + ji;
}
/// <summary>
/// 음력 월의 간지 문자열 조회
/// </summary>
/// <param name="ly"></param>
/// <param name="lm"></param>
/// <returns></returns>
public string GetMonthGanji(int ly, int lm)
{
int lunarMonthes = (ly - SOLAR_BASIC_DATE[0]) * 12 + lm - 1;
string gan = GAN[(lunarMonthes + BASIC_GANJI[2]) % 10];
string ji = JI[(lunarMonthes + BASIC_GANJI[3]) % 12];
return gan + ji;
}
/// <summary>
/// 음력 날의 간지 문자열 조회
/// </summary>
/// <param name="totalDaySolar"></param>
/// <returns></returns>
public string GetDayGanji(int solarDays)
{
string gan = GAN[(solarDays + BASIC_GANJI[4]) % 10];
string ji = JI[(solarDays + BASIC_GANJI[5]) % 12];
return gan + ji;
}
/// <summary>
/// 음력 연도의 전체 날 수
/// </summary>
/// <param name="ly"></param>
/// <returns></returns>
public int GetLunarDaysOfYear(int ly)
{
int ret = 0;
for (int i = 0; i < 12; i++)
{
ret += DAYS_OF_LUNAR_FEATURES[LUNAR_FEATURES[ly - SOLAR_BASIC_DATE[0], i] - 1];
}
return ret;
}
/// <summary>
/// 음력 월의 날 수
/// </summary>
/// <param name="ly"></param>
/// <param name="lm"></param>
/// <returns></returns>
public int GetLunarDaysOfMonth(int ly, int lm)
{
return DAYS_OF_LUNAR_FEATURES[LUNAR_FEATURES[ly - SOLAR_BASIC_DATE[0], lm - 1] - 1];
}
/// <summary>
/// 양력 연도의 특정 월까지의 전체 날 수
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <returns></returns>
public int GetSolarDaysOfYear(int year, int month)
{
int ret = 0;
for (int i = 0; i < month; i++)
{
ret += GetSolarDaysOfMonth(year, i + 1);
}
return ret;
}
}
}
Lunar 클래스는 다음 처럼 사용한다.
끝.