시간 연산 방법

MFC 2011. 8. 26. 12:22
작성자 : CalmMass
작성일자 : 2010.05.28
제목 : 함수를 이용한 윈도우(windows) 시간 계산 방법
툴 : MS Visual C++ 6.0, STLPort 5.6, SDK
함 수 정리
GetLocalTime : IpSystemTime (현재 로컬 시간을 대입받을 SYSTEMTIME 구조체 사용합니다.) ( 바로가기 )
GetTickCount : 수행 시간을 측정 할때 많이 사용하는 함수( 바로가기 )
GetSystemTime : 현재 Windows System상의 날짜 및 시간을 반환 하는 함수( 바로가기 )
MFC
CTime : MFC에서 사용하는 시간 관련 클래스 단, Millisecond는 없다. ( 바로가기 )
CTime::CurrentTime() : 현재 시간을 구하기
CTimeSpan : MFC에서 사용하는 시간을 연산하는 방법이다. ( 바로가기 )

그외
---------------------------------------------------------------------------------------------
CompareFileTime
DosDateTimeToFileTime
FileTimeToDosDateTime
FileTimeToLocalFileTime
FileTimeToSystemTime
GetFileTime
GetLocalTime
GetSystemTime
GetSystemTimeAdjustment
GetSystemTimeAsFileTime
GetTickCount
GetTimeZoneInformation
LocalFileTimeToFileTime
SetFileTime
SetLocalTime
SetSystemTime
SetSystemTimeAdjustment
SetTimeZoneInformation
SystemTimeToFileTime
SystemTimeToTzSpecificLocalTime
---------------------------------------------------------------------------------------------

  • 시간을 연산하는 것이 말처럼 쉬운 것은 아니라고 생각 합니다. 밀리세컨드를 계산할때는 더욱 골치가 아프고 어떻게 연산을 해야 정확한 값을 가지고 오는지 여러번 테스트를 해야 합니다.
  • 가장 좋은 방법은 API를 활용하여 하나의 프로그램을 만드는 것도 좋지만 기존에 있는 좋은 라이브러리는 적절하게 사용하면 되겠습니다.
  • 그외 시간 관련 함수는 찾아

사용 방법 1. CTime

class CTime
{
public:

// Constructors
static CTime PASCAL GetCurrentTime();

CTime();
CTime(time_t time);
CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
int nDST = -1);
CTime(WORD wDosDate, WORD wDosTime, int nDST = -1);
CTime(const CTime& timeSrc);

CTime(const SYSTEMTIME& sysTime, int nDST = -1);
CTime(const FILETIME& fileTime, int nDST = -1);
const CTime& operator=(const CTime& timeSrc);
const CTime& operator=(time_t t);

// Attributes
struct tm* GetGmtTm(struct tm* ptm = NULL) const;
struct tm* GetLocalTm(struct tm* ptm = NULL) const;
BOOL GetAsSystemTime(SYSTEMTIME& timeDest) const;

time_t GetTime() const;
int GetYear() const;
int GetMonth() const; // month of year (1 = Jan)
int GetDay() const; // day of month
int GetHour() const;
int GetMinute() const;
int GetSecond() const;
int GetDayOfWeek() const; // 1=Sun, 2=Mon, ..., 7=Sat

// Operations
// time math
CTimeSpan operator-(CTime time) const;
CTime operator-(CTimeSpan timeSpan) const;
CTime operator+(CTimeSpan timeSpan) const;
const CTime& operator+=(CTimeSpan timeSpan);
const CTime& operator-=(CTimeSpan timeSpan);
BOOL operator==(CTime time) const;
BOOL operator!=(CTime time) const;
BOOL operator<(CTime time) const;
BOOL operator>(CTime time) const;
BOOL operator<=(CTime time) const;
BOOL operator>=(CTime time) const;

// formatting using "C" strftime
CString Format(LPCTSTR pFormat) const;
CString FormatGmt(LPCTSTR pFormat) const;
CString Format(UINT nFormatID) const;
CString FormatGmt(UINT nFormatID) const;

#ifdef _UNICODE
// for compatibility with MFC 3.x
CString Format(LPCSTR pFormat) const;
CString FormatGmt(LPCSTR pFormat) const;
#endif

// serialization
#ifdef _DEBUG
friend CDumpContext& AFXAPI operator<<(CDumpContext& dc, CTime time);
#endif
friend CArchive& AFXAPI operator<<(CArchive& ar, CTime time);
friend CArchive& AFXAPI operator>>(CArchive& ar, CTime& rtime);

private:
time_t m_time;
};




사용 방법 2. CTimeSpan

class CTimeSpan
{
public:

// Constructors
CTimeSpan();
CTimeSpan(time_t time);
CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);

CTimeSpan(const CTimeSpan& timeSpanSrc);
const CTimeSpan& operator=(const CTimeSpan& timeSpanSrc);

