String representation of time_t?

2019-01-23 11:17发布

问题:

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

回答1:

Try 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:

#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().



回答2:

Try this if you want to have the time in a readable string:

#include <ctime>

std::time_t now = std::time(NULL);
std::tm * ptm = std::localtime(&now);
char buffer[32];
// Format: Mo, 15.06.2009 20:20:00
std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm);  

For further reference of strftime() check out cppreference.com



回答3:

The top answer here does not work for me.

See the following examples demonstrating both the stringstream and lexical_cast answers as suggested:

#include <iostream>
#include <sstream>

int main(int argc, char** argv){
 const char *time_details = "2017-01-27 06:35:12";
  struct tm tm;
  strptime(time_details, "%Y-%m-%d %H:%M:%S", &tm);
  time_t t = mktime(&tm); 
  std::stringstream stream;
  stream << t;
  std::cout << t << "/" << stream.str() << std::endl;
}

Output: 1485498912/1485498912 Found here


#include <boost/lexical_cast.hpp>
#include <string>

int main(){
    const char *time_details = "2017-01-27 06:35:12";
    struct tm tm;
    strptime(time_details, "%Y-%m-%d %H:%M:%S", &tm);
    time_t t = mktime(&tm); 
    std::string ts = boost::lexical_cast<std::string>(t);
    std::cout << t << "/" << ts << std::endl;
    return 0;
}

Output: 1485498912/1485498912 Found: here


The 2nd highest rated solution works locally:

#include <iostream>
#include <string>
#include <ctime>

int main(){
  const char *time_details = "2017-01-27 06:35:12";
  struct tm tm;
  strptime(time_details, "%Y-%m-%d %H:%M:%S", &tm);
  time_t t = mktime(&tm); 

  std::tm * ptm = std::localtime(&t);
  char buffer[32];
  std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm);
  std::cout << t << "/" << buffer;
}

Output: 1485498912/2017-01-27 06:35:12 Found: here




回答4:

The C++ way is to use stringstream.

The C way is to use snprintf() to format the number:

 char buf[16];
 snprintf(buf, 16, "%lu", time(NULL));


回答5:

Standard C++ does not have any time/date functions of its own - you need to use the C localtime and related functions.



回答6:

the function "ctime()" will convert a time to a string. If you want to control the way its printed, use "strftime". However, strftime() takes an argument of "struct tm". Use "localtime()" to convert the time_t 32 bit integer to a struct tm.



回答7:

There are a myriad of ways in which you might want to format time (depending on the time zone, how you want to display it, etc.), so you can't simply implicitly convert a time_t to a string.

The C way is to use ctime or to use strftime plus either localtime or gmtime.

If you want a more C++-like way of performing the conversion, you can investigate the Boost.DateTime library.



回答8:

localtime did not work for me. I used localtime_s:

struct tm buf;
char dateString[26];
time_t time = time(nullptr);
localtime_s(&buf, &time);
asctime_s(dateString, 26, &buf);


标签: c++ timestamp