以C ++自定义日期格式 [英] Customize date format in C++

查看:145
本文介绍了以C ++自定义日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能:

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

输出如下:


Thu Oct 13 12:35:30 2011

Thu Oct 13 12:35:30 2011

但是我想看到输出像评论部分:

but I wanna see the output like the comment section:


2011-10-12 13:07:40

2011-10-12 13:07:40

如何可能?

推荐答案

这是非常可能的。看看 strftime()的联机帮助页。在这种情况下,您需要:

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

这篇关于以C ++自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