// Attributes
// extract parts
LONG GetDays() const; // total # of days
LONG GetTotalHours() const;
int GetHours() const;
LONG GetTotalMinutes() const;
int GetMinutes() const;
LONG GetTotalSeconds() const;
int GetSeconds() const;

// Operations
// time math
CTimeSpan operator-(CTimeSpan timeSpan) const;
CTimeSpan operator+(CTimeSpan timeSpan) const;
const CTimeSpan& operator+=(CTimeSpan timeSpan);
const CTimeSpan& operator-=(CTimeSpan timeSpan);
BOOL operator==(CTimeSpan timeSpan) const;
BOOL operator!=(CTimeSpan timeSpan) const;
BOOL operator<(CTimeSpan timeSpan) const;
BOOL operator>(CTimeSpan timeSpan) const;
BOOL operator<=(CTimeSpan timeSpan) const;
BOOL operator>=(CTimeSpan timeSpan) const;

#ifdef _UNICODE
// for compatibility with MFC 3.x
CString Format(LPCSTR pFormat) const;
#endif
CString Format(LPCTSTR pFormat) const;
CString Format(UINT nID) const;

// serialization
#ifdef _DEBUG
friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,CTimeSpan timeSpan);
#endif
friend CArchive& AFXAPI operator<<(CArchive& ar, CTimeSpan timeSpan);
friend CArchive& AFXAPI operator>>(CArchive& ar, CTimeSpan& rtimeSpan);

private:
time_t m_timeSpan;
friend class CTime;
};




사용 방법 3. MFC CTime 클래스 사용


CString CurrentTimeText;
CTime CurTime;

CurTime = CTime::GetCurrentTime(); // 현재 시스템 시각을 구한다.

CurrentTimeText.Format( "현재 날짜 / 시각 : %04d-%02d-%02d / %02d:%02d:%02d",
, CurTime.GetYear()
, CurTime.GetMonth()
, CurTime.GetDay()
, CurTime.GetHour()
, CurTime.GetMinute()
, CurTime.GetSecond()
);



  • 출처 : http://kongmks.cafe24.com/239?TSSESSION=7329c8d8597d2af44015e2b666bb2e6f
  • CString 문자열 클래스에 현재 시간을 대입하여 Format로 각각의 년/월/요 시/분/초 로 정의 할 수 있습니다.

사용 방법 4. Millisecond(밀리세컨드) 현재 시간

SYSTEMTIME cur_time;
GetLocalTime(&cur_time);
CString strPCTime;


strPCTime.Format("%04d%02d%02d%02d%02d%02d%03ld",
cur_time.wYear,
cur_time.wMonth,
cur_time.wDay,
cur_time.wHour,
cur_time.wMinute,
cur_time.wSecond,
cur_time.wMilliseconds); );



  • 출처 : http://blog.naver.com/sanglyn/90047509460
  • CTime 클래스에 밀리세컨드가 없기 때문에 GetLocalTime() API 함수를 이용하여 밀리세컨드 값을 구합니다.


사용 방법 5. GetTickCount 활용 방법

SYSTEMTIME cur_time;

어떤 명령어의 수행시간을 측정하고 싶을때가 있다.
이때 유용하게 사용할수 있는 함수가 GetTickCount()이다.

GetTickCount()함수는 시스템이 시작 된 후 얼마의 시간이 경과했는지를 반환한다. 단위는 밀리세컨드 단위이다. 참고로 경과시간은 DWORD(32비트 값)이므로 최대 49.7일이 지나면 다시 0으로 된다고 한다.

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

DWORD GetTickCount(void);

Parameters : This function has no parameters.
Return values : The return value is the number of milliseconds that have elapsed since the system was started.


#include
#include //GetTickCount()함수 이용을 위해 추가한다.

using namespace std;

void main()
{
long startTime = GetTickCount(); //현재 시각을 저장한다.(시작지점)

for(int i=0; i<100000; i++) //시간 딜레이를 주기 위해 i값을 출력한다.
cout<<

long endTime = GetTickCount(); //현재 시각을 저장한다.(종료지점)
long tickDiff = endTime - startTime; //수행시간 = 종료시각 - 시작시각
long secDiff = tickDiff / 1000 ; //이건 초단위로도 나타내기 위한 것.

cout<<"Start Time : "<<<"ms"<
cout<<"End Time : "<<<"ms"<
cout<<"Tick difference : "<<<"ms"<
cout<<"Second difference : "<<<"s"<
}



  • 출처 : http://dolbbi.com/83
  • 수행시간을 측정하는 방법이며 이것은 49.7일 지나면 0으로 반환 됩니다.

출처 : http://calmmass.tistory.com/220

================================================================================================
출처 : http://rcan.net/598
Posted by pkss
,