두 시간 차이를 계산하여 Total TermDay를 계산하여 준다.
/* // 날짜 관련 구조체. operator 포함 SDate() void Initialize() SDate& operator=( const SDate& rhsDate ) this->iMonth = rhsDate.iMonth; this->iDay = rhsDate.iDay; this->iHour = rhsDate.iHour; this->iMin = rhsDate.iMin; this->iSec = rhsDate.iSec; return (*this); bool operator<( const SDate& rhsDate ) const if( this->iMonth > rhsDate.iMonth ) return false; if( this->iDay > rhsDate.iDay ) return false; if( this->iHour > rhsDate.iHour ) return false; if( this->iMin > rhsDate.iMin ) return false; if( this->iSec >= rhsDate.iSec ) return false; return true; bool operator>( const SDate& rhsDate ) const if( this->iMonth < rhsDate.iMonth ) return false; if( this->iDay < rhsDate.iDay ) return false; if( this->iMin < rhsDate.iMin ) return false; if( this->iSec <= rhsDate.iSec ) return false; return true; bool operator==( const SDate& rhsDate ) const return true; bool operator!=( const SDate& rhsDate ) const return false; bool operator<=( const SDate& rhsDate ) const if( this->iMonth > rhsDate.iMonth ) return false; if( this->iDay > rhsDate.iDay ) return false; if( this->iHour > rhsDate.iHour ) return false; if( this->iMin > rhsDate.iMin ) return false; if( this->iSec > rhsDate.iSec ) return false; return true; bool operator>=( const SDate& rhsDate ) const if( this->iMonth < rhsDate.iMonth ) return false; if( this->iDay < rhsDate.iDay ) return false; if( this->iHour < rhsDate.iHour ) return false; if( this->iMin < rhsDate.iMin ) return false; if( this->iSec < rhsDate.iSec ) return false; return true; */
struct SDate
{
int iYear;
int iMonth;
int iDay;
int iHour;
int iMin;
int iSec;
{
Initialize();
}
{
memset( this, 0x00, sizeof(SDate) );
}
{
this->iYear = rhsDate.iYear;
}
{
if( this->iYear > rhsDate.iYear ) return false;
if( this->iYear < rhsDate.iYear ) return true;
if( this->iMonth < rhsDate.iMonth ) return true;
if( this->iDay < rhsDate.iDay ) return true;
if( this->iHour < rhsDate.iHour ) return true;
if( this->iMin < rhsDate.iMin ) return true;
}
{
if( this->iYear < rhsDate.iYear ) return false;
if( this->iYear > rhsDate.iYear ) return true;
if( this->iMonth > rhsDate.iMonth ) return true;
if( this->iDay > rhsDate.iDay ) return true;
if( this->iHour < rhsDate.iHour ) return false
if( this->iHour > rhsDate.iHour ) return true;
if( this->iMin > rhsDate.iMin ) return true;
}
{
if( this->iYear != rhsDate.iYear ) return false;
if( this->iMonth != rhsDate.iMonth ) return false;
if( this->iDay != rhsDate.iDay ) return false;
if( this->iHour != rhsDate.iHour ) return false;
if( this->iMin != rhsDate.iMin ) return false;
if( this->iSec != rhsDate.iSec ) return false;
}
{
if( this->iYear != rhsDate.iYear ) return true;
if( this->iMonth != rhsDate.iMonth ) return true;
if( this->iDay != rhsDate.iDay ) return true;
if( this->iHour != rhsDate.iHour ) return true;
if( this->iMin != rhsDate.iMin ) return true;
if( this->iSec != rhsDate.iSec ) return true;
}
{
if( this->iYear > rhsDate.iYear ) return false;
if( this->iYear < rhsDate.iYear ) return true;
if( this->iMonth < rhsDate.iMonth ) return true;
if( this->iDay < rhsDate.iDay ) return true;
if( this->iHour < rhsDate.iHour ) return true;
if( this->iMin < rhsDate.iMin ) return true;
}
{
if( this->iYear < rhsDate.iYear ) return false;
if( this->iYear > rhsDate.iYear ) return true;
if( this->iMonth > rhsDate.iMonth ) return true;
if( this->iDay > rhsDate.iDay ) return true;
if( this->iHour > rhsDate.iHour ) return true;
if( this->iMin > rhsDate.iMin ) return true;
}
} // !SDate
// 입력학 SYSTEMTIME 값을 Date 구조체에 담는다.
void GetSysTimeToDateInfo( SDate& sDateInfo, const SYSTEMTIME& sysTime )
{
sDateInfo.iSec = sysTime.wSecond;
sDateInfo.iMin = sysTime.wMinute;
sDateInfo.iHour = sysTime.wHour;
sDateInfo.iDay = sysTime.wDay;
sDateInfo.iMonth = sysTime.wMonth;
sDateInfo.iYear = sysTime.wYear;
}
// SYSTEMTIME 값을 비교하여 몇초 차이 나는지 계산하여 반환한다.
double CompareSystemTime( PSYSTEMTIME pTargetTime, PSYSTEMTIME pCompareTime )
{
tm tmTime1, tmTime2;
time_t timeTime1, timeTime2;
tmTime1.tm_sec = pTargetTime->wSecond;
tmTime1.tm_min = pTargetTime->wMinute;
tmTime1.tm_hour = pTargetTime->wHour;
tmTime1.tm_mday = pTargetTime->wDay;
tmTime1.tm_mon = pTargetTime->wMonth - 1;
tmTime1.tm_year = pTargetTime->wYear - 1900;
tmTime1.tm_isdst = 0;
timeTime1 = ::mktime( &tmTime1 );
tmTime2.tm_sec = pCompareTime->wSecond;
tmTime2.tm_min = pCompareTime->wMinute;
tmTime2.tm_hour = pCompareTime->wHour;
tmTime2.tm_mday = pCompareTime->wDay;
tmTime2.tm_mon = pCompareTime->wMonth - 1;
tmTime2.tm_year = pCompareTime->wYear - 1900;
tmTime2.tm_isdst = 0;
timeTime2 = ::mktime( &tmTime2 );
// time2와 time1의 second 차이를 double 형으로 리턴
return ::difftime( timeTime1, timeTime2 );
}
// SYSTEMTIME 값을 비교.
int GetTermDay( const SDate& sStartDate, const SDate& sEndDate )
{
SYSTEMTIME startSystemTime, endSystemTime;
startSystemTime.wYear = sStartDate.iYear;
startSystemTime.wMonth = sStartDate.iMonth;
startSystemTime.wDay = sStartDate.iDay;
startSystemTime.wHour = sStartDate.iHour;
startSystemTime.wMinute = sStartDate.iMin;
startSystemTime.wSecond = 0;
startSystemTime.wMilliseconds = 0;
startSystemTime.wDayOfWeek = 0;
endSystemTime.wYear = sEndDate .iYear;
endSystemTime.wMonth = sEndDate .iMonth;
endSystemTime.wDay = sEndDate .iDay;
endSystemTime.wHour = sEndDate .iHour;
endSystemTime.wMinute = sEndDate .iMin;
endSystemTime.wSecond = 0;
endSystemTime.wMilliseconds = 0;
endSystemTime.wDayOfWeek = 0;
int iTermTickTime = (int)( ::CompareSystemTime(&endSysTime, &startSysTime) );
if( 0 >= iTermTickTime ) return 0;
// 86400 [Second * Minute * Hour]
const int iTimeToDay = ( 60 * 60 * 24 );
return ( (iTerm / iTimeToDay) + 1 );
}
void main()
{
......
SDateInfo sCurrentDate;
SYSTEMTIME sCurrentSysTime;
::GetLocalTime( &CurrentSysTime );
GetSysTimeToDateInfo( sCurrentDate, CurrentSysTime );
// 설정된 시간에서 현재 시간 까지 Day 계산
int iTermDay = GetTermDay( sTargetDate, sCurrentDate );
}
'[ Programing ] > Algorithm' 카테고리의 다른 글
Tip 클래스 객체 생성과 초기 선언을 한줄로... (0) | 2016.07.25 |
---|---|
설정된 기간 관련 카운터(주간) 계산하기 (0) | 2014.01.10 |
TickTime -> Date 구조체로. localtime_s (0) | 2013.12.20 |
OS(운영체제) 이름 알아오기 (0) | 2013.11.14 |
VARIANT 해제 (0) | 2013.05.21 |