简体   繁体   English

time_t的字符串表示?

[英]String representation of time_t?

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 : 围绕上述技术的一个很好的包装是Boost的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. 对于这样的问题,我喜欢用Herb Sutter链接The Manor Formatters of Manor Farm

UPDATE: 更新:

With C++11, use to_string() . 使用C ++ 11,使用to_string()

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 有关strftime()的进一步参考,请查看cppreference.com

The top answer here does not work for me. 这里的最佳答案对我不起作用。

See the following examples demonstrating both the stringstream and lexical_cast answers as suggested: 请参阅以下示例,演示如下建议的stringstream和lexical_cast答案:

#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 输出:1485498912/1485498912在这里找到


#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 输出:1485498912/1485498912发现: 这里


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 输出:1485498912 / 2017-01-27 06:35:12发现: 这里


The C++ way is to use stringstream. C ++方式是使用stringstream。

The C way is to use snprintf() to format the number: C方式是使用snprintf()格式化数字:

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

标准C ++没有任何自己的时间/日期功能-你需要使用C 本地时间和相关的功能。

the function "ctime()" will convert a time to a string. 函数“ctime()”将时间转换为字符串。 If you want to control the way its printed, use "strftime". 如果要控制其打印方式,请使用“strftime”。 However, strftime() takes an argument of "struct tm". 但是,strftime()接受“struct tm”的参数。 Use "localtime()" to convert the time_t 32 bit integer to a struct tm. 使用“localtime()”将time_t 32位整数转换为struct tm。

localtime did not work for me. 当地时间不适合我。 I used localtime_s: 我用了localtime_s:

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

Here's my formatter -- comments welcome. 这是我的格式化程序 - 欢迎评论。 This q seemed like it had the most help getting me to my a so posting for anyone else who may be looking for the same. 这个问题看起来像是帮助我找到其他任何可能正在寻找相同内容的人。

#include <iostream>
#include "Parser.h"
#include <string>
#include <memory>
#include <ctime>
#include <chrono>
#include <iomanip>
#include <thread>

using namespace std;
string to_yyyyMMddHHmmssffffff();

string to_yyyyMMddHHmmssffffff() {
    using namespace std::chrono;
    high_resolution_clock::time_point pointInTime = high_resolution_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(pointInTime);
    microseconds micros = duration_cast<microseconds>(pointInTime.time_since_epoch());
    std::size_t fractional_microseconds = micros.count() % 1'000'000;

    std:stringstream microstream;
    microstream << "00000" << fractional_microseconds;
    string formatted = microstream.str();
    int index = formatted.length() - 6;
    formatted = formatted.substr(index);
    std::stringstream dateStream;
    dateStream << std::put_time(std::localtime(&now_c), "%F %T") << "." << formatted;
    formatted = dateStream.str();

    return formatted;
}

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. 您可以通过多种方式格式化时间(取决于时区,您希望如何显示它等),因此您不能简单地将time_t隐式转换为字符串。

The C way is to use ctime or to use strftime plus either localtime or gmtime . C方式是使用ctime或使用strftime加上localtimegmtime

If you want a more C++-like way of performing the conversion, you can investigate the Boost.DateTime library. 如果你想要一种更像C ++的方式来执行转换,你可以调查Boost.DateTime库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM