【发布时间】:2011-08-18 20:16:24
【问题描述】:
我想将当前时间打印为2011-08-18 10:11:12 -07:00。我开发了如下代码sn-p,
#include <iostream>
using namespace std;
void time_to_string(time_t clock,const char *fmtstr )
{
char buf[256];
if (strftime(buf, 256, fmtstr, localtime(&clock)) == 0)
buf[0] = 0;
cout << buf << endl;
}
int main()
{
time_to_string(time(NULL), "%Y-%m-%d %H%M%S %z");
}
我可以将时间显示为2011-08-18 10:11:12 -0700,但不能显示为2011-08-18 10:11:12 -07:00。使用"%Y-%m-%d %H%M%S %:z" 产生2011-08-18 10:11:12 %:z。
如何在 C/C++ 中完成上述任务。
【问题讨论】: