/***********************************************************************
*                                                                                                  *
*    기준일에서 gap일 만큼 빼는 함수                                                      *
*                                                                                                  *
************************************************************************/


today = 기준일, 년도,월,일 순서대로 저장되서 입력해야함 (20070110 )
gap = 빼고 싶은 날짜
return = 계산된 날짜 역시 년도,월,일 순서대로 저장되서 반환(int)

 
int minus_day(int today, int gap)
{   
    int total = 0, year = 0, month = 0, day = 0, temp = 0, result;
    char date[MAX_DATE_SIZE] = {0};
    total = today;
   
    // 년,달,일 값 계산 
    year = total / 10000;
    month = total / 100 % 100;
    day = total % 100;
   
    temp = day - gap;
   
    // 뺀값이 1보다 클 경우 연산 없음
    if( temp > 1 )
    {
        // 계산없음
    }
   
    // 뺀값이 1보다 작은 경우 연산
    else
    {
        month -= 1;
        //
        if( month == 0 )
        {
            month = 12;
            year -= 1;
        }   
        switch(month)
        {
            // 31일로 끝나는 달들 계산
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                temp += 31;
                break;
            case 2:
                // 윤달인 경우 29일을 더해줌
                if( year % 400 == 0 ||(year % 100 !=0 && year % 4 == 0) )
                {
                    temp += 29;
                }
                else // 아닐 경우 28일을 더해줌
                {
                    temp += 28;
                }       
                break;
            default:
                temp += 30;
                break;
        } //end of switch
    }
   
    sprintf(date,"%04d%02d%02d",year,month,temp); // date 문자열로 만들어줌
    result = atoi(date);                         // 다시 정수형으로 변환
    return result;
}



/***********************************************************************
*                                                                                                  *
*    기준일에서 gap일 만큼 더하는 함수                                                   *
*                                                                                                  *
************************************************************************/


today = 기준일, 년도,월,일 순서대로 저장되서 입력해야함 (20070110 )
gap = 더하고 싶은 날짜
return = 계산된 날짜 역시 년도,월,일 순서대로 저장되서 반환(int)

 
int plus_day(int today, int gap)
{
    int total = 0, year = 0, month = 0, day = 0, temp = 0, result;
    char date[MAX_DATE_SIZE] = {0};
    total = today;
   
    // 년,달,일 값 계산
    year = total / 10000;
    month = total / 100 % 100;
    day = total % 100;
   
    switch(month)
    {
        // 31일로 끝나는 달들 계산
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            temp = day + gap;
            if( temp > 31 ) // 더한 값이 31일을 넘을 경우
            {
                temp = temp - 31;
                // 12월일 경우 1년이 올라가고 달은 1월이 됨
                if( month == 12 )
                {
                    year += 1;
                    month = 1;
                }
                // 아닐경우 달만 1달 올라감
                else
                {
                    month += 1;
                }
            }       
                   
            break;
        case 2:
            temp = day + gap;
            // 윤달일 경우
            if( year % 400 == 0 ||(year % 100 !=0 && year % 4 == 0) )
            {
                if( temp > 29 ) // 29일이 넘으면 1달 더해줌
                {
                    temp = temp - 29;
                    month += 1;
                }
            }
            else  // 아닐 경우
            {
                if( temp > 28 )  // 28일이 넘으면 1달 더해줌
                {
                    temp = temp - 28;
                    month += 1;
                }
            }
            break;
        // 나머지 4,6,9,11 월달 들 계산  (30일)
        default:
            temp = day + gap;
            // 30일이 넘으면 한달 더해줌
            if( temp > 30 )
            {
                temp = temp - 30;
                month += 1;
            }  
            break;
    } //end of switch
    sprintf(date,"%04d%02d%02d",year,month,temp); // date 문자열로 만들어줌
    result = atoi(date);                         // 다시 정수형으로 변환
    return result;
}
Posted by 용학도리
,