첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

▶ LunarCalendarHelper.cs

using System;
using System.Globalization;

/// <summary>
/// 음력 헬퍼
/// </summary>
public class LunarCalendarHelper
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 정간 배열
    /// </summary>
    private static string[] _celestialStemArray = new string[10]
    {
        "갑", "을", "병", "정", "무", "기", "경", "신", "임", "계"
    };

    /// <summary>
    /// 지지 배열
    /// </summary>
    private static string[] _terrestrialBranchArray  = new string[12]
    {
        "자", "축", "인", "묘", "진", "사", "오", "미", "신", "유", "술", "해"
    };

    /// <summary>
    /// 띠 배열
    /// </summary>
    private static string[] _zodiacsArray = new string[12]
    {
        "쥐", "소", "범", "토끼", "용", "뱀", "말", "양", "원숭이", "닭", "개", "돼지"
    };

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Instance
    //////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 한국 음력
    /// </summary>
    private KoreanLunisolarCalendar koreanLunisolarCalendar;

    /// <summary>
    /// 양력 일자
    /// </summary>
    private DateTime solarDate;

    /// <summary>
    /// 음력 일자
    /// </summary>
    private LunarDate lunarDate;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 음력일시 - LunarDate

    /// <summary>
    /// 음력일시
    /// </summary>
    public LunarDate LunarDate
    {
        get
        {
            return this.lunarDate;
        }
        set
        {
            this.lunarDate = value;

            this.solarDate = GetSolarDateTime(this.lunarDate);
        }
    }

    #endregion
    #region 양력일시 - SolarDateTime

    /// <summary>
    /// 양력일시
    /// </summary>
    public DateTime SolarDateTime
    {
        get
        {
            return this.solarDate;
        }
        set
        {
            this.solarDate = value;

            this.lunarDate = GetLunarDate(this.solarDate);
        }
    }

    #endregion
    #region 윤달 여부 - IsLeapMonth

    /// <summary>
    /// 윤달 여부
    /// </summary>
    public bool IsLeapMonth
    {
        get
        {
            return this.koreanLunisolarCalendar.IsLeapMonth(this.lunarDate.Year, this.lunarDate.Month);
        }
    }

    #endregion
    #region 육십갑자(육갑) - SexagenaryCycle

    /// <summary>
    /// 육십갑자(육갑)
    /// </summary>
    public string SexagenaryCycle
    {
        get
        {
            int sexagenaryYear    = this.koreanLunisolarCalendar.GetSexagenaryYear(this.solarDate);
            int celestialStem     = this.koreanLunisolarCalendar.GetCelestialStem(sexagenaryYear)     - 1;
            int terrestrialBranch = this.koreanLunisolarCalendar.GetTerrestrialBranch(sexagenaryYear) - 1;

            return string.Format("{0}{1}", _celestialStemArray[celestialStem], _terrestrialBranchArray[terrestrialBranch]);
        }
    }

    #endregion
    #region 띠 - Zodiac

    /// <summary>
    /// 띠
    /// </summary>
    public string Zodiac
    {
        get
        {
            int sexagenaryYear    = this.koreanLunisolarCalendar.GetSexagenaryYear(this.solarDate);
            int terrestrialBranch = this.koreanLunisolarCalendar.GetTerrestrialBranch(sexagenaryYear) - 1;

            return _zodiacsArray[terrestrialBranch];
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - LunarCalendarHelper(solarDate)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="solarDate">양력 일자</param>
    public LunarCalendarHelper(DateTime solarDate)
    {
        this.koreanLunisolarCalendar = new KoreanLunisolarCalendar();

        this.solarDate = solarDate;

        this.lunarDate = GetLunarDate(this.solarDate);
    }

    #endregion
    #region 생성자 - LunarCalendarHelper()

    /// <summary>
    /// 생성자
    /// </summary>
    public LunarCalendarHelper() : this(DateTime.Now)
    {
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 음력 일자 구하기 - GetLunarDate(solarDate)

    /// <summary>
    /// 음력 일자 구하기
    /// </summary>
    /// <param name="solarDate">양력 일시</param>
    /// <returns>음력 일자</returns>
    private LunarDate GetLunarDate(DateTime solarDate)
    {
        int lunarYear  = this.koreanLunisolarCalendar.GetYear(solarDate);
        int lunarMonth = this.koreanLunisolarCalendar.GetMonth(solarDate);
        int lunarDay   = this.koreanLunisolarCalendar.GetDayOfMonth(solarDate);

        if(this.koreanLunisolarCalendar.GetMonthsInYear(lunarYear) > 12)
        {
            int lunarLeapMonth = this.koreanLunisolarCalendar.GetLeapMonth(lunarYear);

            if(lunarMonth >= lunarLeapMonth)
            {
                lunarMonth--;
            }
        }

        return new LunarDate(lunarYear, lunarMonth, lunarDay);
    }

    #endregion
    #region 양력 일시 구하기 - GetSolarDateTime(lunarDate)

    /// <summary>
    /// 양력 일시 구하기
    /// </summary>
    /// <param name="lunarDate">음력 일자</param>
    /// <returns>양력 일자</returns>
    private DateTime GetSolarDateTime(LunarDate lunarDate)
    {
        if(this.koreanLunisolarCalendar.GetMonthsInYear(lunarDate.Year) > 12)
        {
            int leapMonth = this.koreanLunisolarCalendar.GetLeapMonth(lunarDate.Year);

            if(lunarDate.Month >= leapMonth - 1)
            {
                return this.koreanLunisolarCalendar.ToDateTime(lunarDate.Year, lunarDate.Month + 1, lunarDate.Day, 0, 0, 0, 0);
            }
            else
            {
                return this.koreanLunisolarCalendar.ToDateTime(lunarDate.Year, lunarDate.Month, lunarDate.Day, 0, 0, 0, 0);
            }
        }
        else
        {
            return this.koreanLunisolarCalendar.ToDateTime(lunarDate.Year, lunarDate.Month, lunarDate.Day, 0, 0, 0, 0);
        }
    }

    #endregion
}

 

▶ LunarDate.cs

/// <summary>
/// 음력 일자
/// </summary>
public class LunarDate
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 연도
    /// </summary>
    private int year;

    /// <summary>
    /// 월
    /// </summary>
    private int month;

    /// <summary>
    /// 일
    /// </summary>
    private int day;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 연도 - Year

    /// <summary>
    /// 연도
    /// </summary>
    public int Year
    {
        get
        {
            return this.year;
        }
    }

    #endregion
    #region 월 - Month

    /// <summary>
    /// 월
    /// </summary>
    public int Month
    {
        get
        {
            return this.month;
        }
    }

    #endregion
    #region 일 - Day

    /// <summary>
    /// 일
    /// </summary>
    public int Day
    {
        get
        {
            return this.day;
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - LunarDate(year, month, day)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="year">연도</param>
    /// <param name="month">월</param>
    /// <param name="day">일</param>
    public LunarDate(int year, int month, int day)
    {
        this.year  = year;
        this.month = month;
        this.day   = day;
    }

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 생성자 - LunarDate

    /// <summary>
    /// 생성자
    /// </summary>
    private LunarDate()
    {
    }

    #endregion
}

 

728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요