Customize date format in C++

大兔子大兔子 提交于 2019-12-06 07:43:01

It's very possible. Look at the manpage for strftime(). In this case, you'd want:

void Log::Write(string line) 
{
    time_t rawtime;
    struct tm * timeinfo;
    char buffer [80];

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    strftime (buffer,80,"%Y-%m-%d %H:%M:%S",timeinfo);
    m_stream << buffer << " - " << line << endl;
}

Boost offers a library for that.

You can use the strftime() function to format times in all ways you want. In your case the format string "%Y-%m-%d %H:%M:%S" should produce the desired result.

You can use strftime() to convert a struct tm into a string with somewhat flexible formatting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!