Let’s say I have time_t and tm structure. I can’t use Boost but MFC. How can I make it a string like following?
Mon Apr 23 17:48:14 2012
Is using sprintf the only way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The C library includes
strftimespecifically for formatting dates/times. The format you’re asking for seems to correspond to something like this:I believe
std::put_timeuses a similar format string, though it does relieve you of having to explicitly deal with a buffer. If you want to write the output to a stream, it’s quite convenient, but to get it into a string it’s not a lot of help — you’d have to do something like:std::put_timeis new with C++11, but C++03 has atime_putfacet in a locale that can do the same thing. If memory serves, I did manage to make it work once, but after that decided it wasn’t worth the trouble, and I haven’t done it since.