Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Stackcat's Blog

UTC String to Local Time 본문

프로그래밍/C, C++, MFC, Win API....

UTC String to Local Time

Stackcat 2018. 6. 4. 16:22
서버에서 UTC 값을 받아왔을 때가 있다. 이 값은 문자열로, 다음과 같은 형식일 것이다 "2108-05-04 06:12:04" 이 값을 로컬에서 보여줄 때는 로컬시간으로 보여줘야한다. 아래와 같이 변환 할 수 있다.
Cstring strDateOfEnrollUTC = _T("2108-05-04 06:12:04"); // 서버로부터 받아온 UTC 시간값. 현재는 더미로 그냥 넣어줌

COleDateTime tmUTC;
SYSTEMTIME stmUTC	= {0};
SYSTEMTIME stmLocal = {0};
try{
	if( !tmUTC.ParseDateTime(strDateOfEnrollUTC) ) // 우선 스트링을 COleDateTime으로 변환
		throw 0;

	if( !tmUTC.GetAsSystemTime(stmUTC) ) // 이 COleDateTime을 SYSTEMTIME 구조체에 저장
		throw 0;

	if( !SystemTimeToTzSpecificLocalTime(NULL, &stmUTC, &stmLocal)) // NULL:현재 시스템의 타임존을 기준으로 UTC타임을 Local 타임으로 변경
		throw 0;
}
catch (...)
{
	CString strLog; strLog.Format(_T("tz Change Error...\n"));
	OutputDebugString(strLog);
}
COleDateTime tmLocal(stmLocal); // 변경된 Local타임을 다시 COleDateTime으로 변경.
Cstring strResult; strResult.Format(_T("%s"), tmLocal.Format(_T("%Y-%m-%d %H:%M:%S"))); // 이와 같은 식으로 출력가능.

'프로그래밍 > C, C++, MFC, Win API....' 카테고리의 다른 글

CRichEditCtrl Encoding Error  (0) 2018.06.04
Read File Encoding Error  (0) 2018.06.04
CString to CFile  (0) 2018.06.04
Comments