mac*_*nir 25 c++ formatting datetime
我想使用boost将日期/时间格式化为字符串.
从当前日期/时间开始:
ptime now = second_clock::universal_time();
Run Code Online (Sandbox Code Playgroud)
并以包含此格式的日期/时间的wstring结束:
%Y%m%d_%H%M%S
Run Code Online (Sandbox Code Playgroud)
你能告诉我实现这个的代码吗?谢谢.
Rob*_*obᵩ 30
无论它值多少,这是我写的功能:
#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>
#include <sstream>
std::wstring FormatTime(boost::posix_time::ptime now)
{
using namespace boost::posix_time;
static std::locale loc(std::wcout.getloc(),
new wtime_facet(L"%Y%m%d_%H%M%S"));
std::basic_stringstream<wchar_t> wss;
wss.imbue(loc);
wss << now;
return wss.str();
}
int main() {
using namespace boost::posix_time;
ptime now = second_clock::universal_time();
std::wstring ws(FormatTime(now));
std::wcout << ws << std::endl;
sleep(2);
now = second_clock::universal_time();
ws = FormatTime(now);
std::wcout << ws << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
该计划的结果是:
20111130_142732
20111130_142734
Run Code Online (Sandbox Code Playgroud)
我发现这些链接很有用: