If you're using Boost.Date_Time, this is done using IO facets.
You need to include boost/date_time/posix_time/posix_time_io.hpp
to get the correct facet typedefs (wtime_facet
, time_facet
, etc.) for boost::posix_time::ptime
. Once this is done, the code is pretty simple. You call imbue on the ostream
you want to output to, then just output your ptime
:
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::posix_time;
using namespace std;
int main(int argc, char **argv) {
time_facet *facet = new time_facet("%d-%b-%Y %H:%M:%S");
cout.imbue(locale(cout.getloc(), facet));
cout << second_clock::local_time() << endl;
}
Output:
14-Dec-2009 16:13:14
See also the list of format flags in the boost docs, in case you want to output something fancier.