c ++将time_t转换为字符串并再次返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7113396/
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
c++ convert time_t to string and back again
提问by Kelly Elton
I want to convert a time_t to a string and back again.
I'd like to convert the time to a string using ctime().
I can't seem to find anything on google or the time.h header file, any ideas?
Basically what I'm trying to do is store a date in a file, and then read it back so I can use it as a time_t again.
Also, no library references outside of std,mfc.
One more note, this will have to function on Windows xp and above and that's it.
我想将 time_t 转换为字符串并再次返回。
我想使用 ctime() 将时间转换为字符串。
我似乎在 google 或 time.h 头文件上找不到任何东西,有什么想法吗?
基本上我想做的是将日期存储在文件中,然后将其读回,以便我可以再次将其用作 time_t。
此外,在 std、mfc 之外没有库引用。
还要注意的是,这必须在 Windows xp 及更高版本上运行,仅此而已。
Edit
编辑
All I want to do is convert a time_t into a string(I don't care if it's human readable) and then convert it back to a time_t. I'm basically just trying to store the time_t into a file and read it again(but I don't want any code for that, as there will be more info in the file besides a time_t).
我想要做的就是将 time_t 转换为字符串(我不在乎它是否是人类可读的),然后将其转换回 time_t。我基本上只是试图将 time_t 存储到一个文件中并再次读取它(但我不想要任何代码,因为除了 time_t 之外,文件中还会有更多信息)。
回答by dario_ramos
You'll have to write your own function to do that. These functions convert any primitive type (or any type which overloads operator<< and/or operator>>) to a string, and viceversa:
您必须编写自己的函数才能做到这一点。这些函数将任何原始类型(或任何重载 operator<< 和/或 operator>> 的类型)转换为字符串,反之亦然:
template<typename T>
std::string StringUtils::toString(const T &t) {
std::ostringstream oss;
oss << t;
return oss.str();
}
template<typename T>
T StringUtils::fromString( const std::string& s ) {
std::istringstream stream( s );
T t;
stream >> t;
return t;
}
回答by Remy Lebeau
ctime()
returns a pointer to a character buffer that uses a specific formatting. You could use sprintf()
to parse such a string into its individual portions, store them in a struct tm
, and use mktime()
to convert that to a time_t
.
ctime()
返回一个指向使用特定格式的字符缓冲区的指针。您可以使用sprintf()
将此类字符串解析为其各个部分,将它们存储在 a 中struct tm
,然后使用mktime()
将其转换为 a time_t
。
回答by Taha
Convert the time_t to struct tm using gmtime(), then convert the struct tm to plain text (preferably ISO 8601 format) using strftime(). The result will be portable, human readable, and machine readable.
使用gmtime()将 time_t 转换为 struct tm ,然后使用strftime()将 struct tm 转换为纯文本(最好是 ISO 8601 格式)。结果将是可移植的、人类可读的和机器可读的。
To get back to the time_t, you just parse the string back into a struct tm and use mktime().
要返回 time_t,您只需将字符串解析回 struct tm 并使用mktime()。
回答by Taha
The time_t Wikipedia articlearticle sheds some light on this. The bottom line is that the type of time_t
is not guaranteed in the C specification. Here is an example of what you can try:
该time_t的维基百科文章文章揭示了一些这方面的光。最重要的是,time_t
C 规范中不保证的类型。以下是您可以尝试的示例:
Try stringstream
.
试试stringstream
。
#include <string>
#include <sstream>
time_t seconds;
time(&seconds);
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>
time_t t;
time(&t);
std::string ts = boost::lexical_cast<std::string>(seconds);
Wikipedia on time_t:
time_t 上的维基百科:
The
time_t
datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standardtime()
library function. This type is a typedef defined in the standard header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values.Unix and POSIX-compliant systems implement the
time_t
type as asigned integer
(typically 32 or 64 bits wide) which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970 (not counting leap seconds). Some systems correctly handle negative time values, while others do not. Systems using a 32-bittime_t
type are susceptible to the Year 2038 problem.
的
time_t
数据类型是用于存储系统时间值所定义的ISO C库中的数据类型。这些值是从标准time()
库函数返回的。此类型是标准头文件中定义的 typedef。ISO C 将 time_t 定义为算术类型,但没有为其指定任何特定类型、范围、分辨率或编码。同样未指定的是应用于时间值的算术运算的含义。Unix 和 POSIX 兼容系统将
time_t
类型实现为 asigned integer
(通常为 32 或 64 位宽),它表示自 Unix 纪元开始以来的秒数:1970 年 1 月 1 日午夜 UTC(不计算闰秒)。一些系统正确处理负时间值,而其他系统则不能。使用 32 位time_t
类型的系统容易受到2038 年问题的影响。