std::string LocaleFormattedDateTime (::boost::posix_time::ptime pt)
{
std::string s;
std::ostringstream ss;
//::boost::posix_time::time_facet * tf = new ::boost::posix_time::time_facet;
::boost::posix_time::time_facet tf;
//std::locale custom_locale (std::locale(""), tf);
std::locale custom_locale (std::locale(""), &tf);
// custom_locale takes ownership of the tf facet
ss.imbue (custom_locale);
//(*tf).format("%Y%m%d-%H%M%S-%f"); // date time
tf.format("%Y%m%d-%H%M%S-%f"); // date time
ss << pt;
// don't explicitly delete tf - what's wrong with a stack variable? why this runtime error? What is the point of new() above and
//what if I don't delete allocated memory here.
return ss.str();
}
void CurrentDateTimeWithMicroseconds(char s[], int sz = 100)
{
using namespace boost::posix_time;
using namespace std;
using boost::date_time::date_facet;
ptime t = microsec_clock::local_time();
ostringstream ss;
//time_facet *df = new time_facet();
time_facet df;
//std::locale loc (std::locale(""), df);
std::locale loc (std::locale(""), &df);
ss << LocaleFormattedDateTime(t) ;
memset(s,0,sz);
strcpy(s,ss.str().c_str());
}
I modified this code after copying this from some online source.