I want to format a date/time to a string using boost.
Starting with the current date/time:
ptime now = second_clock::universal_time();
For whatever it is worth, here is the function that I wrote to do this:
#include "boost/date_time/posix_time/posix_time.hpp"
#include
#include
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 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;
}
The output of this program was:
20111130_142732
20111130_142734
I found these links useful: