C++/이게 왜 돼 ❔❔❔
[C++] 현재(시스템) 시간, 날짜 구하기
트럼프
2022. 2. 23. 20:09
'localtime' : this function or variable may be unsafe 라는 CRT경고 안나오게 구하기
1. inline함수로 localtime_xp() 생성
#include <ctime>
inline std::tm localtime_xp(std::time_t timer)
{
std::tm bt{};
#if defined(__unix__)
localtime_r(&timer, &bt);
#elif defined(_MSC_VER)
localtime_s(&bt, &timer);
#else
static std::mutex mtx;
std::lock_guard<std::mutex> lock(mtx);
bt = *std::localtime(&timer);
#endif
return bt;
}
// 시스템 날짜 리턴
int CMainDlg::GetCurrentDate()
{
time_t timer = time(NULL);
auto t = localtime_xp(timer);
int ndate = 0;
ndate = (t.tm_year + 1900) * 10000 + (t.tm_mon+ 1) * 100 + (t.tm_mday);
return ndate;
}
// 시스템 시간 리턴
int CMainDlg::GetCurrentTime()
{
time_t timer = time(NULL);
auto t = localtime_xp(timer);
int nTime = 0;
nTime = (t.tm_hour) + (t.tm_min) + (t.tm_sec);
return nTime;
}
적용
UINT CMainDlg::Thread_RecvData(LPVOID pParam)
{
CMainDlg *MainDlg = (CMainDlg*)pParam;
int file_count = 0; // 생성할 파일 개수
int nDate = GetCurrentDate(); // localtime 적용
...
return 0;
}
결과
2. CRT warning 무시하기
#define _CRT_SECURE_NO_WARNINGS
이 방법은 추천하지 않음
ref :
https://stackoverflow.com/questions/38034033/c-localtime-this-function-or-variable-may-be-unsafe