C ++ convert time_t to string and vice versa

I want to convert time_t to string and vice versa.
I would like to convert time to string using ctime ().
It seems I can not find anything in google or time.h header file, any ideas?
Basically what I'm trying to do is save the date in a file and then read it in order to use it again as time_t.
Also, no library links outside of std, mfc.
One more note that this will have to work on Windows xp and higher and that it is.

Edit

All I want to do is convert time_t to a string (I don't care if it is readable by a person) and then convert it back to time_t. I am just trying to store time_t in a file and read it again (but I do not want any code for this, since there will be more information in the file except time_t).

+6
source share
4 answers

You will need to write your own function for this. These functions convert any primitive type (or any type that overloads the operator <<and / or operator β†’) into a string, and vice versa:

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; } 
+12
source

ctime() returns a pointer to a character buffer that uses specific formatting. You can use sprintf() to parse such a string in its separate parts, save them in struct tm and use mktime() to convert this to time_t .

+3
source

time_t The Wikipedia article in the article sheds light on this. The bottom line is that the type time_t not guaranteed in the C specification. Here is an example of what you can try:

Try stringstream .

 #include <string> #include <sstream> time_t seconds; time(&seconds); std::stringstream ss; ss << seconds; std::string ts = ss.str(); 

A good wrapper around the above method is 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 at time_t:

The time_t data type is the data type in the ISO C library defined for storing system time values. Such values ​​are returned from the standard time() library function. This typedef is a type 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. Also unspecified values ​​are arithmetic operations applied to time values.

Unix and POSIX-compatible systems implement the time_t type as a signed integer (usually 32 or 64 bits) that represents the number of seconds since the Unix era : midnight UTC from January 1, 1970 (not counting the second of the jump). Some systems handle negative time values ​​correctly, while others do not. Systems using the 32-bit time_t type are susceptible to the 2038 problem .

+2
source

Convert time_t to struct tm with gmtime () , then convert struct tm to plain text (preferably ISO 8601 format) using strftime () . The result will be portable, readable and machine readable.

To return to time_t, you simply parse the string back into struct tm and use mktime () .

+1
source

Source: https://habr.com/ru/post/895359/


All Articles