Marius
Marius

Reputation: 1101

Assign date value to string (boost c++ libraries, posix time)

I know how to output current date by using boost posix_time library:

   time_facet *format = new time_facet("%Y-%m-%d");
   cout.imbue(locale(cout.getloc(), format));
   cout << second_clock::local_time() << endl;

But I don't know how to assign it to a string. I would like to ask you if you know how to do it and also to ask you, what does this line mean cout.imbue?

Upvotes: 3

Views: 155

Answers (1)

sehe
sehe

Reputation: 393064

You can just use a string stream.

std::ostringstream oss;

time_facet *format = new time_facet("%Y-%m-%d");
oss.imbue(locale(cout.getloc(), format));
oss << second_clock::local_time();

std::string datetext = oss.str();

Consider keeping the imbued stream around for efficiency. Use

oss.clear();
oss.str(""); 

to reuse it.

Upvotes: 4

Related Questions