본문 바로가기

Language Proficiency/C++

std::chrono의 time_point를 문자열로 변환하기

std::chrono의 time_point 형의 변수값을 특정 포맷을 가진 문자열 String 변수로 변환이 필요할 때 다음 코드를 참고한다.


1
2
3
4
5
6
7
8
9
std::string CMyDateTime::TimepointToString(const std::chrono::system_clock::time_point& p_tpTime,
                                           const std::string& p_sFormat)
{
    auto converted_timep = std::chrono::system_clock::to_time_t(p_tpTime);
    std::ostringstream oss;
    oss << std::put_time(std::localtime(&converted_timep), p_sFormat.c_str());
 
    return oss.str();
}




위의 함수를 호출할 땐 다음과 같이 호출하면 된다.


1
2
std::string sDate;
sDate = TimepointToString(p_tpTime, "%Y%m%d%H%M%S");