첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 주차 번호 (Week Number) 관리자

----------------------------------------------------------------------------------------------------

WeekNumberManager.cs

 

 

using System;

 

/// <summary>

/// 주차 번호 관리자

/// </summary>

public class WeekNumberManager

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

    #region Field

 

    /// <summary>

    /// 현재 일자

    /// </summary>

    private DateTime currentDate;

 

    /// <summary>

    /// 주 시작 일자

    /// </summary>

    private DateTime weekStartDate;

 

    /// <summary>

    /// 주 종료 일자

    /// </summary>

    private DateTime weekEndDate;

 

    /// <summary>

    /// 주차 번호

    /// </summary>

    private int weekNumber;

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 주차 번호 - WeekNumber

 

    /// <summary>

    /// 주차 번호

    /// </summary>

    public int WeekNumber

    {

        get

        {

            return this.weekNumber;

        }

    }

 

    #endregion

 

    #region 주 시작 일자 - WeekStartDate

        

    /// <summary>

    /// 주 시작 일자

    /// </summary>

    public DateTime WeekStartDate

    {

        get

        {

            return this.weekStartDate;

        }

    }

 

    #endregion

 

    #region 주 종료 일자 - WeekEndDate

 

    /// <summary>

    /// 주 종료 일자

    /// </summary>

    public DateTime WeekEndDate

    {

        get

        {

            return this.weekEndDate;

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 생성자 - WeekNumberManager(currentDate)

 

    /// <summary>

    /// 생성자

    /// </summary>

    /// <param name="currentDate">현재 일자</param>

    public WeekNumberManager(DateTime currentDate)

    {

        this.currentDate = currentDate;

 

        Initialize();

    }

 

    #endregion

 

    #region 생성자 - WeekNumberManager(year, weekNumber)

 

    /// <summary>

    /// 생성자

    /// </summary>

    /// <param name="year">연도</param>

    /// <param name="weekNumber">주차 번호</param>

    public WeekNumberManager(int year, int weekNumber)

    {

        int month = 1;

        int day   = 1;

 

        this.currentDate = new DateTime(year, month, day);

 

        Initialize();

 

        if(weekNumber > 1)

        {

            this.currentDate = this.weekEndDate.AddDays((weekNumber - 1) * 7 - 1);

 

            Initialize();

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method

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

 

    #region 초기화 하기 - Initialize()

 

    /// <summary>

    /// 초기화 하기

    /// </summary>

    private void Initialize()

    {

        int dayNumber      = this.currentDate.DayOfYear;

        int dayStartNumber = 0;

        

        switch(this.currentDate.DayOfWeek)

        {

            case DayOfWeek.Sunday :

 

                dayStartNumber = dayNumber;

 

                this.weekStartDate = this.currentDate;

                this.weekEndDate   = this.currentDate.AddDays(6);

 

                break;

 

            case DayOfWeek.Monday :

 

                dayStartNumber = dayNumber - 1;

 

                this.weekStartDate = this.currentDate.AddDays(-1);

                this.weekEndDate   = this.currentDate.AddDays( 5);

 

                break;

 

            case DayOfWeek.Tuesday :

 

                dayStartNumber = dayNumber - 2;

 

                this.weekStartDate = this.currentDate.AddDays(-2);

                this.weekEndDate   = this.currentDate.AddDays( 4);

 

                break;

 

            case DayOfWeek.Wednesday :

 

                dayStartNumber = dayNumber - 3;

 

                this.weekStartDate = this.currentDate.AddDays(-3);

                this.weekEndDate   = this.currentDate.AddDays( 3);

 

                break;

 

            case DayOfWeek.Thursday :

 

                dayStartNumber = dayNumber - 4;

 

                this.weekStartDate = this.currentDate.AddDays(-4);

                this.weekEndDate   = this.currentDate.AddDays( 2);

 

                break;

 

            case DayOfWeek.Friday :

 

                dayStartNumber = dayNumber - 5;

 

                this.weekStartDate = this.currentDate.AddDays(-5);

                this.weekEndDate   = this.currentDate.AddDays( 1);

 

                break;

 

            case DayOfWeek.Saturday :

 

                dayStartNumber = dayNumber - 6;

 

                this.weekStartDate = this.currentDate.AddDays(-6);

                this.weekEndDate   = this.currentDate;

 

                break;

        }

        

        this.weekNumber = dayStartNumber / 7;

    

        if(this.weekStartDate.Year  > this.currentDate.AddYears(-1).Year)

        {

            this.weekNumber += 1;

 

            int divideWeekNumber = dayStartNumber % 7;

 

            if(divideWeekNumber != 0)

            {

                this.weekNumber += 1;

            }

        

            if(this.currentDate.Year == this.weekEndDate.AddYears(-1).Year)

            {

                this.weekEndDate = this.weekEndDate.AddDays(-1 * this.weekEndDate.DayOfYear );

            }

        }

        else

        {

            this.weekNumber = 1;

 

            dayStartNumber = -1 * dayStartNumber + 1;

 

            this.weekStartDate = this.weekStartDate.AddDays(dayStartNumber);

        }

    }

 

    #endregion

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요