gpt4 book ai didi

c++ - time_t 的字符串表示?

转载 作者:IT老高 更新时间:2023-10-28 12:46:34 26 4
gpt4 key购买 nike

time_t seconds;
time(&seconds);

cout << seconds << endl;

这给了我一个时间戳。如何将该纪元日期转换为字符串?

std::string s = seconds;

没用

最佳答案

试试 std::stringstream .

#include <string>
#include <sstream>

std::stringstream ss;
ss << seconds;
std::string ts = ss.str();

Boost 的 lexical_cast 是上述技术的一个很好的包装。 :

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

std::string ts = boost::lexical_cast<std::string>(seconds);

对于这样的问题,我喜欢链接 The String Formatters of Manor Farm赫伯萨特。

更新:

对于 C++11,使用 to_string() .

关于c++ - time_t 的字符串表示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com