我有这个功能:

void Log::Write(string logline){
//2011-10-12 13:07:40 correct format
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    m_stream.write( asctime (timeinfo), 24 );
    m_stream << " - " << logline << std::endl;
}

输出如下:



但我想看到输出类似于注释部分:



这怎么可能?

最佳答案

这很有可能。查看联机帮助页中的strftime()。在这种情况下,您需要:

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;
}

10-08 12:40