C++ time_t 的字符串表示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/997512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String representation of time_t?
提问by g33kz0r
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
不起作用
回答by Fred Larson
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
:
围绕上述技术的一个很好的包装是 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 Farmby Herb Sutter.
对于这样的问题,我喜欢链接Herb Sutter 的The String Formatters of Manor Farm。
UPDATE:
更新:
With C++11, use to_string()
.
对于 C++11,使用to_string()
.
回答by nulldevice
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
回答by Mo Morsi
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 找到:这里
回答by Reed Hedges
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));
回答by Matthias Wandel
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.
函数“ctime()”将时间转换为字符串。如果您想控制其打印方式,请使用“strftime”。但是, strftime() 接受“struct tm”的参数。使用“localtime()”将time_t 32位整数转换为struct tm。
回答by Matthias Wandel
回答by No Refunds No Returns
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.
这是我的格式化程序——欢迎评论。这个 q 似乎对让我找到我的帖子有最大的帮助,以便其他可能正在寻找相同内容的人。
#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;
}
回答by Josh Kelley
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 ctimeor to use strftimeplus either localtimeor gmtime.
C 方法是使用ctime或使用strftime加上localtime或gmtime。
If you want a more C++-like way of performing the conversion, you can investigate the Boost.DateTimelibrary.
如果您想要一种更类似于 C++ 的方式来执行转换,您可以研究Boost.DateTime库。
回答by Danushka Chathuranga
localtime did not work for me. I used localtime_s:
localtime 对我不起作用。我使用了localtime_s:
struct tm buf;
char dateString[26];
time_t time = time(nullptr);
localtime_s(&buf, &time);
asctime_s(dateString, 26, &buf);