time_t seconds; time(&seconds); cout << seconds << endl;
This gives me a timestamp. How can I get that epoch date into a string?
std::string s = seconds;
does not work
Try std::stringstream.
std::stringstream
#include <string> #include <sstream> std::stringstream ss; ss << seconds; std::string ts = ss.str();
A nice wrapper around the above technique is Boost's lexical_cast:
lexical_cast
#include <boost/lexical_cast.hpp> #include <string> std::string ts = boost::lexical_cast<std::string>(seconds);
And for questions like this, I'm fond of linking The String Formatters of Manor Farm by Herb Sutter.
UPDATE:
With C++11, use to_string().
to_string()
1.4m articles
1.4m replys
5 comments
57.0k users